Notaras
Notaras

Reputation: 717

Javascript: printing the contents of iframe on page load not working

background: i have a page in my web app with an iframe in it that displays a pdf file.

I require a browser's print preview window to appear as soon as this page in my web app loads. The print preview window will contain the contents of the iframe and print it. Currently, i bind a function on the iframe's load event as follows:

$('#iframe').load(function () {
    $("#iframe").get(0).contentWindow.print();
});

This works fine when i debug locally. However when i upload to server, the print preview screen will not automatically popup. In fact i have to first click on a button or element for it appear. Is there a way around this?

Thanks in advance..

Upvotes: 1

Views: 1313

Answers (2)

Notaras
Notaras

Reputation: 717

Numerous methods using either jQuery or plain javascript did not fix the issue. However, after i set a 1 second delay to the print function, it worked!

 setTimeout(function () {   
        $("#iframe").get(0).contentWindow.print();
    }, 1000);

I suppose it has something to do with the server. It probably needs extra time to process the pdf in the iframe

Upvotes: 1

Lumi Lu
Lumi Lu

Reputation: 3305

Try this,

window.frames['#iframe'].focus();
window.frames['#iframe'].print();

Upvotes: 1

Related Questions