pspdf.pl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/perl
  2. #
  3. # Wrapper around a variety of programs that can do PS -> PDF conversion
  4. #
  5. use strict;
  6. use File::Spec;
  7. my $compress = 1;
  8. my $win32_ok = eval {
  9. require Win32::TieRegistry;
  10. Win32::TieRegistry->import();
  11. 1;
  12. };
  13. while ($ARGV[0] =~ /^-(.*)$/) {
  14. my $opt = $1;
  15. shift @ARGV;
  16. if ($opt eq '-nocompress') {
  17. $compress = 0;
  18. }
  19. }
  20. # Ghostscript executable name. "gs" on Unix-based systems.
  21. my $gs = 'gs';
  22. my ($in, $out) = @ARGV;
  23. if (!defined($out)) {
  24. die "Usage: $0 [-nocompress] infile ou{ tfile\n";
  25. }
  26. # If Win32, help GhostScript out with some defaults
  27. sub win32_gs_help() {
  28. return if (!$win32_ok);
  29. use Sort::Versions;
  30. use sort 'stable';
  31. my $Reg = $::Registry->Open('', {Access => 'KEY_READ', Delimiter => '/'});
  32. my $dir;
  33. my @gs;
  34. foreach my $k1 ('HKEY_CURRENT_USER/Software/',
  35. 'HKEY_LOCAL_MACHINE/SOFTWARE/') {
  36. foreach my $k2 ('Artifex/', '') {
  37. foreach my $k3 ('GPL Ghostscript/', 'AFPL Ghostscript/',
  38. 'Ghostscript/') {
  39. my $r = $Reg->{$k1.$k2.$k3};
  40. if (ref($r) eq 'Win32::TieRegistry') {
  41. foreach my $k (keys(%$r)) {
  42. my $rk = $r->{$k};
  43. if (ref($rk) eq 'Win32::TieRegistry' &&
  44. defined($rk->{'/GS_LIB'})) {
  45. push @gs, $rk;
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }
  52. @gs = sort {
  53. my($av) = $a->Path =~ m:^.*/([^/]+)/$:;
  54. my($bv) = $b->Path =~ m:^.*/([^/]+)/$:;
  55. versioncmp($av, $bv);
  56. } @gs;
  57. return unless (scalar(@gs));
  58. $ENV{'PATH'} .= ';' . $gs[0]->{'/GS_LIB'};
  59. $ENV{'GS_FONTPATH'} .= (defined($ENV{'GS_FONTPATH'}) ? ';' : '')
  60. . $ENV{'windir'}.'\\fonts';
  61. my $gsp = undef;
  62. foreach my $p (split(/\;/, $gs[0]->{'/GS_LIB'})) {
  63. foreach my $exe ('gswin64c.exe', 'gswin32c.exe', 'gs.exe') {
  64. last if (defined($gsp));
  65. my $e = File::Spec->catpath($p, $exe);
  66. $gsp = $e if (-f $e && -x _);
  67. }
  68. }
  69. $gs = $gsp if (defined($gsp));
  70. }
  71. # Remove output file
  72. unlink($out);
  73. # 1. Acrobat distiller
  74. my $r = system('acrodist', '-n', '-q', '--nosecurity', '-o', $out, $in);
  75. exit 0 if ( !$r && -f $out );
  76. # 2. ps2pdf (from Ghostscript)
  77. #
  78. # GhostScript uses # rather than = to separate options and values on Windows,
  79. # it seems. Call gs directly rather than ps2pdf, because -dSAFER
  80. # breaks font discovery on some systems, apparently.
  81. win32_gs_help();
  82. my $o = $win32_ok ? '#' : '=';
  83. my $r = system($gs, "-dCompatibilityLevel${o}1.4", "-q",
  84. "-P-", "-dNOPAUSE", "-dBATCH", "-sDEVICE${o}pdfwrite",
  85. "-sstdout${o}%stderr", "-sOutputFile${o}${out}",
  86. "-dOptimize${o}true", "-dEmbedAllFonts${o}true",
  87. "-dCompressPages${o}" . ($compress ? 'true' : 'false'),
  88. "-dUseFlateCompression${o}true",
  89. "-c", ".setpdfwrite", "-f", $in);
  90. exit 0 if ( !$r && -f $out );
  91. # 3. pstopdf (BSD/MacOS X utility)
  92. my $r = system('pstopdf', $in, '-o', $out);
  93. exit 0 if ( !$r && -f $out );
  94. # Otherwise, fail
  95. unlink($out);
  96. exit 1;