简体   繁体   中英

Print pdf from iframe

I dynamically create a pdf in a iframe:

<iframe name="output" id="output"></iframe>

and try to print from it:

window.frames["output"].focus();
window.frames["output"].print();

But how you can see in this example:

http://jsfiddle.net/FEvq6/78/

It prints a blank site.

It prints blank page because you're printing the iframe itself. Here is what you should do:

1. The iframe should contain EMBED tag which will load the PDF (for some reason stack overflow did not formatted code well, please see paste bin link below):

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

2. Then you should call the EMBED object to print the document. But since it may require some time for it to load you would need a timeout:

var doPrinting = (id){
    var pdfObject = document.getElementById(id);
    if (typeof(pdfObject.print) === 'undefined') {    
         setTimeout(function(){ doPrinting(id); }, 1000);
    } else {
        pdfObject.print();
    }
 }

 doPrinting('pdf');

Here is paste bin of the above: http://pastebin.com/s6qSTE8t

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<strike>funny</strike>.

    <iframe src="file_to_print.pdf" class="frameSet" type="application/pdf" runat="server" name="I5" scrolling="yes" width="100%" height="400">  </iframe>

</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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