Click here to Skip to main content
Click here to Skip to main content

Controlling Internet Explorer Print Functionality (Printer Templates)

By , 19 Mar 2010
Rate this:
Please Sign up or sign in to vote.

What are Internet Explorer Print Templates?

Print Templates are a method of customizing the way HTML pages are printed. They use JavaScript to help transform an HTML page into Layout Rectangles to be printed on every page. They give the developer control to change things that normally could be controlled through the print setup. I originally looked into Print Templates as a way to print to a specific named printer without prompting the user for input. In our setup, we have a Contract Printer and a Check Printer for our specific application. Our contract Printers have MICR toner in them, so I wanted to be sure to have the correct printer selected when printing checks or printing contracts.

Print Templates are Internet Explorer only, Why Would I Use Them?

Some developers, like myself, are lucky enough to control the client that their applications run on. Usually this is because the application is a home-grown application that is internal use only. An application that is an intranet application vs. an internet application. One caveat to using print templates is that the site it is running in must be part of the Internet Explorer's trusted sites. In my office, we control this using active directory.

How is this Article Different from Other Print Template Articles

Back in 2000, Chuck Ainslie did an article called "Beyond Print Preview: Print Customization for Internet Explorer 5.5.". In his article, he talks about print templates, what they are and how they are used. However, in order to invoke them, there is a bunch of Microsoft C++ code on how to deal with the IWebBrowser2 interface. After doing some digging around, I found that the built in "InternetExplorer.Application" object contains this interface, and simply calling it from ActiveX gave you the access to the ExecWB method. Furthermore, I had to find out the constant values that were being passed to the method and get the INT values for them. After figuring all of this out, I decided to build a generic templatePrinter JavaScript object to handle all of the lifting for me.

The templatePrinter JavaScript Object

The templatePrinter object gives an abstraction layer to all of the mechanisms behind printing HTML.

An example use would be:

templatePrinters= new templatePrinter()
templatePrinters.htmlContent="HELLO WORLD!";
templatePrinters.printer="YourPrinterName";
templatePrinters.printUsingTemplate("Full/Path/to/PrinterTemplate.html");

Additionally you can set things like margins, and orientation:

templatePrinters= new templatePrinter()
templatePrinters.margins.top=0.25;
templatePrinters.margins.bottom=0.25;
templatePrinters.margins.left=0.25;
templatePrinters.margins.right=0.25;
templatePrinters.orientation="landscape";
templatePrinters.htmlContent="HELLO WORLD!";
templatePrinters.printer="YourPrinterName";
templatePrinters.printUsingTemplate("Full/Path/to/PrinterTemplate.html");	

Included in the source files is a printerTemplateTemplate.html file. This file contains a generic printer template HTML file that will setup the HTML that you pass to the templatePrinter object. It is easy to think about the printerTemplateTemplate file as a UI representation of the content as it relates to multiple pages to be printed.

Other Uses for this Object

I have used this object by wrapping it into a WSC (Windows script component) file and registered the script on the server using regsvr32. This enables you to do fun things with ASP like printing HTML server side. Coupled with a PDF printer, you could convert HTML to PDF very easily server side. Also, if you have an intranet application and you want to add all the networked printers to the server, you could come up with a naming convention so that your application could print to a specific printer from the server that you might have assigned to a user in a database.

Internet Explorer's Default Print Templates

Internet Explorer uses print templates internally for its print preview. You can see the default print template by entering res://ieframe.dll/preview.dlg into your browser. notice that the source HTML shows a reference to preview.js. You can get this file by simply entering res://ieframe.dll/preview.js. These files could be fun if you would like to have a template similar to the Default one, but changed slightly.

History

  • 19th March, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Austin Cherry
Chief Technology Officer Legendary Technologies
United States United States
No Biography provided

Comments and Discussions

 
QuestionWSC (Windows script component) file Pinmemberlerm21-May-13 13:55 
QuestionSecurity issue PinmemberRobert Gaffaney13-Feb-13 5:55 
QuestionWorks great! PinmemberShirley A Burns4-Jun-12 7:07 
QuestionQuestion for Austin Cherry on the Printing [modified] PinmemberWartech20-Dec-11 8:57 
QuestionRe: Question for Austin Cherry on the Printing Pinmemberchristiedensmore15-Jan-13 10:25 
QuestionNow working with IE 8 PinmemberMember 832663322-Oct-11 13:22 
QuestionDoesn't work with IE8. Pinmemberlapsdba8-Aug-11 10:40 
AnswerRe: Doesn't work with IE8. [modified] PinmemberMember 832663322-Oct-11 11:13 
GeneralThere's some problem in IE8 Pinmemberdont88824-Jan-11 21:48 
GeneralRe: There's some problem in IE8 PinmemberWartech27-Dec-11 10:22 
QuestionDoes it work in IE8? PinmemberG2F23-Jan-11 13:36 
GeneralPrint to PDF PinmemberLesF4-Jun-10 16:49 
I am intrigued by your mention of coupling all this with a PDF printer to translate a HTML page to a PDF file.
 
I cannot see if the printer template features any method to programmatically set the file name to output to tho, would that be hidden away in the chosen printer device drivers, or should one be able to set this via te printer template options?
GeneralMore explanations needed PinmemberJPaula23-Mar-10 1:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 19 Mar 2010
Article Copyright 2010 by Austin Cherry
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid