cartoon of hip woman in red

A Perl program that takes over print

I thought I was so hip when I coded my pages with DIVs.

Until, that is, I tried to print them. My DIVS are coded with absolute positioning, so that I can place a leftside and a rightside DIV next to each other.

My other two DIVS, which I call masthead and navbar, get suppressed, and they should—they're only for navigation on the screen. But the two remaining DIVS are important for print. Think of them as two columns of a magazine or newspaper.

To see an example, view my Life Planners Newsletter.

The left and right DIV looked great on the screen. But print was another matter.

First I'll tell you what happened in IE. Then I'll tell you what happened in Netscape. Last, I'll tell you what happened in Opera.

I wondered how well these browsers would have printed, had I coded my pages in a table layout instead of DIVs. So I decided to run a test. I re-coded one of my typical pages, such as the one mentioned above, in table layout. Then I tried to print the page. In all three browsers that I tried: IE, Opera and Netscape, they were fixed. Yikes! What to do? Are browsers building in that much disincentive to migrate to DIV layout?

Here were some solutions that were suggested to me or that I thought of fleetingly:

What to do about bad browser printing of long DIVs?

Then I had an idea. Like the sneaky Grinch, it was a wonderful, awful idea. I put on a Santee-Claus hat, decked out my dog with some reindeer antlers, and set out to pretend my pages are something they're not. How?

cartoon of presumably Santa upside down in the chimney

It's amazing what a little programming can do. I wrote a Perl CGI that runs when the visitor clicks on browser print! That sneaky CGI converts all my DIVS to tables temporarily on the fly, then prints my pages perfectly. End of story? Not quite.

It's great that I found a way to take over browser print, and I'll tell you in a minute how I did it. But—arghhh—Netscape doesn't support my fine workaround.

So in addition to taking over the print browser (which is simply ignored by Netscape), I also coded a print button on each page. When the visitor clicks on the print button, the same CGI runs, but it doesn't take over browser print. Instead, the CGI calls print as it runs.

I coded the CGI for the print button so it puts up a print preview of sorts—actually it's a screen preview. Then it calls the print function. It works well enough, although the contrived print preview will look slightly different from what comes out of your printer. I guess I could fix that, but it doesn't seem important to me. What's more important is that the hardcopy page that my visitors' printers spit out as a result of clicking the print buttom will be identical to what they would get from browser print in all browsers except...Netscape.

In Netscape, as you already know, my CGI can't run from browser print. So in Netscape if you try to print my page using browser print, my HTML code simply uses a different CSS file and suppresses the egregious right side DIV. The solution of using a different CSS file based on browser detection is discussed here browser-dependent alternate style sheets.

And now, the code you've been waiting for. How to take over the browser print?

<link rel="alternate" media="print" href="http://www.betterwaypress.com/cgi-bin/LMprint.cgi" />

The end result? In all cases except one, the visitor gets a proper printout. The one exception is Netscape browser print, which produces a good-looking page with the rightside DIV suppressed.

Not bad, huh? To see an example, view the Life Planners Newsletter. Try printing it with browser print and with the print button. Try it in Mozilla, and try it in non-Mozilla. If you'd like to see the code in my CGI, contact Linda Moran.

Did this page help you?

.

What Mike Moran says about this solution

I (Linda) say the above solution to printing problems is clearly a hack. Mike disagrees. He says it's elegant. Which is the right answer? Maybe he's only trying to butter me up for some intimate pillow talk later about content management systems as they pertain to blogs.

Find out more

Search Engine Marketing, Inc. book cover

Want to find out how else to make the most of your Web site? The book, Search Engine Marketing, Inc., by Mike Moran and Bill Hunt, is available on Amazon.com. There's something for everyone in this thorough, delightfully written text that covers every aspect of Search Marketing. I know from the inside—I personally ripped it to shreds a few times while it was in the making.

Click on the book cover for more information.

And just who is Linda Moran, and why does she call herself "Search Wifey?" Find out  who is Linda Moran?



Did this page help you?

If the free information on this page was helpful to you, might it help your visitors as well? Consider linking to this page. And do you have friends with Web sites who could benefit from a link to this page? We'll help you e-mail all the link information to yourself or anyone else of your choosing. Just click on the e-mail icon.


Direct all technical questions and comments about this site to webmaster

Last Modified: Saturday, 12-Jan-2008 21:49:27 PST Linda Moran

All rights reserved.

Copyright © 2005 Linda Moran

 Linda Moran    Home | Cognitive therapy | Disabilities | Intuitive Eating | Technical