Jeff Sanders Technical Blog

I am a Microsoft employee that has worked on all aspects of the Web Stack for a long time. I hope these blogs are useful to you! Use this information at your own risk.


<< Go Back

Print From Internet Explorer Without The Printer Selection Box

- 01 Oct 2008

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):<p mce_keep="true"> </p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” <font color=#0000ff>http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</font>“><?xml:namespace prefix = o ns = “urn:schemas-microsoft-com:office:office” /></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">

 </p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">«/span>html xmlns=”<font color=#0000ff>http://www.w3.org/1999/xhtml</font>“></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">«/span>head></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">    «/span>title>Untitled Page</title></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">    «/span>script language=’VBScript’></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">Sub Print()</p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">OLECMDID_PRINT = 6</p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">OLECMDEXECOPT_DONTPROMPTUSER = 2</p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">OLECMDEXECOPT_PROMPTUSER = 1</p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)</p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">End Sub</p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">document.write “<object ID=’WB’ WIDTH=0 HEIGHT=0 CLASSID=’CLSID:8856F961-340A-11D0-A96B-00C04FD705A2′></object>”</p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"></script></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"> </p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"> </p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">«/span>script language=”javascript” type=”text/javascript”></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">// <!CDATA[</p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"> </p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">function Button1_onclick() {</p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">window.print();</p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">}</p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"> </p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">// ]]></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"></script></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"></head></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">«/span>body></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">«/span>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> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"> </p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">«/span>p></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">    «/span>input id=”Button1″ type=”button” value=”button” onclick=”Button1_onclick()” /></p></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"> </p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"> </p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">«/span>P> Example code:</P></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"> </p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt">«/span>P>This is an example of the failing page:</P></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"> </p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"></body></p> <p class=MsoNormal style="MARGIN: 0in 0in 0pt"></html></p> <p mce_keep="true"> </p>

<< Go Back