Click here to Skip to main content
13,351,896 members (88,574 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 19 Mar 2010

Controlling Internet Explorer Print Functionality (Printer Templates)

, 19 Mar 2010
Rate this:
Please Sign up or sign in to vote.
Using JavaScript to control the output and format of printed webpages in Internet Explorer

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!";

Additionally you can set things like margins, and orientation:

templatePrinters= new templatePrinter();
templatePrinters.htmlContent="HELLO WORLD!";

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.


  • 19th March, 2010: Initial post


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

You may also be interested in...


Comments and Discussions

QuestionDoes it work on IE 11? Pin
Member 964331416-Jul-15 12:53
memberMember 964331416-Jul-15 12:53 
QuestionWSC (Windows script component) file Pin
lerm21-May-13 14:55
memberlerm21-May-13 14:55 
QuestionSecurity issue Pin
Robert Gaffaney13-Feb-13 6:55
memberRobert Gaffaney13-Feb-13 6:55 
QuestionWorks great! Pin
Shirley A Burns4-Jun-12 8:07
memberShirley A Burns4-Jun-12 8:07 
QuestionQuestion for Austin Cherry on the Printing Pin
Wartech20-Dec-11 9:57
memberWartech20-Dec-11 9:57 
QuestionRe: Question for Austin Cherry on the Printing Pin
christiedensmore15-Jan-13 11:25
memberchristiedensmore15-Jan-13 11:25 
QuestionNow working with IE 8 Pin
Member 832663322-Oct-11 14:22
memberMember 832663322-Oct-11 14:22 
Hello there fellow developers, I have been looking for this also for some time now and decided to take a shot at using it also. Well just like everyone else I had the same identical issues with it, but I kept at it for way to many hours until I got it to work perfectly. Her is the how for everyone to enjoy this great piece of code: NOTE: I am using Internet Explorer 8.

//TODO: CHANGE THE PATH TO THE printerTemplateTemplate.html file. Nothing will happen unless you have the correct path!!!

From: PrintTemplateLocation= "./printerTemplateTemplate.html";
To: PrintTemplateLocation= "C:/myFolder/printerTemplateTemplate.html";

From: strPrinter"ContractPrinter";

To: strPrinter="Lexmark E260d"; // You can find the name of your default printer in your printer and faxes dialog.

To inject your pages into your layout:

place your content inside the content div, you can do includes for Coldfusion and it works great.

I am running this in a Windows XP platform. In addtion, I tested it in ColdFusion and it works just like plain HTML.

I hope this helps everyone.
QuestionDoesn't work with IE8. Pin
lapsdba8-Aug-11 11:40
memberlapsdba8-Aug-11 11:40 
AnswerRe: Doesn't work with IE8. Pin
Member 832663322-Oct-11 12:13
memberMember 832663322-Oct-11 12:13 
GeneralThere's some problem in IE8 Pin
dont88824-Jan-11 22:48
memberdont88824-Jan-11 22:48 
GeneralRe: There's some problem in IE8 Pin
Wartech27-Dec-11 11:22
memberWartech27-Dec-11 11:22 
QuestionDoes it work in IE8? Pin
G2F23-Jan-11 14:36
memberG2F23-Jan-11 14:36 
GeneralPrint to PDF Pin
LesF4-Jun-10 17:49
memberLesF4-Jun-10 17:49 
GeneralMore explanations needed Pin
JPaula23-Mar-10 2:02
memberJPaula23-Mar-10 2:02 

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

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

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