在Chrome中打印嵌入式PDF文件

5

我认为Chrome和Firefox对DOM的解释正在干扰我的工作。我正在尝试从浏览器(动态创建)打印PDF,因为我无法在打印HTML页面时正常打印页眉和页脚。现在,我只是从php页面使用fpdf发送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#没有print方法”的错误。

所以我尝试使用thinking嵌入,认为它导致了在Chrome中不正确的对象解释:

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

alt: test.pdf

并在onClick中以相同方式调用,其中documentID =“pdfD2” … “未捕获的TypeError:对象#没有方法'print'”

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

我感到非常沮丧,因为Chrome是我的首选…我甚至禁用了chrome的内置PDF查看器并使用了Adobe 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')">
1个回答

0
我认为错误在这一行:
((function(){return document.getElementById(documentId);})()).print();

这意味着你将未完成的DOM“打包”(可能是)到闭包中。
它在下一行检查“undefined”打印之前执行。
除此之外,为什么要使用timeout,而不是直接使用onloadDOMReady事件?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接