Print pdf from iframe

后端 未结 2 1733
旧时难觅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

提交回复
热议问题