PerliKnight has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I want to be able to fetch a web page, save it in a file, render it in a browser and then print the rendered version.

I can do the first 3 bits (LWP::Simple etc) but can't think how to get the rendered page to go straight to the printer. Trying to use Internet Explorer or Firefox.

Anyone got any ideas?

Many thanks,

David.

Replies are listed 'Best First'.
Re: Printing from a browser
by shmem (Chancellor) on May 21, 2007 at 11:41 UTC
    If you have JavaScript enabled in the browser, you could insert a
    <script type="text/javascript"> window.print(); </script>

    before or after the closing </body> tag, using i.e. HTML::Parser for this task, before sending it to the browser.

    Alternatively, change the opening <body> tag to

    <body OnLoad="window.print()">

    Whether the page is sent straight to the printer or a Printing Dialog is opened depends on the implementation of that JavaScript method in the respective browser. Doesn't work on MSIE 4, MSIE 5 on Mac.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Printing from a browser
by derby (Abbot) on May 21, 2007 at 11:51 UTC

    You could replace the 'render it in a browser' phase with something like html2ps or html2pdf.

    -derby
Re: Printing from a browser
by tilly (Archbishop) on May 21, 2007 at 13:29 UTC
    I would go with one of the first two solutions. But a third alternative is to use the Win32::OLE module to launch IE, load the page, and then print it. (It has been years since I've used the module, I don't know whether it will work with Firefox.)
      Thanks chaps (and chapesses).

      Printed those three out and will give them a go...

      Cheers,David

        Hi,

        Got

        $IE = Win32::OLE->new("InternetExplorer.Application") || die "Could not start Internet Explorer.Application\n";

        $IE->{visible} = 1;

        $URL = "http://news.bbc.co.uk";

        $IE->Navigate($URL);

        all of which works just fine. But... what's the one to send the page directly to the printer...? Anyone know?

        Thanks, David.