Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for printpdf.pl

(view source code of printpdf.pl as plain text)

  1. #! perl
  2.  
  3. # Store help text in variable
  4. $syntax = "\nPrintPDF.pl,  Version 1.00 for Windows\n";
  5. $syntax = $syntax."Print the specified PDF files\n\n";
  6. $syntax = $syntax."Usage:  PRINTPDF.PL  pdf_files\n\n";
  7. $syntax = $syntax."Where:               \"pdf_files\" is the (list of) PDF file(s) to be printed\n";
  8. $syntax = $syntax."                                 (separated by spaces, no wildcards allowed)\n\n";
  9. $syntax = $syntax."Note:   This script will only work if Acrobat Reader is the default\n";
  10. $syntax = $syntax."        association for PDF files.\n";
  11. $syntax = $syntax."        It usually fails on PCs with other Adobe software installed.\n\n";
  12. $syntax = $syntax."Written by Rob van der Woude\n";
  13. $syntax = $syntax."http://www.robvanderwoude.com\n";
  14.  
  15. # Check number of command line arguments (at least 1 required)
  16. if ( !@ARGV[0] ) {
  17. 	die $syntax;
  18. }
  19. # Check if arguments are valid files and build file list
  20. $pdflist = "";
  21. foreach ( @ARGV ) {
  22. 	die "\nFile not found: $_\n$syntax"    unless -e $_;
  23. 	die "\nInvalid filespec: $_\n$syntax"  unless -f $_;
  24. 	die "\nInvalid file type: $_\n$syntax" if -d $_ or -x $_ or -T $_;
  25. 	$pdflist = "$pdflist \"$_\"";
  26. }
  27.  
  28. # Initialize the required module
  29. use Win32API::Registry 0.21 qw( :ALL );
  30.  
  31. # Read the print command from the registry
  32. $key = ".pdf";
  33. RegOpenKeyEx( HKEY_CLASSES_ROOT, $key, 0, KEY_READ, $handle ) || die "Cannot open HKEY_CLASSES_ROOT\\$key: ".regLastError()."\n";
  34. RegQueryValueEx( $handle, "", [], $type, $filetype, [] )      || die "Cannot read HKEY_CLASSES_ROOT\\$key: ".regLastError()."\n";
  35. RegCloseKey( $handle )                                        || die "Cannot close HKEY_CLASSES_ROOT\\$key: ".regLastError()."\n";
  36. $key = "$filetype\\shell\\print\\command";
  37. RegOpenKeyEx( HKEY_CLASSES_ROOT, $key, 0, KEY_READ, $handle ) || die "Cannot open HKEY_CLASSES_ROOT\\$key: ".regLastError()."\n";
  38. RegQueryValueEx( $handle, "", [], $type, $printcmd, [] )      || die "Cannot read HKEY_CLASSES_ROOT\\$key: ".regLastError()."\n";
  39. RegCloseKey( $handle )                                        || die "Cannot close HKEY_CLASSES_ROOT\\$key: ".regLastError()."\n";
  40.  
  41. # Quit if Acrobat Reader is not the default association for PDF files
  42. $other = "\nThis script only works when Acrobat Reader is the default association\nfor PDF files.\n";
  43. $other = $other."On this PC that doesn't seem to be the case, maybe because other\nAdobe software is installed as well.";
  44. die $other if index( $printcmd, "{" );
  45.  
  46. # Display a message telling the user to manually close Acrobat Reader
  47. print "\nWaiting for you to close the Acrobat Reader window manually after it has sent\nall files to the printer...\n";
  48.  
  49. # Execute the print command for all files from the list
  50. $printcmd =~ s/"%1"/$pdflist/;
  51. `$printcmd`;
  52.  

page last uploaded: 2017-04-06, 13:33