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
There seems to be a bug in the code sample. PrintCommandObject will always be null. Besides, PrintCommandObject need not be a global variable. For the above code to get working, I had to modify “printPage()” method as shown below:
The key difference being call to “document.getElementById(’PrintCommandObject’);
function printPage(){
var printCommandObject = document.getElementById(”PrintCommandObject”);
if(printCommandObject)
{
try{
printCommandObject.ExecWB(6, 2);
printCommandObject.outerHTML = “”;
}
catch(e){
window.print();
}
}
else{
window.print();
}
}
Thanks for the Article. It was really more than helpful.
Regards,
Srikanth
The above code is not working for IE8 and windows XP combination. Any body can help to solve this issue.
[...] 9th 2008 8:46am [-] From: justtalkaboutweb.com [...]
[...] “this is security issue”. And guys from Apple took this sh*t too darn seriously because under windows you can bypass print dialog rather easily by unblocking ActiveX support. And such trick should be implemented in Mac OS too (i mean - taking [...]