Print PDF file in IFrame using javascript receiving only one page

here is my code to print the pdf file. here, when iam print time only gets one page, I need a solution for this

function printPdf(){ var ifr = document.getElementById("frame1"); //PDF is completely loaded. (.load() wasn't working properly with PDFs) ifr.onreadystatechange = function () { if (ifr.readyState == 'complete') { ifr.contentWindow.focus(); ifr.contentWindow.print(); } } } 
+7
javascript internet-explorer pdf
source share
2 answers

I suspect that since the whole window is being printed (which has the current iframe view with the first page of the PDF rendering). Use <object> instead:

 <!DOCTYPE html> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <script> function PrintPdf() { idPrint.disabled = 0; idPdf.Print(); } function idPdf_onreadystatechange() { if (idPdf.readyState === 4) setTimeout(PrintPdf, 1000); } </script> </head> <body> <button id="idPrint" disabled=1 onclick="PrintPdf()">Print</button> <br> <object id="idPdf" onreadystatechange="idPdf_onreadystatechange()" width="300" height="400" type="application/pdf" data="test.pdf?#view=Fit&scrollbar=0&toolbar=0&navpanes=0"> <span>PDF plugin is not available.</span> </object> </body> 

This code is checked using IE. Other browsers will still make the PDF file but cannot print it.

[UPDATE] If you need dynamic loading and printing, the changes in the above code are minimal:

 <!DOCTYPE html> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge"/> <script> function PrintPdf() { idPdf.Print(); } function idPdf_onreadystatechange() { if (idPdf.readyState === 4) setTimeout(PrintPdf, 1000); } function LoadAndPrint(url) { idContainer.innerHTML = '<object id="idPdf" onreadystatechange="idPdf_onreadystatechange()"'+ 'width="300" height="400" type="application/pdf"' + 'data="' + url + '?#view=Fit&scrollbar=0&toolbar=0&navpanes=0">' + '<span>PDF plugin is not available.</span>'+ '</object>'; } </script> </head> <body> <button id="idPrint" onclick="LoadAndPrint('http://localhost/example.pdf')">Load and Print</button> <br> <div id="idContainer"></div> </body> 
+5
source share
 <iframe src="teste.pdf" id="meupdf" width="800" height="600" /> function printPdf) { var PDF = document.getElementById("meupdf"); PDF.focus(); PDF.contentWindow.print(); } 
+1
source share

All Articles