Thursday, March 21, 2013

Click a button to print an embedded PDF file without the printing dialog in Internet Explorer (silent print)

I recently helped a friend implementing a button to click, and it sends a hosted pdf to the defaulted printer without the printing dialog in IE8. I googled this about a few dozen pages and finally came across http://stackoverflow.com/questions/975652/silent-print-a-embedded-pdf. So here is the test.html. And due to the ActiveX used, you have to make this page to be trusted or in intranet.
 <html>  
 <head>  
 <title>Test</title>  
 <script>  
 function printpdf(pdf_path) {  
  // remove the previous pdf <object> if existed  
  var elem = document.getElementById("pdf");  
  if (elem) {  
   elem.parentNode.removeChild(elem);  
  }  
  document.body.insertAdjacentHTML('beforeEnd', '<object id="pdf" classid="clsid:CA8A9780-280D-11CF-A24D-444553540000" width="0" height="0"><param name="src" value="' + pdf_path + '"/></object>');  
  pdf.printAll();  
  return false;  
 }  
 </script>  
 </head>  
 <body>  
 <!-- http://stackoverflow.com/questions/975652/silent-print-a-embedded-pdf -->  
 <button onclick="printpdf('sample.pdf');">Print PDF</button>  
 </body>  
 </html>  
I put it in my dropbox as well: https://dl.dropbox.com/u/48348/pdf/pdf.html (put dropbox domain into trusted sites) EDIT: Adobe Reader 8 was reported to leave a blank Adobe Reader application running whenever you click, then version 11 doesn't have this problem.

5 comments:

devMom said...

printAll() ??

Anonymous said...

I am sure there're some other methods to call besides printAll() if you have other requirements.

Unknown said...


Thank you, but this only works in internet explorer, do you have a Chrome solution? Edge?

Matthew Kwong said...

Googled this today (5/11/2017) and found a link to

https://productforums.google.com/forum/#!topic/chrome/JJNzUxxtMio

talking about a chrome command line option " --disable-print-preview" or "--kiosk --kiosk-printing" (no idea what kiosk is though)

Unknown said...

Thank you, for what I have seen in chrome to disable print preview is only possible with the kioske mode, I can not find any way to do it programatically..