The print engine that we use for one product I work with is based on Internet Explorer’s custom print templates functionality. This actually works really well, and gives us lots of flexibility and generally is pretty straightforward to use. Unfortunately, we have run into a bit of a problem recently when trying to print documents in IE8 standards compliance mode: multiple page documents print blank pages, are missing content or sometimes even fail to start printing.
I have been able to reproduce this issue using Microsoft’s own printtemplates.exe example (link in Introduction of article), with only a couple of minor changes to trigger the problem.
Symptoms
The issue arises when all of the following specific conditions are met:
- The document to be printed is in IE8 standards compliance mode. This is mostly easily forced with the X-UA-Compatible META element:
<META HTTP-EQUIV='X-UA-Compatible' CONTENT='IE=8' />
- The document to be printed is greater than 1 page long.
- The print template uses LAYOUTRECT elements of differing sizes. This is a common requirement for letters which may have an address section or expanded letterhead at the top of the first page, but will have a much smaller letterhead on subsequent pages.
The Baseline
We’ll use the printtemplates.exe sample provided by Microsoft. The sample Template3.htm in the example shows how to dynamically create LAYOUTRECT elements. Here’s what happens before we make any changes to the example:
Print Template sample application, working with defaults |
Print Preview, defaults, page 1 |
Print Preview, defaults, page 2 |
As shown in the screen shots, the print preview displays as expected.
Reproducing the Problem
With Internet Explorer 8, the problem can be reproduced as follows:
- Start printtemplates.exe, and select Template3.htm.
- Click the Page Source button. In the HTML file that is opened, add the META element as the first child of the HEAD element. This forces the page into IE8 standards compliant mode. Save the file as sample.htm:
<HTML> <HEAD> <META HTTP-EQUIV='X-UA-Compatible' CONTENT='IE=8' /> <TITLE>Print Template Samples</TITLE> </HEAD>
- Back in printtemplates, click the Template Source button. In the HTML file that is opened, make the highlighted change to the OnRectComplete function. This small change means pages other than the first page have a smaller LAYOUTRECT than the initial page. Save this file as template.htm.
function OnRectComplete() { if (event.contentOverflow == true) { document.all("layoutrect" + (iNextPageToCreate - 1)).onlayoutcomplete = null; newHTML = ""; newHTML += "STYLE='height:5.5in'/>"; newHTML += ""; pagecontainer.insertAdjacentHTML("beforeEnd", newHTML); iNextPageToCreate++; } }
- Enter the full paths of sample.htm and template.htm into the respective fields in the print template sample, then press ENTER in the page field to load the modified sample.htm.
When you press Print Preview now, the print preview window will show blank pages instead of the expected content, and will have only 2 pages instead of the 5 or more that were shown previously:
The print template application with modified page and template ready to roll |
Failing print preview, page 1 |
Failing print preview, page 2 |
If either of the changes are removed from the sample.htm and template.htm, the problem does not occur. In Internet Explorer 9, the problem also occurs but appears to be resolved when using IE=9 in the META tag. However, as Internet Explorer 9 is not available for Windows XP, this is not a viable solution for us.
Workarounds
We’ve found a few workarounds. None of these are very viable for us but I list them for completeness:
- Don’t use LAYOUTRECT elements of differing sizes. This is a non-starter for us.
- Don’t use IE8 standards compliant mode. This would mean rewriting a number of reports and some complicated CSS to workaround bugs in IE7’s standards compliant mode, but may be a way forward. Or use quirks mode with all the joy that brings.
- Use IE9.