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 and some changes to make it work with windows 2000 and XP.

SYNOPSIS

 use Printer;

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

or for windows network printers $prn = new Printer('MSWin32' => '\\server\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);

Special options for print_command under Windows

 $prn->print_command('MSWin32' => {'type' => 'command',
                                  'command' => MS_ie});

Under Windows, the print_command method accepts the options MS_ie, MS_word and MS_excel to print data using Internet Explorer, Word and Excel.

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().

Printer ports and network printers under windows

To use a printer which is directly attached to your network, you need to share that printer from a windows host, otherwise you will just get a file which contains the print job in the perl script's directory.

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 just-about works).

List available printers

 %printers = list_printers().

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

  • %hash{name} - printer names

  • %hash{port} - 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

(Or why this will work better on Linux/UNIX than windows)

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 on 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, though 0.98 adds the ability to print data using Microsoft's Internet Explorer, Word and Excel via OLE.

BUGS

  • list_jobs needs writing for win32

AUTHORS

Stephen Patterson (steve@patter.mine.nu)

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

TODO

  • Make list_jobs work on windows.

  • Port to MacOS. Any volunteers?

Changelog

0.98

  • use_default adjusted for Windows XP to pick the first available printer.

  • Added windows subroutines for MS IE, Word and Excel.

  • Basic windows printing (ASCII text) migrated from a collection of crufty code which was depending on backwards compatibility features removed in windows 2000/XP to use Edgars Binans Win32::Printer module. You can call $printer->print_orig() to use the pre 0.98 printing routines should you need to.

0.97a, 0.97b, 0.97c, 0.97d

  • Sequential fixes to work with 'use strict' and '-w'

  • list_printers and use_default updated to look at the right parts of the registry on windows 2000.

  • Printing an array actually works now.

0.97

  • Bug which produced: Can't modify constant item in scalar assignment at line 224 fixed.

  • Unix and Win32 specific code split from the general routines.

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