javascript - 打印由 google docviewer 通过 javascript 在 iframe 中打开的 pdf

标签 javascript pdf iframe printing google-document-viewer

是否可以通过 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/

相关文章:

javascript - MongoDB 属性迭代

javascript - 添加新模型后渲染

javascript - iframe insideHTML 为空

javascript - 使用 javascript 将页面加载到 Iframe

javascript - 在 AngularJS 中插入来自可信源的 iframe

javascript - 如何使用 jQuery 用动态生成的元素包装页面元素?

javascript - 类似 Facebook 的 anchor 滚动

excel - "Cannot insert object"将 PDF 文件/对象插入 Microsoft Excel 时出错

php - 我如何在 html 中显示 pdf(响应/跨浏览器解决方案)

c# - 在 .net 中以编程方式突出显示 PDF - 是否有任何 API 和库来生成偏移文件或是否有更好的方法?