1

I have pdf files stored on my server.

I would like the user to be able to click a link/button and have a print dialog popup so they can print the pdf. I tried convincing them that its not a big deal to click download, then print it from the browser tab in which it opens, but they are fickle. :)

I have tried a few javascript solutions, mostly what I end up with is:

contentWindow is not defined

My js snippet is as follows

  a = $("a:contains('Download PDF')")
  url = a.attr('href')
  uid = unique_id()
  uid = "abc_" + uid
  ifrm = $("<iframe width='500px' src='" + url + "' id='" + uid + "' name='" + uid + "'  onload='this.contentWindow.print()'></iframe>")
  $('body').append(ifrm)

I have also tried

  ...
  ifrm = $("<iframe width='500px' src='" + url + "' id='" + uid + "' name='" + uid + "' ></iframe>")
  $('body').append(ifrm)
  pdf = document.findElementById(uid)
  pdf.contentWindow.print();

And a few other variations with the same result. Or I get a print of "About:Blank" presumably because print is called before the document is loaded in the iframe.

The pdf opens and renders in the iframe.

Any ideas

recursive_acronym
  • 2,981
  • 6
  • 40
  • 59
  • I agree with you; the user should download the PDF to print it. – Nathan Wall Sep 12 '12 at 04:01
  • The print dialog for PDFs is generally triggered from the PDF viewer, which you have no control over (unless their `` code lets you). – nneonneo Sep 12 '12 at 04:04
  • If you have control over the content of the PDF, here's an interesting possible solution: Apparently, [according to this](http://www.actionscript.org/forums/showthread.php3?t=89594), you can embed some JavaScript in the PDF which tells it to print itself when it's loaded.. maybe then loading it in the IFRAME would do the trick? – Nathan Wall Sep 12 '12 at 04:06
  • Old answer that might help you out: http://stackoverflow.com/questions/205180/how-to-print-a-pdf-from-the-browser – endyourif Sep 12 '12 at 04:07
  • If you have control over the pdf-files you can make them [auto-printable](http://stackoverflow.com/a/11305061/36866), WITHOUT using javascript. (Javascript can and should be disabled due to security reasons). – some Sep 12 '12 at 04:14

0 Answers0