| 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 | ############################################################################ | ||
| 13 | ############################################################################ | ||
| 14 | |||
| 15 | # Win32 Routines | ||
| 16 | |||
| 17 | ############################################################################ | ||
| 18 | sub list_printers { | ||
| 19 | # list available printers | ||
| 20 | my $self = shift(); | ||
| 21 | my %printers; | ||
| 22 | |||
| 23 | # look at registry to get printer names for local machine | ||
| 24 | my $Register = 'SYSTEM\CurrentControlSet\Control\Print\Printers'; | ||
| 25 | my ($hkey, @key_list, @names, @ports); | ||
| 26 | my $HKEY_LOCAL_MACHINE = $main::HKEY_LOCAL_MACHINE; | ||
| 27 | $HKEY_LOCAL_MACHINE->Open($Register, $hkey) or | ||
| 28 | Carp::croak "Can't open registry key HKEY_LOCAL_MACHINE\\$Register: $!"; | ||
| 29 | $hkey->GetKeys(\@key_list); | ||
| 30 | foreach my $key (@key_list) { | ||
| 31 | my $path = $Register . '\\' . $key; | ||
| 32 | my ($pkey, %values, $printers); | ||
| 33 | $HKEY_LOCAL_MACHINE->Open($path, $pkey) or | ||
| 34 | Carp::croak "Can't open registry key HKEY_LOCAL_MACHINE\\$path: $!"; | ||
| 35 | $pkey->GetValues(\%values); | ||
| 36 | push @ports, $values{Port}[2]; | ||
| 37 | push @names, $values{Name}[2]; | ||
| 38 | } | ||
| 39 | $printers{name} = [ @names ]; | ||
| 40 | $printers{port} = [ @ports ]; | ||
| 41 | return %printers; | ||
| 42 | } | ||
| 43 | ###################################################################### | ||
| 44 | sub use_default { | ||
| 45 | # select the default printer | ||
| 46 | my $self = shift; | ||
| 47 | my ($hkey, %values); | ||
| 48 | |||
| 49 | # default name is the human readable printer name (not port) | ||
| 50 | # look in the registry to find it | ||
| 51 | if ($self->{winver} eq ('Win95' or 'Win98' or 'WinNT4')) { | ||
| 52 | # the old routines, win95/nt4 tested | ||
| 53 | my $register = 'Config\0001\SYSTEM\CurrentControlSet\Control\Print\Printers'; | ||
| 54 | my $HKLM = $main::HKEY_LOCAL_MACHINE; | ||
| 55 | $HKLM->Open($register, $hkey) or | ||
| 56 | Carp::croak "Can't open registry key " . $register | ||
| 57 | . "in use_default(): $EXTENDED_OS_ERROR\n"; | ||
| 58 | $hkey->GetValues(\%values); | ||
| 59 | my $default = $values{Default}[2]; | ||
| 60 | # $default holds the long printer name, get the port | ||
| 61 | $register = 'SYSTEM\CurrentControlSet\Control\Print\Printers\\'; | ||
| 62 | my $path = $register . $default; | ||
| 63 | $HKLM->Open($path, $HKEY) or | ||
| 64 | Carp::croak "Can't open registry key $path in use_default() " | ||
| 65 | . $EXTENDED_OS_ERROR; | ||
| 66 | $hkey->GetValues(\%values); | ||
| 67 | $self->{'printer'}{$OSNAME} = $values{Port}[2]; | ||
| 68 | } elsif ($self->{winver} =~ /2000/) { | ||
| 69 | # pull it from a different registry path | ||
| 70 | my $register = 'Software\Microsoft\Windows NT\CurrentVersion\Windows'; | ||
| 71 | my $HKCU = $main::HKEY_CURRENT_USER; | ||
| 72 | $HKCU->Open($register, $hkey) or | ||
| 73 | Carp::croak "Can't open registry key " . $register | ||
| 74 | . "in use_default(): $EXTENDED_OS_ERROR\n"; | ||
| 75 | $hkey->GetValues(\%values); | ||
| 76 | my $default = $values{Device}[2]; | ||
| 77 | # $default holds the long printer name, get the port | ||
| 78 | $register = 'SYSTEM\CurrentControlSet\Control\Print\Printers\\'; | ||
| 79 | my $path = $register . $default; | ||
| 80 | my $HKLM = $main::HKEY_LOCAL_MACHINE; | ||
| 81 | $HKLM->Open($path, $HKEY) or | ||
| 82 | Carp::croak "Can't open registry key $path in use_default() " | ||
| 83 | . $EXTENDED_OS_ERROR; | ||
| 84 | $hkey->GetValues(\%values); | ||
| 85 | $self->{'printer'}{$OSNAME} = $values{Port}[2]; | ||
| 86 | } elsif ($self->{winver} =~ /XP/) { | ||
| 87 | # bugger. nothing appropriate in registry for windows XP | ||
| 88 | # pick 1st available printer | ||
| 89 | my %printers = $self->list_printers; | ||
| 90 | $self->{printer}{$OSNAME} = $printers{port}[0]; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | ###################################################################### | ||
| 94 | sub list_jobs { | ||
| 95 | # list the current print queue | ||
| 96 | my $self = shift; | ||
| 97 | # return an empty queue (for compatibility) | ||
| 98 | # Carp::croak 'list_jobs hasn\'t yet been written for windows. Share and enjoy'; | ||
| 99 | my ($pHandle, @jobs, $start); | ||
| 100 | $start = 0; | ||
| 101 | |||
| 102 | $OpenPrinter = new Win32::API('Winspool.drv', | ||
| 103 | 'OpenPrinter', | ||
| 104 | [P, P, P], | ||
| 105 | I); | ||
| 106 | $ClosePrinter = new Win32::API('Winspool.drv', | ||
| 107 | 'ClosePrinter', | ||
| 108 | [P], | ||
| 109 | I) || die $!; | ||
| 110 | $GetLastError = new Win32::API('kernel32.dll', | ||
| 111 | 'GetLastError', | ||
| 112 | I) || die $!; | ||
| 113 | |||
| 114 | # human readable printer name | ||
| 115 | my $pName = $self->{'printer'}{'MSWin32'}; | ||
| 116 | |||
| 117 | $pHandle = " " x 128; # init a char buffer | ||
| 118 | $OpenPrinter->Call($pName, $pHandle, NULL) or Carp::croak | ||
| 119 | "couldn't open printer handle for list_jobs $GetLastError->Call"; | ||
| 120 | $pHandle =~ s/\0.*$//; | ||
| 121 | |||
| 122 | GetJobs($pHandle, @jobs, $start) || die "Couldn't call GetJobs via xs\n"; | ||
| 123 | print @jobs; | ||
| 124 | } | ||
| 125 | ###################################################################### | ||
| 126 | |||
| 127 | ###################################################################### | ||
| 128 | ## ## | ||
| 129 | ## Extra windows drivers for specific data types ## | ||
| 130 | ## ## | ||
| 131 | ###################################################################### | ||
| 132 | |||
| 133 | ###################################################################### | ||
| 134 | sub MS_word { | ||
| 135 | # understood by MS Word | ||
| 136 | require Win32::OLE; | ||
| 137 | # require Win32::OLE::Const qq(Microsoft Word); | ||
| 138 | my ($self, $spoolfile) = @_; | ||
| 139 | my $Word = Win32::OLE->new('Word.Application', 'Quit'); | ||
| 140 | $Word->ActivePrinter($self->{'printer'}{$OSNAME}); | ||
| 141 | $Word->Documents->Open($spoolfile) or Carp::croak | ||
| 142 | ("unable to open document", Win32::OLE->LastError()); | ||
| 143 | $Word->ActiveDocument->PrintOut({ | ||
| 144 | Background => 0, | ||
| 145 | Append => 0, | ||
| 146 | Range => wdPrintAllDocument, | ||
| 147 | Item => wdPrintDocumentContent, | ||
| 148 | Copies => 1, | ||
| 149 | PageType => wdPrintAllPages}); | ||
| 150 | unlink $spoolfile; | ||
| 151 | } | ||
| 152 | ###################################################################### | ||
| 153 | sub MS_excel { | ||
| 154 | # understood by MS Excel | ||
| 155 | require Win32::OLE; | ||
| 156 | # require Win32::OLE::Const 'Microsoft Excel'; | ||
| 157 | my ($self, $xlfile) = @_; | ||
| 158 | my $xl_app = Win32::OLE->new('Excel.Application', 'Quit') or Carp::croak | ||
| 159 | ("Cannot start excel", Win32::OLE->LastError); | ||
| 160 | my $workbook = $xl_app->Workbooks->Open($xlfile) or Carp::croak | ||
| 161 | ("Can't open file", Win32::OLE->LastError); | ||
| 162 | my $worksheet = $workbook->Worksheets(1); | ||
| 163 | $worksheet->PrintOut; | ||
| 164 | $xl_app->Quit; | ||
| 165 | } | ||
| 166 | ###################################################################### | ||
| 167 | sub MS_ie { | ||
| 168 | # internet explorer | ||
| 169 | require Win32::OLE; | ||
| 170 | my ($self, $spoolfile) = shift(); | ||
| 171 | my $IE = Win32::OLE->new('InternetExplorer.Application', 'Quit') or | ||
| 172 | Carp::croak("Cannot start Internet Explorer", Win32::OLE->LastError); | ||
| 173 | $IE->navigate($spoolfile); | ||
| 174 | # or Window.Print | ||
| 175 | # IE can't set printer or orientation | ||
| 176 | sleep 1 while ($explorer->{ReadyState} < 4); | ||
| 177 | $explorer ->{Visible} = 1; | ||
| 178 | # sleep(5); | ||
| 179 | $IE->ExecWB(6, 1); # print with prompt | ||
| 180 | $IE->Quit(); | ||
| 181 | } | ||
| 182 | ###################################################################### | ||
| 183 | sub print { | ||
| 184 | # new style printing - just use stuff from wasx | ||
| 185 | my ($self, $data) = @_; | ||
| 186 | my $self = shift; | ||
| 187 | my $data = join('', @_); | ||
| 188 | # paper orientation | ||
| 189 | my $orient; | ||
| 190 | if ($self->{orientation} =~ /landscape/) { | ||
| 191 | $orient = 2; | ||
| 192 | } else { | ||
| 193 | $orient = 1; | ||
| 194 | } | ||
| 195 | |||
| 196 | print "printing $data to $self->{printer}{$OSNAME}, orientation $orient\n"; | ||
| 197 | |||
| 198 | my $printer = new Win32::Printer( papersize => 9, # USE a4 | ||
| 199 | dialog => 0, | ||
| 200 | orientation => $orient, | ||
| 201 | printer => $self->{printer}{$OSNAME}, | ||
| 202 | unit => 'mm'); | ||
| 203 | my $font = $printer->Font('Arial', 10); | ||
| 204 | $printer->Font($font); | ||
| 205 | $printer->Write($data, 10, 10); | ||
| 206 | $printer->Close; | ||
| 207 | } | ||
| 208 | ###################################################################### | ||
| 209 | sub print_orig { | ||
| 210 | # print- old style - deprecated to use Win32::Printer from wasx, | ||
| 211 | # Printer.pm versions 0.98 onwards | ||
| 212 | my $self = shift; | ||
| 213 | my $data = join("", @_); | ||
| 214 | unless ($self->{print_command}->{$OSNAME}) { | ||
| 215 | # default pipish method | ||
| 216 | my $printer = $self->{'printer'}{$OSNAME}; | ||
| 217 | |||
| 218 | # Windows NT variations | ||
| 219 | if ($self->{winver} =~ m/WinNT|Win2000/ ) { | ||
| 220 | open SPOOL, ">>$printer" or | ||
| 221 | Carp::croak "Can't open print spool " . $printer . ": $!" ; | ||
| 222 | print SPOOL $data or | ||
| 223 | Carp::croak "Can't write to print spool $self->{'printer'}: $!"; | ||
| 224 | close SPOOL; | ||
| 225 | } elsif ($self->{winver} =~ m/WinXP/) { | ||
| 226 | open SPOOL, ">>$printer" or | ||
| 227 | Carp::croak "Can't open print spool " . $printer . ": $!" ; | ||
| 228 | print SPOOL $data or | ||
| 229 | Carp::croak "Can't write to print spool $self->{'printer'}: $!"; | ||
| 230 | close SPOOL; | ||
| 231 | } | ||
| 232 | |||
| 233 | # any other windows version | ||
| 234 | # for win95, may work with ME | ||
| 235 | else { | ||
| 236 | my $spoolfile = get_unique_spool(); | ||
| 237 | open SPOOL, ">" . $spoolfile; | ||
| 238 | print SPOOL $data; | ||
| 239 | close SPOOL; | ||
| 240 | system("copy /B $spoolfile $self->{printer}{$OSNAME}"); | ||
| 241 | unlink $spoolfile; | ||
| 242 | } | ||
| 243 | |||
| 244 | } else { | ||
| 245 | # custom print command | ||
| 246 | if ($self->{print_command}->{$OSNAME}->{type} eq 'command') { | ||
| 247 | # non-pipe accepting command - use a spoolfile | ||
| 248 | my $cmd = $self->{print_command}->{$OSNAME}->{command}; | ||
| 249 | my @specials = qw(MS_word MS_excel MS_ie); | ||
| 250 | my $spoolfile = get_unique_spool(); | ||
| 251 | $cmd =~ s/FILE/$spoolfile/; | ||
| 252 | open SPOOL, ">" . $spoolfile; | ||
| 253 | print SPOOL $data; | ||
| 254 | close SPOOL; | ||
| 255 | |||
| 256 | if (grep /$cmd/, @specials) { | ||
| 257 | # run my new Win32^::OLE methods | ||
| 258 | if ($cmd eq 'MS_word') { | ||
| 259 | $self->MS_word($spoolfile); | ||
| 260 | } elsif ($cmd eq 'MS_excel') { | ||
| 261 | $self->MS_excel($spoolfile); | ||
| 262 | } elsif ($cmd eq 'MS_ie') { | ||
| 263 | $self->MS_ie($spoolfile); | ||
| 264 | } | ||
| 265 | } else { | ||
| 266 | system($cmd) or die $OS_ERROR; | ||
| 267 | unlink $spoolfile; | ||
| 268 | } | ||
| 269 | } else { | ||
| 270 | # pipe accepting command | ||
| 271 | # can't use this - windows perl doesn't support pipes. | ||
| 272 | } | ||
| 273 | } | ||
| 274 | } | ||
| 275 | ###################################################################### | ||
| 276 | 1; | ||
| 277 |