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).

ie6-security-settings.gif

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!

firefox-about-configs.gif

(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!


Related Posts