Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

formatting web pages for a printer

by markjugg (Curate)
on Aug 27, 2002 at 15:24 UTC ( #193191=perlquestion: print w/replies, xml ) Need Help??
markjugg has asked for the wisdom of the Perl Monks concerning the following question:


Several times recently I've had requests to produce Perl-driven web pages that print out nicely on a single page. The issue of course is that this isn't really a Perl issue-- it depends on their browser and OS. I've thought of a few solutions to this, none of which I'm convinced are the best available:

  • Produce a PDF on the server. My understanding is this involves a commercial module and is non-trivial to take a working HTML page and reproduce the same thing in PDF output.
  • Produce a PDF on the client. I believe this involves purchasing Acrobat (about $220) for eachc client that wants this feature, and then there is still a wait time for Acrobat to grind away to convert the HTML to a PDF, mostly to get a "shrink to fit" printing option.
  • Print from a word processor. Another solution is to save the HTML file to the desktop, open it in Microsoft Word (or another HTML-reading word processor) and then use the word processors printing features. This works, but it's slow, at least with Word 2000 on Windows 98 and a ~500 Mhz processor, it took a bit of grinding just to open and print a single page with an HTML table on it.
Does anyone know of solutions in this area that are easy to develop and easy to use? Thanks,


Replies are listed 'Best First'.
Re: formatting web pages for a printer
by Hero Zzyzzx (Curate) on Aug 27, 2002 at 15:31 UTC

    Look at HTMLdoc as an EXCELLENT server-side html to pdf converter. It's open-source, very fast, free, and generates very, very good output. I can't say enough good things about it. I'd be happy to send you links to sample output, if you wish. (pm me).

    If you've taken the wise step of separating presentation from content and logic using modules like HTML:Template, then you can also create a "printable" template that nixes all your graphics and wraps the content in 650 pixel width table. This table width seems to avoid lines being cut off on most browsers. This can be a less desirable option, depending on the nature of the content you're serving.

    Update: Spelling fix and added second paragraph.

    -Any sufficiently advanced technology is
    indistinguishable from doubletalk.

      HTMLdoc does look like an excellent solution for a lot of cases. It looks it's been ported to FreeBSD as well. However, it failed my "quick probe" test with the demo provided, by cutting off a wide table instead of shrinking it to web page, or at least creating a second page in the PDF. Maybe that particular issue could be worked out by stricter HTML formatting, or some HTMLdoc configuration option...

      It's not perfect, but it appears to be better than anything I've found. :) Thanks.


        Hmm. I haven't run into that issue yet mainly because I think fixed tables suck and should be avoided whenever possible. I assume that this wide table was a fixed width table?

        One other tip- I create an HTMLdoc specific template that I then use as the "base" page for pdf conversion- this allows you to ixnay the frivolous (sp?) graphics, leaving only the good stuff. This is a trivial operation if you have everything in a templatting system.

        HTMLdoc has an enormous amount of configuration options, I'd be surprised if you weren't able to tweak it to fix your problem on a system-wide basis.

        -Any sufficiently advanced technology is
        indistinguishable from doubletalk.

        If it's a pixel-fixed-width table you can fix this by passing a --browserwidth parameter to htmldoc that's greater than the total width of the table.

Re: formatting web pages for a printer
by Maclir (Curate) on Aug 27, 2002 at 15:51 UTC
    Style sheets and media types are your friend. Make use of the PRINT media type such that the format and styles used for the web pages are printer friendly when the person goes to print the page.

    Big advantage - you only have to set this up once for all pages on the site.

    What's that? Your browser does not support these standards? Then upgrade the browser to one that does support CSS-2.

      Here Here!

      Along that line, you could also use something like AxKit on the backend. It's based on the same premise, xml/xsl/xslt transformations; even mix that backend with client side css on the front end. This is a win win if you want to repurpose output for more than just html for browsers from your site. It even has an AxPoint plugin, so you could translate the same xml doc into a .pdf output by doing something like ?media=pdf with little extra work.

      Regardless how what you choose, anything is better and two seperate pages just for two seperate looks on the same content.

      Yes, I'm a HUGE fan of AxKit.

        Hmm. I wasn't implying that markjugg create two different pages, but two different HTML::Templates, one for browser output and one tweaked especially for HTMLdoc.

        That said, Axkit is on my short list of systems to spend more time figuring out, along with Bricolage.

        -Any sufficiently advanced technology is
        indistinguishable from doubletalk.

Re: formatting web pages for a printer
by Django (Pilgrim) on Aug 27, 2002 at 17:34 UTC

    My advice is to generate a very simple "HTML light" version: no nested or fixed width tables, no CSS, no JavaScript, no backgrounds or colors of any kind, and perhaps not even images. Also strip it to the basic content, but add information that otherwise might be lost (e.g. current URL). That's the best you can do to work with any browser, printer, OS etc.

    Working with XSLT most of my time (which is rather horrible, because you usually end up duplicating code, typing tons of error-prone lines and creating maintenance chaos), I unfortunately can't tell you the way to do it in perl, but surely some fellow monks in here can.

Re: formatting web pages for a printer
by Steve_p (Priest) on Aug 27, 2002 at 23:27 UTC
    The other alternative is to just get a Mac. PDF is a native format in Mac OS X. Just print preview whatever you want to print and you can save to PDF. Or you could just print it I guess ;)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://193191]
Approved by Hero Zzyzzx
and the questions are moot...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2017-03-24 07:34 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (297 votes). Check out past polls.