http://qs1969.pair.com?node_id=404740

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

Hello Fellow Monks,

Looking for some guidance. I am needing to write a perl script to run on windows 2000 server, that will print a html file to several printers installed on that server. If you could point me in the direction of some good reading material or have some code snippets I could browse I would appreciate it.

Thanks in advance!

Replies are listed 'Best First'.
Re: Win32 Printing
by BrowserUk (Patriarch) on Nov 02, 2004 at 22:49 UTC
    ...that will print a html file to several printers installed on that server....

    The solution will depend upon what you mean by that?

    If you mean print the entire contents of a file that contains html, tags and all, as ascii--just as you would see it on the screen if you did type file.html, then the easiest way is to use open directly to the printers device id (lpt1: or com2: etc) or it's full share name (\\server\\printername). and just print to the filehandle in the normal way.

    But if you want the html, minus the tags, and formatted as you would see it in a browsers, then you will need to use a browser's print facility to take care of the formatting. You can (with a lot of reading) do this using Win32::OLE or possibly using Win32::GuiTest. Either is not simple, but if you use super search you may find some guidance.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      That does sound challenging. I have some experience with win32::guitest and it would definitely be a challenge. Would changing them to a pdf make things easier?

        Either way, you are faced with the same problem if you want to format and then print. You either:

        1. Use an existing application that knows how to format the data format the file is in, and drive that from Perl.

          Which is the Win32::GUI or Win32::OLE method.

        2. Try to render the formatting yourself.

          This is by far the harder task. Even with years, and hundreds or even thousands of man hours behind them, both browsers and Adobe's reader still don't always make a great job of printing their formats. Doing it your self is extremely hard in the case of html, and both extremely hard and possibly illegal in the case of PDF.

        If your very unfussy about the way the output is rendered you might get away with using something like the linx text browser to render the html or ghostscript for the PDF, but neither is very satisfactory.

        I'm not sure, but I seem to remember that at least one of the popular browsers had a command line switch that would cause it to render the url supplied, print to the default printer and then exit--but I don't recall where I got that notion from?


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      i want to print a text file from to the default windows printer . I don't know wich printer that wil be on the pc's who wil run the software it could be a local printer or a network printer

        Maybe the simplest option is system "print $file";.

        The downside is that if they don't have printer configured, or if it is turned off, or it is set to a network printer and they aren't connected to it, then the user will be prompted by popup.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Win32 Printing
by ldln (Pilgrim) on Nov 02, 2004 at 21:07 UTC
    I haven't tried it, but looks very interesting, Win32::Printer - Perl extension for Win32 printing
      I was reading up on this. Thanks for the tip.
Re: Win32 Printing
by ldln (Pilgrim) on Nov 03, 2004 at 23:06 UTC
    If ya wanna go up the road with Win32::GuiTest, then this should be something for ya that makes it very simple. We open up the Internet Explorer print dialog box and now we just have to focus this and send it "ENTER" to start printing.
    use strict; use LWP::Simple; use Win32::GuiTest qw( WaitWindowLike SetForegroundWindow SendKeys + ); my $url = q(http://www.cnn.com); my $fn = $ENV{TEMP}."\\test.html"; getstore ($url, $fn); my $prg = qq(start rundll32 mshtml.dll,PrintHTML "$fn"); system $prg; $_ = WaitWindowLike( 0, qr/Apply/i, undef , undef, undef, 10000) +; SetForegroundWindow($_); SendKeys ("{ENTER}"); print "Printed ",-s $fn, " bytes from $url";

    Switches to mshtml.dll
    %1 - File to print
    %2 - Printer-name
    %3 - Printer driver
    %4 - Printer port

      Which version of GUITest do you have? Mine doesn't export a 'WaitWindowLike' function.


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "Think for yourself!" - Abigail
      "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
        You need the one from CPAN, Win32::GuiTest. It comes with .ppd, which installed fine with perl 5.8.3-809.