The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Printer.pm - a low-level, platform independent printing interface (curently Linux and MS Win32. other UNIXES should also work.)

This version includes working support for Windows 95.

SYNOPSIS

 use Printer;

 $prn = new Printer('linux' => 'lp', 
                    'MSWin32' => 'LPT1', 
                    $OSNAME => 'Printer');

 $prn->print_command('linux' => {'type' => 'pipe',
                                'command' => 'lpr -P lp'},
                    'MSWin32' => {'type' => 'command',
                                 'command' => 'gswin32c -sDEVICE=mswinpr2 
                                 -dNOPAUSE -dBATCH $spoolfile'}
                    );

 %available_printers = $prn->list_printers;

 $prn->use_default;

 $prn->print($data);

DESCRIPTION

A low-level cross-platform interface to system printers.

This module is intended to allow perl programs to use and query printers on any computer system capable of running perl. The intention of this module is for a program to be able to use the printer without having to know which operating system is being used.

PLATFORMS

This code has been tested on Linux, DEC-OSF, Solaris, HP/UX windows 95 and windows NT4.

UNIX printing works using the Linux routines. This assumes that your print command is lpr, your queue list command is lpq and that your printer names can be found by grepping /etc/printcap. If it's anything different, email me with the value of $OSNAME or $^O and the corrections.

USAGE

Open a printer handle

 $printer = new Printer('osname' => 'printer port');
 $printer = new Printer('MSWin32' => 'LPT1', 
                        'Linux' => 'lp');

This method takes a hash to set the printer name to be used for each operating system that this module is to be used on (the hash keys are the values of $^O or $OSNAME for each platform) and returns a printer handle which is used by the other methods.

If you intend to use the use_default() or print_command() methods, you don't need to supply any parameters to new().

This method dies with an error message on unsupported platforms.

Define a printer command to use

 $prn->print_command('linux' => {'type' => 'pipe',
                     'command' => 'lpr -P lp'},
                     'MSWin32' => {'type' => 'file',
                                  'command' => 'gswin32c -sDEVICE=mswinpr2 
                                  -dNOPAUSE -dBATCH $spoolfile'}
                    );

This method allows you to specify your own print command to use. It takes 2 parameters for each operating system:

type

  • pipe - the specified print command accepts data on a pipe.

  • file - the specified print command works on a file. The Printer module replaces $spoolfile with a temporary filename which contains the data to be printed

command

This specifies the command to be used.

Select the default printer

 $printer->use_default;

This should not be used in combination with print_command.

Linux

The default printer is read from the environment variables $PRINTER, $LPDEST, $NPRINTER, $NGPRINTER in that order, or is set to the value of lpstat -d or is set to "lp" if it cannot be otherwise determined. You will be warned if this happens.

Win32

 THe default printer is read from the registry (trust me, this works).

List available printers

 %printers = list_printers().
  

This returns a hash of arrays listing all available printers. The hash keys are:

  • %hash{names} - printer names

  • %hash{ports} - printer ports

Print

 $printer->print($data);

 $printer->print(@pling);

Print a scalar value or an array onto the print server through a pipe (like Linux)

List queued jobs

 @jobs = $printer->list_jobs();

This returns an array of hashes where each element in the array contains a hash containing information on a single print job. The hash keys are: Rank, Owner, Job, Files, Size.

This code shows how you can access each element of the hash for all of the print jobs.


 @queue = list_jobs();
 foreach $ref (@queue) {
    foreach $field (qw/Rank Owner Job Files Size/) {
        print $field, " = ", $$ref{$field}, " ";
    }
 print "\n";
 }

Windows

The array returned is empty (for compatibility).

NOTES ON THE WINDOWS AND LINUX/UNIX PRINT SPOOLERS

The Linux and UNIX printing systems are based around postscript and come with a set of ancillary programs to convert anything which should be printable into postscript. The postscript representation of your print job is then converted into a set of printing commands which your printer can recognise.

Windows printing is based applications wanting to print using windows API calls (hideous) to create a GDI file which is then converted by the print spooler into printer specific commands and sent to the physical printer.

What this means to a user of the Printer module is that on Linux/UNIX the data passed to the print method can be anything which should be printable, i.e. groff/troff, PostScript, plain text, TeX dvi, but on windows the only data which can be handled by the printing system is plain text, GDI commands or flies written in your printer's interface language.

BUGS

  • list_jobs needs writing for win32

AUTHORS

Stephen Patterson (steve@lexx.uklinux.net)

David W Phillips (ss0300@dfa.state.ny.us)

TODO

  • Make list_jobs work on windows.

  • Port to MacOS. Any volunteers?

Changelog

0.96

  • Some bugs which generated warnings when using -w fixed thanks to a patch from David Wheeler

0.95c

  • Author's email address changed

0.95b

  • Bug when using print_command with the command option on linux fixed.

0.95a

  • sundry bug fixes

0.95

  • added support for user defined print commands.

0.94c

  • removed unwanted dependency on Win32::AdminMisc

  • added support of user-defined print command

0.94b

  • added documentation of the array input capabilities of the print() method

  • windows installation fixed (for a while)

0.94a

  • glaring typos fixed to pass a syntax check (perl -c)

0.94

  • uses the first instance of the lp* commands from the user's path

  • more typos fixed

  • list_jobs almost entirely rewritten for linux like systems.

0.93b

  • Checked and modified for dec_osf, solaris and HP/UX thanks to data from David Phillips.

  • Several quoting errors fixed.

0.93a

  • list_jobs returns an array of hashes

  • list_printers exported into main namespace so it can be called without an object handle (which it doesn't need anyway).

0.93

  • Printing on windows 95 now uses a unique spoolfile which will not overwrite an existing file.

  • Documentation spruced up to look like a normal linux manpage.

0.92

  • Carp based error tracking introduced.

0.91

  • Use the linux routines for all UNIXES.

0.9

Initial release version

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 690:

You forgot a '=back' before '=head2'

Around line 756:

You forgot a '=back' before '=head2'