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

I am trying to incorporate print capabilities to an app I have written and have trie Net::Printer, Win32::Printer::Enum and one other and none seem to work. I am not sure if I am using the syntax right or am using the wrong module all together.

I am writing this as for the Win32 OS platform and was under the impression that Win32::Printer::Enum was the correct one. Also Printer is non-platform specific so I tried that also. But as I said neither have worked.

Has anyone had any success using either of these or have any others they have successfully printed a file with? If anyone has I could really use a snippet to show me the correct syntax.

Here was my last attempt at using the Print module.

my $printer = new Printer('linux' => 'lp', 'MSWin32' => 'LPT1'); $printer->print_command('linux' => {'type' => 'pipe', 'command' => 'lpr -P lp'}, 'MSWin32' => {'type' => 'command', 'command' => 'gswin32c -sDEVICE=m +swinpr2 -dNOPAUSE -dBATCH $spoolfile'} ); sub print { my $pw = MainWindow->new(-title=>"Printer Options"); my $frame = $pw->Frame->pack(-side => 'top', -fill => 'x'); $frame->Button(-text => "Show Printers", -background => 'navy blue' +, -foreground => 'white', -command => \&show_printers)-> pack(-side => 'right'); $frame->Button(-text => "Print File", -background => 'navy blue', - +foreground => 'white', -command => \&print_file)-> pack(-side => 'left'); sub print_file { $status->delete("1.0", "end"); $status->insert("end", "Printing Document...."); my $target = $t->get("1.0", "end"); $printer->use_default; $printer->print($target); $status->insert("end", "done\n"); } sub show_printers { my %available_printers = $printer->list_printers(); foreach my $key (%available_printers) { foreach my $field (qw/name port/) { print $field, "=", $$key{$field}, " "; } } } sub list_jobs { my @queue = list_jobs(); foreach my $ref (@queue) { foreach my $field (qw/Rank Owner Job Files Size/) { print $field, " = ", $$ref{$field}, " "; } print "\n"; } } }
I have not addes a third button for the ques jobs yet but was trying to work on the backend first. I of course get an error here saying the barewaord names I have for hash fields cannot be used while strict subs is in use. However I still do not like the way this is laid out and think there has got to be a better way to simply print a file to a printer.

Replies are listed 'Best First'.
Re: Need printer help.
by tachyon (Chancellor) on Dec 13, 2003 at 12:25 UTC

    You may find Printing from Perl in Windows of use. It offers a range of approaches that work. I have an example there of how to print out of Word which offers the advantages of Font selection and color to name a few.... There are also a number of direct dll/exe calls for html amongst other userful snippets.

    cheers

    tachyon

Re: Need printer help.
by tachyon (Chancellor) on Dec 13, 2003 at 05:23 UTC

    On Win32 I just do this.....

    `print /D:LPT1 C:\\file.txt`;

    The only real caveats of this minimal approach are that the font will be Courier and it will hang indefinitely if the printer does not exist. It does work though.

    cheers

    tachyon