简体   繁体   English

打印通过Javascript加载到IFrame中的PDF

[英]Print a PDF loaded in IFrame via Javascript

Why it can't be done? 为什么不能做到? I spent 2 days researching it. 我花了2天的时间进行研究。 It's just not possible to do it! 只是不可能做到这一点!

Recent versions of Chrome does let parent window access PDF's iframe. Chrome的最新版本确实允许父窗口访问PDF的iframe。 But neither FF or IE let me touch the iframe that contains "application/pdf" on it. 但是FF或IE都不会让我触摸上面包含“应用程序/ pdf”的iframe。

The code is simple: 代码很简单:

<iframe id="pdfFrame" src="/mydomain/document.pdf"></iframe>

If I call: 如果我打电话给:

pdfFrame.contentWindow.print();

FF says: FF说:

Error: Permission denied to access property 'print'

IE says: IE说:

SCRIPT65535: Invalid calling object 

Has anyone faced the same situation before? 有人遇到过同样的情况吗? Has anyone managed to solve it? 有人设法解决吗? Does anyone have a different suggestion? 有人有其他建议吗? I just want to download a PDF from my own domain and send it gracefully to the printer. 我只想从我自己的域下载PDF,然后将其优雅地发送到打印机。

I tried formatting my document with CSS @media. 我尝试使用CSS @media格式化文档。 But it's a mess. 但这是一团糟。 Only print half of the document no matter what I setup the frame to be 100% everthing. 无论我将框架设置为100%,都只打印文档的一半。

Thanks!! 谢谢!!

Show the pdf using the below html. 使用以下html显示pdf。 Iframe is for chrome printing iframe用于镀铬打印

<object id="exPDF" type="application/pdf" data="pdfUrl#toolbar=1&navpanes=0&statusbar=0&messages=0" style="width:100%;height:475px;" >

<iframe id="iframePDF" name="iframePDF" src="pdfUrl" width="1" height="1" >

//javascript
try {

    if (isChrome) {
        var iframe = document.getElementById('iframePDF');
        if (iframe.src) {
            var frm = iframe.contentWindow;

            frm.focus();// focus on contentWindow is needed on some versions  
            frm.print();
        }
    }
    else {
        var _pdf_o = document.getElementById('exPDF');
        if (_pdf_o != undefined) {
            _pdf_o.print();
        }
        else {
            alert("pdf object not found");
        }
    }
} catch (e) {
    alert(e.description);
}

This is a messy solution, depending on your depth of experience. 这是一个麻烦的解决方案,取决于您的经验深度。 BUT, I suggest adding a window event listener on the iframe content. 但是,我建议在iframe内容上添加一个窗口事件监听器。 And then emit a message/ post message on the parent window. 然后在父窗口上发出消息/发布消息。 Then when the message is picked up on the iframe, have that page print out the pdf, which should then be on the same level. 然后,当在iframe上收到邮件时,请将该页面打印出pdf,然后将其打印在同一级别上。 Granted, I haven't actually done that personally, but I have worked with generalized event listeners /post messages that go back and forth between iframes. 当然,我实际上还没有亲自做过,但是我已经处理了在iframe之间来回往返的通用事件侦听器/发布消息。

Maybe you don't have any HTML in the iframe though, if it's just a direct link to the pdf, try going with another answer first. 不过,也许在iframe中没有任何HTML,如果它只是与pdf的直接链接,请尝试首先尝试另一个答案。

A good place to start perhaps: https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage 也许是一个开始的好地方: https : //developer.mozilla.org/en-US/docs/Web/API/Window.postMessage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM