2

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!

CraigTeegarden
  • 8,173
  • 8
  • 38
  • 43
vcestra
  • 41
  • 1
  • 1
  • 5
  • 3
    If you want to print the .pdf on paper, using a real printer, then it might be better to [check here](http://www.jquery4u.com/plugins/10-jquery-print-page-options/) for different plugins and solutions on how to print out a pdf via jQuery. Additionally, [this question](http://stackoverflow.com/questions/687675/can-a-pdf-files-print-dialog-be-opened-with-javascript) on Stack Overflow could help. Good luck! – Alice May 06 '13 at 14:27
  • @Thara - this sounds like a good answer. I'd vote it up. – Dan Pichelman May 07 '13 at 14:15
  • I tried with the tutorial that you linked. But I always print a blank page! – vcestra May 08 '13 at 10:13
  • 1
    PrintJs so good http://printjs.crabbly.com/ – Phd. Burak Öztürk Apr 24 '18 at 20:33

2 Answers2

4

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();
});
Bishal Paudel
  • 1,896
  • 2
  • 21
  • 28
0

I believe the Problem is that the content is not loaded yet? I had the same issue in IE8. I had to save the document, reopen it, print, and close the window again.

Setup
  • 330
  • 2
  • 21