Print a pdf with javascript or jquery

23,499

Create a iframe in html:

<iframe id="pdf-iframe">

Then change the src of that iframe and on load, print it.

$('#pdf-iframe').attr("src", pdf_url).load(function(){
    document.getElementById('pdf-iframe').contentWindow.print();
});
Share:
23,499
vcestra
Author by

vcestra

Updated on August 24, 2020

Comments

  • vcestra
    vcestra over 3 years

    I need to print a .pdf file (I generate it with jasperReport and I save it on a server) when I click on a print.gif button. Now I found this solution that works for Firefox but not for IE!

    function printPdf(){    
      var url= document.getElementById('_url').value;    
      newwindow=window.open();  
      newdocument=newwindow.document;   
      newdocument.write('<embed   type="application/pdf"  src="'+url+'"
           id="pdfDocument"   height="100%" width="100%" ></embed> <SCR'+'IPT
           LANGUAGE="JavaScript">window.print();window.close();</SCR'+'IPT>'); 
    }
    

    I tried also document.close(); window.focus(); window.print(); window.close();, but they didn't work!

    I read a lot of topics, but I can't find a solution for this issue!

  • Pedro Joaquín
    Pedro Joaquín over 4 years
    This tirggers the following error: "blocked a frame with origin from accessing a cross-origin frame"