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

According to the M$ documentation and other sites I have found the following code should cause test.html to be sent to the printer. I get no errors but nothing prints either (which is technically an error, I guess). Any help?
use strict; use Win32::API; print printFile("G:\\dev\\win32\\test.html"); ################ sub printFile{ #call shellExecute with the print verb to print a file # http://msdn.microsoft.com/archive/default.asp?url=/archive/en +-us/dnarvb4/html/msdn_shelexec.asp my $file=shift || return -1; if(!-e $file){return "$file does not exist";} print "printing $file\n"; my $ShellExecute = new Win32::API("shell32", "ShellExecuteA", ['N' +,'P', 'P', 'P', 'P', 'I'], 'N') || return 0; my $rtn=$ShellExecute->Call( 0, #hwnd "Print", #open or print. 'open' opens file specified (exe or d +oc file). 'print' prints doc file $file, #cmd to execute or file to open "", #Parameters to be passed to application, should be null + if document "", #default directory 1 #Show state: 0=hide(SW_HIDE),1=normal(SW_SHOWNORMAL),6=mi +nimized(SW_MINIMIZE),3=maximized(SW_MAXIMIZE) ); if($rtn<33){return $rtn;} else{return $rtn;} }

-------------------------------
Need a good Perl friendly Host Provider?
http://www.dreamhost.com

Replies are listed 'Best First'.
Re: printing an html file to a printer on windows
by Jenda (Abbot) on May 23, 2007 at 15:13 UTC

    Try it like this:

    use strict; use Win32::FileOp qw(ShellExecute); ShellExecute Print => "G:\\dev\\win32\\test.html";

    If this doesn't work, try to run regedit.exe and see whether there actually is a Print action defined for extension .html. (See HKEY_CLASSES_ROOT\.html, look at the default value, then look into HKEY_CLASSES_ROOT\<that value>\shell and see if there is a Print subkey.)

    Also, not sure it's the problem, but I use new Win32::API("shell32", "ShellExecute", ['N','P','P','P','P','N'], 'I'), that is ShellExecute instead of ShellExecuteA and with a bit different options.

Re: printing an html file to a printer on windows
by BrowserUk (Patriarch) on May 23, 2007 at 15:03 UTC

    Update: Albannach informs me that the example code does render html to the printer via the default browser on his system. (Like for the OP, it does nothing on my system.)

    What are you hoping for here? The above code is basically equivalent to print file.html from the command line. If it can be made to work--the article you cited is over a decade old, referring to VB4 and w95!--then all it will do is print the html as a text file to the current default printer. That is more easily achieved using system qq[cmd.exe /c print $file]; If you are hoping to print the rendered html, then you'll need to find a way to automate a webbrowser.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.