Print pdf from iframe

后端 未结 2 1734
旧时难觅i
旧时难觅i 2021-01-23 10:28

I dynamically create a pdf in a iframe:


and try to print from it:

w         


        
相关标签:
2条回答
  • 2021-01-23 11:03

    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

    0 讨论(0)
  • 2021-01-23 11:05
    <!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>
    
    0 讨论(0)
提交回复
热议问题