Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

Controlling Internet Explorer Print Functionality (Printer Templates)

4.25/5 (11 votes)
19 Mar 2010CPOL3 min read 1   2.3K  
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:

JavaScript
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:

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