0

Why it can't be done? I spent 2 days researching it. It's just not possible to do it!

Recent versions of Chrome does let parent window access PDF's iframe. But neither FF or IE let me touch the iframe that contains "application/pdf" on it.

The code is simple:

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

If I call:

pdfFrame.contentWindow.print();

FF says:

Error: Permission denied to access property 'print'

IE says:

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.

I tried formatting my document with CSS @media. But it's a mess. Only print half of the document no matter what I setup the frame to be 100% everthing.

Thanks!!

2 Answers 2

2

Show the pdf using the below html. Iframe is for chrome printing

<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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Tried your solution. It works fine for Chrome. But in both IE11 and FF34, <object> TAG does not have print() function defined. What else can I try?
0

This is a messy solution, depending on your depth of experience. BUT, I suggest adding a window event listener on the iframe content. 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. 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.

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.

A good place to start perhaps: https://developer.mozilla.org/en-US/docs/Web/API/Window.postMessage

1 Comment

Thanks Eleanor. But the thing is, the only way I could successfuly print a PDF was to load it directly in the iframe, and then call iframe's print() fucnction. You are right. I don't have any HTML in the iframe, pure PDF content. SO there's no way to add an event listener in the iframe to capture the message sent from the main window... :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.