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!

4
  • 3
    If you want to print the .pdf on paper, using a real printer, then it might be better to check here for different plugins and solutions on how to print out a pdf via jQuery. Additionally, this question on Stack Overflow could help. Good luck! Commented May 6, 2013 at 14:27
  • @Thara - this sounds like a good answer. I'd vote it up. Commented May 7, 2013 at 14:15
  • I tried with the tutorial that you linked. But I always print a blank page! Commented May 8, 2013 at 10:13
  • 1
    PrintJs so good printjs.crabbly.com Commented Apr 24, 2018 at 20:33

2 Answers 2

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();
});
Sign up to request clarification or add additional context in comments.

1 Comment

This tirggers the following error: "blocked a frame with origin from accessing a cross-origin frame"
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.