【问题标题】:Printing embeded PDFs in chrome在 chrome 中打印嵌入的 PDF
【发布时间】:2012-11-29 08:45:33
【问题描述】:

所以我认为 Chrome 和 Firefox 对 DOM 的解释让我很困惑。我正在尝试从浏览器(动态创建)打印 pdf,因为我无法正常打印 HTML 页面的页眉和页脚。现在我只是使用 fpdf 从 php 页面发送 PDF 并使用工具栏或右键单击并打印,但现在客户想要页面上的一个按钮来启动打印对话框,但当然除了 PDF 之外不打印任何其他内容...... . 所以我嵌入了它:

    <embed
        type="application/pdf"
        src="print_pdf.php"
        id="pdfDocument"
        width="100%"
        height="100%" />

还有一个按钮的onClick调用

    <script type="text/javascript">

        function printDocument(documentId) {

        ((function(){return document.getElementById(documentId);})()).print();


        //Wait until PDF is ready to print    
        if (typeof document.getElementById(documentId).print == 'undefined') {

            setTimeout(function(){printDocument(documentId);}, 1000);

        } else {

            var x = document.getElementById(documentId);
            x.print();
        }
    }
    </script>

其中 documentID = "pdfDocument"

这在 IE9 中效果很好,但 chrome 和 mozilla 都说“Uncaught TypeError: Object # has no method 'print'”

所以我尝试使用 Thinking embed 导致 chrome 中的对象解释不正确:

<object data="print_pdf.php" type="application/pdf" width="100%" height="100%" id="pdfD2">

alt : test.pdf

并调用了相同的 onClick,其中 documentID = "pdfD2"... "Uncaught TypeError: Object # has no method 'print'"

然后我尝试了一个 iframe: ...“未捕获的 TypeError:对象 # 没有方法 'print'”

鉴于 Chrome 是我的首选,我感到非常沮丧......我什至禁用了 chrome 的内置 PDF 视图并使用了 Adob​​e 10.xxx.. ARGH!!!

仅供参考,我的简单按钮标签是:

<input type="button" value="Print Rx" onclick="printDocument('pdfDocument')">
<input type="button" value="Print Rx2" onclick="printDocument('pdfD2')">
<input type="button" value="Print Rx3" onclick="printDocument('pdfD3')">

【问题讨论】:

    标签: javascript google-chrome


    【解决方案1】:

    我认为错误是:

    ((function(){return document.getElementById(documentId);})()).print();
    

    这意味着你“打包”(很可能)未完成的 DOM 到闭包中。

    它位于检查“未定义”打印的下一行之前。

    除此之外,为什么还要使用超时,而不是只使用onloadDOMReady 事件?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-01
      • 2012-01-28
      • 2012-07-21
      • 1970-01-01
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      相关资源
      最近更新 更多