Print from Internet Explorer without the Printer Selection Box
Quickie post...
If you have a default printer selected on your system, then this HTML page with jscript should print without showing the printer selection dialog (I will embellish this post later if I have time):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
<script language='VBScript'>
Sub Print()
OLECMDID_PRINT = 6
OLECMDEXECOPT_DONTPROMPTUSER = 2
OLECMDEXECOPT_PROMPTUSER = 1
call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
End Sub
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
</script>
<script language="javascript" type="text/javascript">
// <!CDATA[
function Button1_onclick() {
window.print();
}
// ]]>
</script>
</head>
<body>
<P>I had an issue where someone wanted to stream a file that was generated on the web server and prevent it from caching on the client. The correct way to do this is to specify the Cache-Control: no-cache header. When this was done however the pdf file did not display using https and on Windows XP. After some research the method that worked for this scenario was to define an OBJECT tag and point the SRC attribute to the page that generates the file on the fly.</P>
<p>
<input id="Button1" type="button" value="button" onclick="Button1_onclick()" /></p>
<P> Example code:</P>
<P>This is an example of the failing page:</P>
</body>
</html>
Comments
Anonymous
August 12, 2009
Great job! I've searched it long time! Thanks a lot!Anonymous
August 12, 2009
I appreciate the feedback! Is there any key words I could have added to this post to make it more discoverable for you?Anonymous
August 25, 2009
thanks i try to write in english my PC has a default printer, but i want that for example that the internet explorer print with pdf and not to the default printer. is your solution able to this and when yes what i have to do with your code, ? thansk for your soon answer.Anonymous
August 26, 2009
Albert, Very good English! What you want to do you cannot do easily. You have two options that I can think of:
- Write a ActiveX Control you can embed and call from your page to set the default printer. Something like these KB's: http://support.microsoft.com/kb/300518 http://support.microsoft.com/kb/321025
- Use winmgt objects (but this would require your page to be an HTML application HTA since IE will not have sufficient privilages to create the object... This is by design) var service = GetObject('winmgmts:') // Get the instance of the Win32_Printer instance var Systems = new Enumerator (service.InstancesOf("Win32_printer") ); var PName = Systems.item(); // Get Printer var Printer = (PName.name); alert(Printer);