是否可以通过 javascript 打印在 iframe 中打开的 pdf(由 google 文档查看器提供)...?出于某种原因,我希望用户位于同一页面上并从同一页面打印 pdf...
这是 iframe 的代码:
<button style="height:35px; cursor:pointer; " class="button" id="print"> <img src="images/print.png"/> <span style=" vertical-align: super; "> Print this note </span> </button>
<?php echo '<iframe id="PDFtoPrint" src="http://docs.google.com/gview?url='.JURI::root().'resources/pdf_full/'.$full_name.'&embedded=true" style="width:800px; height:1000px; " frameborder="0"></iframe>';?>
这是打印的 jquery 代码...
jQuery(document).ready(function($) {
$('#print').click(function(){
// window.frames["PDFtoPrint"].focus();
// window.frames["PDFtoPrint"].print(); // NOT WORKING
// var PDF = document.getElementById('PDFtoPrint');
// PDF.focus();
// PDF.contentWindow.print(); // NOT WORKING
// $("#PDFtoPrint").get(0).contentWindow.print(); // NOT WORKING
// document.getElementById("PDFtoPrint").contentWindow.print(); // NOT WORKING
});
});
最佳答案
不,由于 JavaScript 的同源策略,这是不可能的。 JavaScript 无法操作从另一个域加载到框架中的页面的 DOM 或与之交互。据我所知,有一个异常(exception),但这需要 Google 明确允许您的网站这样做,但他们不会。您可以在此处阅读有关同源策略的更多信息:https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy
关于javascript - 打印由 google docviewer 通过 javascript 在 iframe 中打开的 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21231354/