| 1 | ############################################################################ | ||
| 2 | ############################################################################ | ||
| 3 | ## ## | ||
| 4 | ## Copyright 2004 Stephen Patterson (steve@patter.mine.nu) ## | ||
| 5 | ## ## | ||
| 6 | ## A cross platform perl printer interface ## | ||
| 7 | ## This code is made available under the perl artistic licence ## | ||
| 8 | ## ## | ||
| 9 | ## Documentation is at the end (search for __END__) or process with ## | ||
| 10 | ## pod2man/pod2text/pod2html ## | ||
| 11 | ## ## | ||
| 12 | ## Debugging and code contributions from: ## | ||
| 13 | ## David W Phillips (ss0300@dfa.state.ny.us) ## | ||
| 14 | ## Graham K Jenkins (Graham.K.Jenkins@team.telstra.com) ## | ||
| 15 | ## ## | ||
| 16 | ############################################################################ | ||
| 17 | ############################################################################ | ||
| 18 | |||
| 19 | # generic routines for Printer | ||
| 20 | |||
| 21 | package Printer; | ||
| 22 | $VERSION = '0.98'; | ||
| 23 | |||
| 24 | use English; | ||
| 25 | use strict; | ||
| 26 | no strict 'refs'; | ||
| 27 | use Carp qw(croak cluck); | ||
| 28 | use Env qw(PATH); | ||
| 29 | use vars qw(%Env @ISA); | ||
| 30 | use DynaLoader; | ||
| 31 | |||
| 32 | ############################################################################# | ||
| 33 | sub new { | ||
| 34 | # constructor | ||
| 35 | my $type = shift; | ||
| 36 | my %params = @_; | ||
| 37 | my $self = {}; | ||
| 38 | |||
| 39 | # frob the system value to use linux routines below for the | ||
| 40 | # various unices | ||
| 41 | # see perldoc perlport for system names | ||
| 42 | if (grep { /^$OSNAME$/ } qw(aix bsdos dgux dynixptx | ||
| 43 | freebsd hpux irix rhapsody | ||
| 44 | machten next openbsd dec_osf | ||
| 45 | svr4 sco_sv unicos unicosmk | ||
| 46 | solaris sunos netbsd linux) ) { | ||
| 47 | $self->{system} = 'linux'; | ||
| 48 | |||
| 49 | # search PATH for lpr, lpq, lp, lpstat (use first found) | ||
| 50 | my %progs; | ||
| 51 | my @PathDirs = grep {/^[^\.]/} (split /:/,$PATH); | ||
| 52 | foreach my $dir ( @PathDirs ) { | ||
| 53 | foreach my $prg ( qw(lpr lpq lp lpstat) ) { | ||
| 54 | next if exists $progs{$prg}; | ||
| 55 | my $loc = "$dir/$prg"; | ||
| 56 | -f $loc && -x $loc && ($progs{$prg}=$loc); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | $self->{'program'} = \%progs; | ||
| 60 | |||
| 61 | # load the unix printer module | ||
| 62 | require Printer::Unix; | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | # load system specific modules for win32 | ||
| 67 | if ($OSNAME eq "MSWin32") { | ||
| 68 | # win32 specific modules | ||
| 69 | #bootstrap Printer; # for XS only | ||
| 70 | require Win32::Registry; # to list printers | ||
| 71 | require Win32; | ||
| 72 | require Win32::API; | ||
| 73 | require Win32::Printer; # load Edgars Binans libs from wasx.net | ||
| 74 | require Printer::Win32; | ||
| 75 | } | ||
| 76 | |||
| 77 | # for windows, add the windows version. | ||
| 78 | # from http://aspn.activestate.com/ASPN/Reference/Products/ActivePerl/lib/Win32.html | ||
| 79 | if ($OSNAME eq "MSWin32") { | ||
| 80 | $self->{system} = 'MSWin32'; | ||
| 81 | $self->{winver} = Win32::GetOSName(); | ||
| 82 | } | ||
| 83 | |||
| 84 | $self->{printer} = \%params; | ||
| 85 | |||
| 86 | # die with an informative message if using an unsupported platform. | ||
| 87 | unless ($self->{system} eq 'linux' or $self->{system} eq 'MSWin32') { | ||
| 88 | Carp::croak "Platform $OSNAME is not yet supported. Share and enjoy."; | ||
| 89 | return undef; | ||
| 90 | } | ||
| 91 | |||
| 92 | # set orientation | ||
| 93 | $self->{orientation} = 'portrait'; | ||
| 94 | if (defined $params{orientation} ) { | ||
| 95 | if ($params{orientation} eq 'landscape') { | ||
| 96 | $self->{orientation} = 'landscape' | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | return bless $self, $type; | ||
| 101 | |||
| 102 | } | ||
| 103 | ############################################################################ | ||
| 104 | sub print_command { | ||
| 105 | # allow users to specify a print command to use for a system | ||
| 106 | my $self = shift(); | ||
| 107 | my %systems = @_; | ||
| 108 | my %final_data; | ||
| 109 | |||
| 110 | foreach my $system (keys %systems) { | ||
| 111 | foreach my $opt (keys %{ $systems{$system} }) { | ||
| 112 | my %cmd_data = %{ $systems{$system} }; | ||
| 113 | $final_data{$system} = \%cmd_data; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | $self->{print_command} = \%final_data; | ||
| 118 | } | ||
| 119 | ############################################################################ | ||
| 120 | sub get_unique_spool { | ||
| 121 | # Get a filename to use as the | ||
| 122 | # spoolfile without overwriting another file | ||
| 123 | my ($i, $spoolfile); | ||
| 124 | $i = 0; | ||
| 125 | my $sys = shift; | ||
| 126 | |||
| 127 | # linux - no TEMP env var | ||
| 128 | if ( (! $ENV{TEMP}) && $sys eq 'linux') { | ||
| 129 | $ENV{TEMP} = '/tmp'; | ||
| 130 | } | ||
| 131 | # end linux | ||
| 132 | |||
| 133 | while (-e "$ENV{TEMP}/printer-$PID.$i") { | ||
| 134 | ++$i; | ||
| 135 | } | ||
| 136 | if ($OSNAME eq 'MSWin32') { | ||
| 137 | $spoolfile = $ENV{TEMP} . '\printer-' . $PID . $i; | ||
| 138 | } else { | ||
| 139 | $spoolfile = $ENV{TEMP} . '/printer-' . $PID . $i; | ||
| 140 | } | ||
| 141 | return $spoolfile; | ||
| 142 | } | ||
| 143 | ############################################################################ | ||
| 144 | __END__ |