2017-04-07 11:20:04 +08:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# Wrapper around a variety of programs that can do PS -> PDF conversion
|
|
|
|
#
|
|
|
|
|
|
|
|
use strict;
|
2017-04-26 15:05:12 +08:00
|
|
|
use File::Spec;
|
2017-04-07 11:20:04 +08:00
|
|
|
|
2017-04-11 07:10:28 +08:00
|
|
|
my $compress = 1;
|
|
|
|
|
2017-04-26 15:05:12 +08:00
|
|
|
my $win32_ok = eval {
|
2017-04-26 15:16:57 +08:00
|
|
|
require Win32::TieRegistry;
|
|
|
|
Win32::TieRegistry->import();
|
|
|
|
1;
|
2017-04-26 15:05:12 +08:00
|
|
|
};
|
|
|
|
|
2017-04-11 07:10:28 +08:00
|
|
|
while ($ARGV[0] =~ /^-(.*)$/) {
|
|
|
|
my $opt = $1;
|
|
|
|
shift @ARGV;
|
|
|
|
|
|
|
|
if ($opt eq '-nocompress') {
|
2017-04-26 15:16:57 +08:00
|
|
|
$compress = 0;
|
2017-04-11 07:10:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 09:02:22 +08:00
|
|
|
# Ghostscript executable name. "gs" on Unix-based systems.
|
|
|
|
my $gs = 'gs';
|
|
|
|
|
2017-04-07 11:20:04 +08:00
|
|
|
my ($in, $out) = @ARGV;
|
|
|
|
|
|
|
|
if (!defined($out)) {
|
2017-04-26 15:16:57 +08:00
|
|
|
die "Usage: $0 [-nocompress] infile ou{ tfile\n";
|
2017-04-26 15:05:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# If Win32, help GhostScript out with some defaults
|
|
|
|
sub win32_gs_help() {
|
2017-04-26 15:16:57 +08:00
|
|
|
return if (!$win32_ok);
|
|
|
|
|
|
|
|
use Sort::Versions;
|
|
|
|
use sort 'stable';
|
|
|
|
|
|
|
|
my $Reg = $::Registry->Open('', {Access => 'KEY_READ', Delimiter => '/'});
|
|
|
|
my $dir;
|
|
|
|
my @gs;
|
|
|
|
|
|
|
|
foreach my $k1 ('HKEY_CURRENT_USER/Software/',
|
|
|
|
'HKEY_LOCAL_MACHINE/SOFTWARE/') {
|
|
|
|
foreach my $k2 ('Artifex/', '') {
|
|
|
|
foreach my $k3 ('GPL Ghostscript/', 'AFPL Ghostscript/',
|
|
|
|
'Ghostscript/') {
|
|
|
|
my $r = $Reg->{$k1.$k2.$k3};
|
|
|
|
if (ref($r) eq 'Win32::TieRegistry') {
|
|
|
|
foreach my $k (keys(%$r)) {
|
2017-04-26 15:05:12 +08:00
|
|
|
my $rk = $r->{$k};
|
|
|
|
if (ref($rk) eq 'Win32::TieRegistry' &&
|
|
|
|
defined($rk->{'/GS_LIB'})) {
|
|
|
|
push @gs, $rk;
|
|
|
|
}
|
2017-04-26 15:16:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@gs = sort {
|
|
|
|
my($av) = $a->Path =~ m:^.*/([^/]+)/$:;
|
|
|
|
my($bv) = $b->Path =~ m:^.*/([^/]+)/$:;
|
|
|
|
versioncmp($av, $bv);
|
|
|
|
} @gs;
|
|
|
|
|
|
|
|
return unless (scalar(@gs));
|
|
|
|
|
|
|
|
$ENV{'PATH'} .= ';' . $gs[0]->{'/GS_LIB'};
|
|
|
|
$ENV{'GS_FONTPATH'} .= (defined($ENV{'GS_FONTPATH'}) ? ';' : '')
|
|
|
|
. $ENV{'windir'}.'\\fonts';
|
2018-09-25 09:02:22 +08:00
|
|
|
|
|
|
|
my $gsp = undef;
|
|
|
|
foreach my $p (split(/\;/, $gs[0]->{'/GS_LIB'})) {
|
|
|
|
foreach my $exe ('gswin64c.exe', 'gswin32c.exe', 'gs.exe') {
|
|
|
|
last if (defined($gsp));
|
|
|
|
my $e = File::Spec->catpath($p, $exe);
|
|
|
|
$gsp = $e if (-f $e && -x _);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$gs = $gsp if (defined($gsp));
|
2017-04-07 11:20:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Remove output file
|
|
|
|
unlink($out);
|
|
|
|
|
|
|
|
# 1. Acrobat distiller
|
|
|
|
my $r = system('acrodist', '-n', '-q', '--nosecurity', '-o', $out, $in);
|
|
|
|
exit 0 if ( !$r && -f $out );
|
|
|
|
|
|
|
|
# 2. ps2pdf (from Ghostscript)
|
2018-09-25 09:02:22 +08:00
|
|
|
#
|
|
|
|
# GhostScript uses # rather than = to separate options and values on Windows,
|
|
|
|
# it seems. Call gs directly rather than ps2pdf, because -dSAFER
|
|
|
|
# breaks font discovery on some systems, apparently.
|
2017-04-26 15:05:12 +08:00
|
|
|
win32_gs_help();
|
2018-09-25 09:02:22 +08:00
|
|
|
my $o = $win32_ok ? '#' : '=';
|
|
|
|
my $r = system($gs, "-dCompatibilityLevel${o}1.4", "-q",
|
|
|
|
"-P-", "-dNOPAUSE", "-dBATCH", "-sDEVICE${o}pdfwrite",
|
|
|
|
"-sstdout${o}%stderr", "-sOutputFile${o}${out}",
|
|
|
|
"-dOptimize${o}true", "-dEmbedAllFonts${o}true",
|
2017-04-26 15:16:57 +08:00
|
|
|
"-dCompressPages${o}" . ($compress ? 'true' : 'false'),
|
2018-09-25 09:02:22 +08:00
|
|
|
"-dUseFlateCompression${o}true",
|
|
|
|
"-c", ".setpdfwrite", "-f", $in);
|
2017-04-07 11:20:04 +08:00
|
|
|
exit 0 if ( !$r && -f $out );
|
|
|
|
|
|
|
|
# 3. pstopdf (BSD/MacOS X utility)
|
|
|
|
my $r = system('pstopdf', $in, '-o', $out);
|
|
|
|
exit 0 if ( !$r && -f $out );
|
|
|
|
|
|
|
|
# Otherwise, fail
|
|
|
|
unlink($out);
|
|
|
|
exit 1;
|