Techniques Behind Modern Web
9 May
Searching a solution for how to bypass printer dialog box when printing multiple web pages in Internet Explorer which is required in one of my company’s projects I found a hack from DevX.com.
1 2 3 4 5 6 7 8 9 10 | if (navigator.appName == "Microsoft Internet Explorer") { var PrintCommand = '<object ID="PrintCommandObject" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>'; document.body.insertAdjacentHTML('beforeEnd', PrintCommand); PrintCommandObject.ExecWB(6, -1); PrintCommandObject.outerHTML = ""; } else { window.print(); } |
But it does not work in Windows XP SP2 (and Windows Server 2003 SP1 or above). Further researches reveal that the augments for printing command should be:
PrintCommandObject.ExecWB(<strong>6, 2</strong>);
Another problem is IE considers the print ActiveX control not “safe” (though it’s a native one, no?) so you must enable “Initialize and script ActiveX control not marked as safe” option under site security settings (see picture below).

Please note that lowering security settings in IE should be made with care. You should add the app address to “Trusted sites” zone and set security settings as above for that zone only.
Finally, I come to the scripts below that support fallback to normal JavaScript print command “window.print()” when the ActiveX is unable to initialize or printing from other browsers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | var PrintCommandObject = null; function printPage(){ if(PrintCommandObject) { try{ PrintCommandObject.ExecWB(6, 2); PrintCommandObject.outerHTML = ""; } catch(e){ window.print(); } } else{ window.print(); } } window.onload = function(){ if (navigator.appName == "Microsoft Internet Explorer"){ // attach and initialize print command ActiveX object try{ var PrintCommand = '<object id="PrintCommandObject" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" width="0" height="0"></object>'; document.body.insertAdjacentHTML('beforeEnd', PrintCommand); } catch(e){} } setTimeout(printPage, 1); }; |
In Firefox, the solution is much simpler: just type “about:config” to address bar and add a new Boolean preference item named “print.always_print_silent” then set value of the newly-added item to “true“. Done!

(Note: may you want to disable printing progress dialog, just set “print.show_print_progress” item value to “false” too.)
Hope this would help those who are struggling with the same issue. Please share your solutions for Safari, Opera…? Thanks.
Free Website Magazine: Know more than your competitors with Website Magazine
Don't forget to subscribe
so you don't miss out on future posts!
I want to print option but print dialog box is not enable
Just what I needed, thanks.
For the Prism browser for linux (which is just another skin for firefox), close Prism and edit this file with your text editor:
~/.prism/your_app_name/blablabla.default/prefs.js
and add the line:
user_pref(“print.always_print_silent”, true);
Which is the same thing but without the user interface.
This may also work on firefox on the ~/.mozilla/firefox/blahblahblah.default/prefs.js
thanks.. great tweak…
sad
thanks man but do not understand the function PrintPage because PrintCommandObject always going to be null and never enters the conditional
I solved it by removing the line
var PrintCommandObject = null;
thanks
Does this work for IE7 or higher on windows server 7?
Thanks
hi,
Thanks for the code.
Its working fine but does not print data of the page.
In Mozilla the printed page contain data but in IE the page is blank…:(
Plz help
thanks its great help!
please tell me how to select printer? it sent only to default and i need to sent prints on two different printers one is default.
thankssss !!!!!! you saved me …
[...] 9th 2008 8:46am [-] From: justtalkaboutweb.com [...]
[...] mandar el ticket de venta directamente a la impresora. “Googleando” encontramos esta solución, espero y les sea útil. Easy AdSense by Unreal window.fbAsyncInit = function() { [...]