| 1 |
#!/usr/bin/env perl |
| 2 |
use strict; |
| 3 |
$^W=1; # turn warning on |
| 4 |
# |
| 5 |
# pdfcrop.pl |
| 6 |
# |
| 7 |
# Copyright (C) 2002, 2004, 2005, 2008-2012 Heiko Oberdiek. |
| 8 |
# 2020-2023 Oberdiek Package Support Group |
| 9 |
# |
| 10 |
# This program may be distributed and/or modified under the |
| 11 |
# conditions of the LaTeX Project Public License, version 1.3c or later. |
| 12 |
# |
| 13 |
# See file "README.md" for a list of files that belong to this project. |
| 14 |
# |
| 15 |
# This file "pdfcrop.pl" may be renamed to "pdfcrop" |
| 16 |
# for installation purposes. |
| 17 |
# |
| 18 |
my $prj = 'pdfcrop'; |
| 19 |
my $file = "$prj.pl"; |
| 20 |
my $program = uc($&) if $file =~ /^\w+/; |
| 21 |
my $version = "1.42"; |
| 22 |
my $date = "2023/04/15"; |
| 23 |
my $author = "Heiko Oberdiek, Oberdiek Package Support Group"; |
| 24 |
my $copyright = "Copyright (c) 2002-2023 by $author."; |
| 25 |
# |
| 26 |
# Reqirements: Perl5, Ghostscript |
| 27 |
# History: |
| 28 |
# 2002/10/30 v1.0: First release. |
| 29 |
# 2002/10/30 v1.1: Option --hires added. |
| 30 |
# 2002/11/04 v1.2: "nul" instead of "/dev/null" for windows. |
| 31 |
# 2002/11/23 v1.3: Use of File::Spec module's "devnull" call. |
| 32 |
# 2002/11/29 v1.4: Option --papersize added. |
| 33 |
# 2004/06/24 v1.5: Clear map file entries so that pdfTeX |
| 34 |
# does not touch the fonts. |
| 35 |
# 2004/06/26 v1.6: Use mgs.exe instead of gswin32c.exe for MIKTEX. |
| 36 |
# 2005/03/11 v1.7: Support of spaces in file names |
| 37 |
# (open("-|") is used for ghostscript call). |
| 38 |
# 2008/01/09 v1.8: Fix for moving the temporary file to the output |
| 39 |
# file across file system boundaries. |
| 40 |
# 2008/04/05 v1.9: Options --resolution and --bbox added. |
| 41 |
# 2008/07/16 v1.10: Support for XeTeX added with new options |
| 42 |
# --pdftex, --xetex, and --xetexcmd. |
| 43 |
# 2008/07/22 v1.11: Workaround for open("-|"). |
| 44 |
# 2008/07/23 v1.12: Workarounds for the workaround (error detection, ...). |
| 45 |
# 2008/07/24 v1.13: open("-|")/workaround removed. |
| 46 |
# Input files with unsafe file names are linked/copied |
| 47 |
# to temporary file with safe file name. |
| 48 |
# 2008/09/12 v1.14: Error detection for invalid Bounding Boxes. |
| 49 |
# 2009/07/14 v1.15: Fix for negative coordinates in Bounding Boxes |
| 50 |
# (David Menestrina). |
| 51 |
# 2009/07/16 v1.16: Security fixes: |
| 52 |
# * -dSAFER added for Ghostscript, |
| 53 |
# * -no-shell-escape added for pdfTeX/XeTeX. |
| 54 |
# 2009/07/17 v1.17: Security fixes: |
| 55 |
# * Backticks and whitespace are forbidden |
| 56 |
# for options --(gs|pdftex|xetex)cmd. |
| 57 |
# * Validation of options --papersize and --resolution. |
| 58 |
# 2009/07/18 v1.18: * Restricted mode added. |
| 59 |
# * Option --version added. |
| 60 |
# 2009/09/24 v1.19: * Ghostscript detection rewritten. |
| 61 |
# * Cygwin: `gs' is preferred to `gswin32c'. |
| 62 |
# 2009/10/06 v1.20: * File name sanitizing in .tex file. |
| 63 |
# 2009/12/21 v1.21: * Option --ini added for IniTeX mode. |
| 64 |
# * Option --luatex and --luatexcmd added for LuaTeX. |
| 65 |
# 2009/12/29 v1.22: * Syntax description for option --bbox fixed |
| 66 |
# (Lukas Prochazka). |
| 67 |
# 2010/01/09 v1.23: * Options --bbox-odd and -bbox-even added. |
| 68 |
# 2010/08/16 v1.24: * Workaround added for buggy ghostscript ports |
| 69 |
# that print the BoundingBox data twice. |
| 70 |
# 2010/08/26 v1.25: * Fix for the case that the PDF file contains |
| 71 |
# an entry /CropBox different to /MediaBox. |
| 72 |
# * \pageinclude implemented for XeTeX. |
| 73 |
# * XeTeX: --clip does not die, but this option |
| 74 |
# is ignored, because XeTeX always clip. |
| 75 |
# 2010/08/26 v1.26: * XeTeX's \XeTeXpdffile expects keyword |
| 76 |
# `media', not `mediabox'. |
| 77 |
# * New option --pdfversion. |
| 78 |
# Default is `auto' that means the PDF version |
| 79 |
# is inherited from the input file. Before |
| 80 |
# pdfcrop has used the TeX engine's default. |
| 81 |
# * Option --luatex fixed (extra empty page at end). |
| 82 |
# 2010/09/03 v1.27: * Workaround of v1.24 fixed. |
| 83 |
# 2010/09/06 v1.28: * The Windows registry is searched if Ghostscript |
| 84 |
# is not found via PATH. |
| 85 |
# * Windows only: support of spaces in command |
| 86 |
# names in unrestricted mode. |
| 87 |
# 2010/09/06 v1.29: * Find the latest Ghostscript version in registry. |
| 88 |
# 2010/09/15 v1.30: * Warning of pdfTeX because of \pdfobjcompresslevel |
| 89 |
# avoided when reducing \pdfminorversion. |
| 90 |
# * Fix for TeX syntax characters in input file names. |
| 91 |
# 2010/09/17 v1.31: * Passing the input file name via hex string to TeX. |
| 92 |
# * Again input file names restricted for Ghostscript |
| 93 |
# command line, switch then to symbol link/copy |
| 94 |
# method. |
| 95 |
# 2011/08/10 v1.32: * Detection for gswin64c.exe added. |
| 96 |
# 2012/02/01 v1.33: * Input file can be `-' (standard input). |
| 97 |
# 2012/04/18 v1.34: * Format of --version changed |
| 98 |
# from naked version number to a line with |
| 99 |
# program name, date and version. |
| 100 |
# 2012/10/15 v1.35: * Additional debug infos added for Perl version. |
| 101 |
# 2012/10/16 v1.36: * More error codes added. |
| 102 |
# 2012/10/16 v1.37: * Extended error messages if available. |
| 103 |
# * Fix for broken v1.36. |
| 104 |
# 2012/11/02 v1.38: * Fix for unsufficient cleanup, if function `cleanup' is |
| 105 |
# prematurely called in `eval' for `symlink' checking. |
| 106 |
# 2020/05/24 v1.39: * adapted to pdfversion 2.0, corrected luatex support, |
| 107 |
# corrected a problem with xetex. |
| 108 |
# 2020/06/06 v1.40: * improved ghostscript detection on windows when a bash is used |
| 109 |
# added direct pdf version support to xetex. |
| 110 |
# 2023/04/13 v1.41: * allow gswin64c in restricted mode, fix typos in messages issues 14, 17 |
| 111 |
# add -q option, issue 7; |
| 112 |
# don't print whole help msg for unknown options, issue 7. |
| 113 |
# do not create two pages with xetex, issue 3 |
| 114 |
# 2023/04/15 v1.42: * update help text issue 18 |
| 115 |
|
| 116 |
### program identification |
| 117 |
my $title = "$program $version, $date - $copyright\n"; |
| 118 |
|
| 119 |
### error strings |
| 120 |
my $Error = "!!! Error:"; # error prefix |
| 121 |
|
| 122 |
### make ENV safer |
| 123 |
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; |
| 124 |
|
| 125 |
# Windows detection (no SIGHUP) |
| 126 |
my $Win = 0; |
| 127 |
$Win = 1 if $^O =~ /mswin32/i; |
| 128 |
$Win = 1 if $^O =~ /windows/i; |
| 129 |
$Win = 1 if $^O =~ /cygwin/i; |
| 130 |
use Config; |
| 131 |
my $archname = $Config{'archname'}; |
| 132 |
$archname = 'unknown' unless defined $Config{'archname'}; |
| 133 |
|
| 134 |
### extended error messages |
| 135 |
sub exterr () { |
| 136 |
chomp(my $msg_errno = $!); |
| 137 |
chomp(my $msg_extended_os_error = $^E); |
| 138 |
if ($msg_errno eq $msg_extended_os_error) { |
| 139 |
$msg_errno; |
| 140 |
} |
| 141 |
else { |
| 142 |
"$msg_errno/$msg_extended_os_error"; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
### string constants for Ghostscript run |
| 147 |
# get Ghostscript command name |
| 148 |
$::opt_gscmd = ''; |
| 149 |
sub find_ghostscript () { |
| 150 |
return if $::opt_gscmd; |
| 151 |
if ($::opt_debug) { |
| 152 |
print "* Perl executable: $^X\n"; |
| 153 |
if ($] < 5.006) { |
| 154 |
print "* Perl version: $]\n"; |
| 155 |
} |
| 156 |
else { |
| 157 |
printf "* Perl version: v%vd\n", $^V; |
| 158 |
} |
| 159 |
if (defined &ActivePerl::BUILD) { |
| 160 |
printf "* Perl product: ActivePerl, build %s\n", ActivePerl::BUILD(); |
| 161 |
} |
| 162 |
use Config; |
| 163 |
printf "* Pointer size: $Config{'ptrsize'}\n"; |
| 164 |
printf "* Pipe support: %s\n", |
| 165 |
(defined($Config{'d_pipe'}) ? 'yes' : 'no'); |
| 166 |
printf "* Fork support: %s\n", |
| 167 |
(defined($Config{'d_fork'}) ? 'yes' : 'no'); |
| 168 |
} |
| 169 |
my $system = 'unix'; |
| 170 |
$system = "dos" if $^O =~ /dos/i; |
| 171 |
$system = "os2" if $^O =~ /os2/i; |
| 172 |
$system = "win" if $^O =~ /mswin32/i; |
| 173 |
$system = "win" if $^O =~ /windows/i; |
| 174 |
$system = "cygwin" if $^O =~ /cygwin/i; |
| 175 |
$system = "miktex" if defined($ENV{"TEXSYSTEM"}) and |
| 176 |
$ENV{"TEXSYSTEM"} =~ /miktex/i; |
| 177 |
print "* OS name: $^O\n" if $::opt_debug; |
| 178 |
print "* Arch name: $archname\n" if $::opt_debug; |
| 179 |
print "* System: $system\n" if $::opt_debug; |
| 180 |
my %candidates = ( |
| 181 |
'unix' => [qw|gs gsc gswin64c gswin32c|], |
| 182 |
'dos' => [qw|gs386 gs|], |
| 183 |
'os2' => [qw|gsos2 gs|], |
| 184 |
'win' => [qw|gswin32c gswin64c gs|], |
| 185 |
'cygwin' => [qw|gs gswin32c|], |
| 186 |
'miktex' => [qw|mgs gswin32c gs|] |
| 187 |
); |
| 188 |
if ($system eq 'win' or $system eq 'cygwin' or $system eq 'miktex') { |
| 189 |
if ($archname =~ /mswin32-x64/i) { |
| 190 |
my @a = (); |
| 191 |
foreach my $name (@{$candidates{$system}}) { |
| 192 |
push @a, 'gswin64c' if $name eq 'gswin32c'; |
| 193 |
push @a, $name; |
| 194 |
} |
| 195 |
$candidates{$system} = \@a; |
| 196 |
} |
| 197 |
} |
| 198 |
my %ext = ( |
| 199 |
'unix' => '', |
| 200 |
'dos' => '.exe', |
| 201 |
'os2' => '.exe', |
| 202 |
'win' => '.exe', |
| 203 |
'cygwin' => '.exe', |
| 204 |
'miktex' => '.exe' |
| 205 |
); |
| 206 |
my $candidates_ref = $candidates{$system}; |
| 207 |
my $ext = $Config{'_exe'}; |
| 208 |
$ext = $ext{$system} unless defined $ext; |
| 209 |
use File::Spec; |
| 210 |
my @path = File::Spec->path(); |
| 211 |
my $found = 0; |
| 212 |
foreach my $candidate (@$candidates_ref) { |
| 213 |
foreach my $dir (@path) { |
| 214 |
my $file = File::Spec->catfile($dir, "$candidate$ext"); |
| 215 |
if (-x $file) { |
| 216 |
$::opt_gscmd = $candidate; |
| 217 |
$found = 1; |
| 218 |
print "* Found ($candidate): $file\n" if $::opt_debug; |
| 219 |
last; |
| 220 |
} |
| 221 |
print "* Not found ($candidate): $file\n" if $::opt_debug; |
| 222 |
} |
| 223 |
last if $found; |
| 224 |
} |
| 225 |
if (not $found and $Win) { |
| 226 |
$found = SearchRegistry(); |
| 227 |
} |
| 228 |
if ($found) { |
| 229 |
print "* Autodetected Ghostscript command: $::opt_gscmd\n" if $::opt_debug; |
| 230 |
} |
| 231 |
else { |
| 232 |
$::opt_gscmd = $$candidates_ref[0]; |
| 233 |
print "* Default Ghostscript command: $::opt_gscmd\n" if $::opt_debug; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
sub SearchRegistry () { |
| 238 |
my $found = 0; |
| 239 |
eval 'use Win32::TieRegistry qw|KEY_READ REG_SZ|;'; |
| 240 |
if ($@) { |
| 241 |
if ($::opt_debug) { |
| 242 |
print "* Registry lookup for Ghostscript failed:\n"; |
| 243 |
my $msg = $@; |
| 244 |
$msg =~ s/\s+$//; |
| 245 |
foreach (split /\r?\n/, $msg) { |
| 246 |
print " $_\n"; |
| 247 |
} |
| 248 |
} |
| 249 |
return $found; |
| 250 |
} |
| 251 |
my $open_params = {Access => KEY_READ(), Delimiter => '/'}; |
| 252 |
my $key_name_software = 'HKEY_LOCAL_MACHINE/SOFTWARE/'; |
| 253 |
my $current_key = $key_name_software; |
| 254 |
my $software = new Win32::TieRegistry $current_key, $open_params; |
| 255 |
if (not $software) { |
| 256 |
print "* Cannot find or access registry key `$current_key'!\n" |
| 257 |
if $::opt_verbose; |
| 258 |
return $found; |
| 259 |
} |
| 260 |
print "* Search registry at `$current_key'.\n" if $::opt_debug; |
| 261 |
my %list; |
| 262 |
foreach my $key_name_gs (grep /Ghostscript/i, $software->SubKeyNames()) { |
| 263 |
$current_key = "$key_name_software$key_name_gs/"; |
| 264 |
print "* Registry entry found: $current_key\n" if $::opt_debug; |
| 265 |
my $key_gs = $software->Open($key_name_gs, $open_params); |
| 266 |
if (not $key_gs) { |
| 267 |
print "* Cannot open registry key `$current_key'!\n" if $::opt_debug; |
| 268 |
next; |
| 269 |
} |
| 270 |
foreach my $key_name_version ($key_gs->SubKeyNames()) { |
| 271 |
$current_key = "$key_name_software$key_name_gs/$key_name_version/"; |
| 272 |
print "* Registry entry found: $current_key\n" if $::opt_debug; |
| 273 |
if (not $key_name_version =~ /^(\d+)\.(\d+)$/) { |
| 274 |
print " The sub key is not a version number!\n" if $::opt_debug; |
| 275 |
next; |
| 276 |
} |
| 277 |
my $version_main = $1; |
| 278 |
my $version_sub = $2; |
| 279 |
$current_key = "$key_name_software$key_name_gs/$key_name_version/"; |
| 280 |
my $key_version = $key_gs->Open($key_name_version, $open_params); |
| 281 |
if (not $key_version) { |
| 282 |
print "* Cannot open registry key `$current_key'!\n" if $::opt_debug; |
| 283 |
next; |
| 284 |
} |
| 285 |
$key_version->FixSzNulls(1); |
| 286 |
my ($value, $type) = $key_version->GetValue('GS_DLL'); |
| 287 |
if ($value and $type == REG_SZ()) { |
| 288 |
print " GS_DLL = $value\n" if $::opt_debug; |
| 289 |
$value =~ s|([\\/])([^\\/]+\.dll)$|$1gswin32c.exe|i; |
| 290 |
my $value64 = $value; |
| 291 |
$value64 =~ s/gswin32c\.exe$/gswin64c.exe/; |
| 292 |
if ($archname =~ /mswin32-x64/i and -f $value64) { |
| 293 |
$value = $value64; |
| 294 |
} |
| 295 |
if (-f $value) { |
| 296 |
print " EXE found: $value\n" if $::opt_debug; |
| 297 |
} |
| 298 |
else { |
| 299 |
print " EXE not found!\n" if $::opt_debug; |
| 300 |
next; |
| 301 |
} |
| 302 |
my $sortkey = sprintf '%02d.%03d %s', |
| 303 |
$version_main, $version_sub, $key_name_gs; |
| 304 |
$list{$sortkey} = $value; |
| 305 |
} |
| 306 |
else { |
| 307 |
print " Missing key `GS_DLL' with type `REG_SZ'!\n" if $::opt_debug; |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
foreach my $entry (reverse sort keys %list) { |
| 312 |
$::opt_gscmd = $list{$entry}; |
| 313 |
print "* Found (via registry): $::opt_gscmd\n" if $::opt_debug; |
| 314 |
$found = 1; |
| 315 |
last; |
| 316 |
} |
| 317 |
return $found; |
| 318 |
} |
| 319 |
|
| 320 |
# restricted mode |
| 321 |
my $restricted = 0; |
| 322 |
if ($0 =~ /rpdfcrop/ or $0 =~ /restricted/) { |
| 323 |
$restricted = 1; |
| 324 |
} |
| 325 |
|
| 326 |
# "null" device |
| 327 |
use File::Spec::Functions qw(devnull); |
| 328 |
my $null = devnull(); |
| 329 |
|
| 330 |
### variables |
| 331 |
my $inputfile = ""; |
| 332 |
my $outputfile = ""; |
| 333 |
my $tmp = "tmp-\L$program\E-$$"; |
| 334 |
|
| 335 |
### paper sizes |
| 336 |
|
| 337 |
my @papersizes = qw[ |
| 338 |
11x17 ledger legal letter lettersmall |
| 339 |
archE archD archC archB archA |
| 340 |
a0 a1 a2 a3 a4 a4small a5 a6 a7 a8 a9 a10 |
| 341 |
isob0 isob1 isob2 isob3 isob4 isob5 isob6 |
| 342 |
c0 c1 c2 c3 c4 c5 c6 |
| 343 |
jisb0 jisb1 jisb2 jisb3 jisb4 jisb5 jisb6 |
| 344 |
b0 b1 b2 b3 b4 b5 |
| 345 |
flsa flse halfletter |
| 346 |
]; |
| 347 |
my %papersizes; |
| 348 |
foreach (@papersizes) { |
| 349 |
$papersizes{$_} = 1; |
| 350 |
} |
| 351 |
|
| 352 |
### option variables |
| 353 |
my @bool = ("false", "true"); |
| 354 |
$::opt_version = 0; |
| 355 |
$::opt_help = 0; |
| 356 |
$::opt_debug = 0; |
| 357 |
$::opt_verbose = 0; |
| 358 |
$::opt_quiet = 0; |
| 359 |
$::opt_pdftexcmd = "pdftex"; |
| 360 |
$::opt_xetexcmd = "xetex"; |
| 361 |
$::opt_luatexcmd = "luatex"; |
| 362 |
$::opt_tex = "pdftex"; |
| 363 |
$::opt_margins = "0 0 0 0"; |
| 364 |
$::opt_clip = 0; |
| 365 |
$::opt_hires = 0; |
| 366 |
$::opt_papersize = ""; |
| 367 |
$::opt_resolution = ""; |
| 368 |
$::opt_bbox = ""; |
| 369 |
$::opt_bbox_odd = ""; |
| 370 |
$::opt_bbox_even = ""; |
| 371 |
$::opt_initex = 0; |
| 372 |
$::opt_pdfversion = "auto"; |
| 373 |
$::opt_uncompress = 0; |
| 374 |
|
| 375 |
sub usage ($) { |
| 376 |
my $ret = shift; |
| 377 |
find_ghostscript(); |
| 378 |
my $usage = <<"END_OF_USAGE"; |
| 379 |
${title}Syntax: \L$program\E [options] <input[.pdf]> [output file] |
| 380 |
Function: Margins are calculated and removed for each page in the file. |
| 381 |
Options: (defaults:) |
| 382 |
--help print usage |
| 383 |
--version print version number |
| 384 |
--(no)verbose verbose printing ($bool[$::opt_verbose]) |
| 385 |
--(no)quiet silence normal output ($bool[$::opt_quiet]) |
| 386 |
--(no)debug debug information ($bool[$::opt_debug]) |
| 387 |
--gscmd <name> call of Ghostscript ($::opt_gscmd) |
| 388 |
--pdftex | --xetex | --luatex |
| 389 |
use pdfTeX | use XeTeX | use LuaTeX ($::opt_tex) |
| 390 |
--pdftexcmd <name> call of pdfTeX ($::opt_pdftexcmd) |
| 391 |
--xetexcmd <name> call of XeTeX ($::opt_xetexcmd) |
| 392 |
--luatexcmd <name> call of LuaTeX ($::opt_luatexcmd) |
| 393 |
--margins "<left> <top> <right> <bottom>" ($::opt_margins) |
| 394 |
add extra margins, unit is bp. If only one number is |
| 395 |
given, then it is used for all margins; in the case |
| 396 |
of two numbers, they are also used for right and bottom. |
| 397 |
--(no)clip clipping support, if margins are set ($bool[$::opt_clip]) |
| 398 |
(not available for --xetex) |
| 399 |
--(no)hires use `%%HiResBoundingBox' ($bool[$::opt_hires]) |
| 400 |
instead of `%%BoundingBox' |
| 401 |
--(no)ini use iniTeX variant of the TeX compiler ($bool[$::opt_initex]) |
| 402 |
Expert options: |
| 403 |
--restricted turn on restricted mode ($bool[$restricted]) |
| 404 |
--papersize <foo> parameter for gs's -sPAPERSIZE=<foo>, |
| 405 |
use only with older gs versions <7.32 ($::opt_papersize) |
| 406 |
--resolution <xres>x<yres> ($::opt_resolution) |
| 407 |
--resolution <res> pass argument to Ghostscript's option -r |
| 408 |
Example: --resolution 72 |
| 409 |
--bbox "<left> <bottom> <right> <top>" ($::opt_bbox) |
| 410 |
override bounding box found by Ghostscript |
| 411 |
with origin at the lower left corner |
| 412 |
--bbox-odd Same as --bbox, but for odd pages only ($::opt_bbox_odd) |
| 413 |
--bbox-even Same as --bbox, but for even pages only ($::opt_bbox_even) |
| 414 |
--pdfversion <x.y> | auto | none |
| 415 |
Set the PDF version to x.y, x= 1 or 2, y= 0-9. |
| 416 |
If `auto' is given as value, then the |
| 417 |
PDF version is taken from the header |
| 418 |
of the input PDF file. |
| 419 |
An empty value or `none' uses the |
| 420 |
default of the TeX engine. ($::opt_pdfversion) |
| 421 |
--uncompress create uncompressed pdf, for debugging ($bool[$::opt_uncompress]) |
| 422 |
|
| 423 |
Input file: If the name is `-', then the standard input is used and |
| 424 |
the output file name must be explicitly given. |
| 425 |
Examples: |
| 426 |
\L$program\E --margins 10 input.pdf output.pdf |
| 427 |
\L$program\E --margins '5 10 5 20' --clip input.pdf output.pdf |
| 428 |
In case of errors: |
| 429 |
Try option --verbose first to get more information. |
| 430 |
In case of bugs: |
| 431 |
Please, use option --debug for bug reports. |
| 432 |
END_OF_USAGE |
| 433 |
if ($ret) { |
| 434 |
die $usage; |
| 435 |
} |
| 436 |
else { |
| 437 |
print $usage; |
| 438 |
exit(0); |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
### process options |
| 443 |
my @OrgArgv = @ARGV; |
| 444 |
use Getopt::Long; |
| 445 |
GetOptions( |
| 446 |
"help!", |
| 447 |
"version!", |
| 448 |
"debug!", |
| 449 |
"verbose!", |
| 450 |
"quiet!", |
| 451 |
"gscmd=s", |
| 452 |
"pdftexcmd=s", |
| 453 |
"xetexcmd=s", |
| 454 |
"luatexcmd=s", |
| 455 |
"pdftex" => sub { $::opt_tex = 'pdftex'; }, |
| 456 |
"xetex" => sub { $::opt_tex = 'xetex'; }, |
| 457 |
"luatex" => sub { $::opt_tex = 'luatex'; }, |
| 458 |
"initex!", |
| 459 |
"margins=s", |
| 460 |
"clip!", |
| 461 |
"hires!", |
| 462 |
"papersize=s", |
| 463 |
"resolution=s", |
| 464 |
"bbox=s", |
| 465 |
"bbox-odd=s" => \$::opt_bbox_odd, |
| 466 |
"bbox-even=s" => \$::opt_bbox_even, |
| 467 |
"restricted" => sub { $restricted = 1; }, |
| 468 |
"pdfversion=s" => \$::opt_pdfversion, |
| 469 |
"uncompress!", |
| 470 |
) or die "Try $0 --help for more information\n"; |
| 471 |
|
| 472 |
!$::opt_help or usage(0); |
| 473 |
|
| 474 |
if ($::opt_version) { |
| 475 |
print "$prj $date v$version\n"; |
| 476 |
exit(0); |
| 477 |
} |
| 478 |
|
| 479 |
$::opt_verbose = 1 if $::opt_debug; |
| 480 |
|
| 481 |
@ARGV >= 1 or usage(1); |
| 482 |
|
| 483 |
print $title if ! $::opt_quiet; |
| 484 |
|
| 485 |
print "* Restricted mode: ", ($restricted ? "enabled" : "disabled"), "\n" |
| 486 |
if $::opt_debug; |
| 487 |
|
| 488 |
$::opt_pdfversion =~ /^(|none|auto|(([1-2])\.(\d)))$/ |
| 489 |
or die "!!! Error: Invalid value `$::opt_pdfversion' for option `--pdfversion'!\n"; |
| 490 |
print "* Option `pdfversion': $::opt_pdfversion\n" if $::opt_debug; |
| 491 |
$::opt_pdfversion = $2 if $2; |
| 492 |
$::opt_pdfmajorversion = $3 if $2; |
| 493 |
$::opt_pdfminorversion = $4 if $2; |
| 494 |
$::opt_pdfversion = '' if $::opt_pdfversion eq 'none'; |
| 495 |
|
| 496 |
find_ghostscript(); |
| 497 |
|
| 498 |
if ($::opt_bbox) { |
| 499 |
$::opt_bbox =~ s/^\s+//; |
| 500 |
$::opt_bbox =~ s/\s+$//; |
| 501 |
$::opt_bbox =~ s/\s+/ /; |
| 502 |
if ($::opt_bbox =~ /^-?\d*\.?\d+ -?\d*\.?\d+ -?\d*\.?\d+ -?\d*\.?\d+$/) { |
| 503 |
print "* Explicit Bounding Box: $::opt_bbox\n" if $::opt_debug; |
| 504 |
} |
| 505 |
else { |
| 506 |
die "$Error Parse error (option --bbox \"$::opt_bbox\")!\n"; |
| 507 |
} |
| 508 |
} |
| 509 |
if ($::opt_bbox_odd) { |
| 510 |
$::opt_bbox_odd =~ s/^\s+//; |
| 511 |
$::opt_bbox_odd =~ s/\s+$//; |
| 512 |
$::opt_bbox_odd =~ s/\s+/ /; |
| 513 |
if ($::opt_bbox_odd =~ /^-?\d*\.?\d+ -?\d*\.?\d+ -?\d*\.?\d+ -?\d*\.?\d+$/) { |
| 514 |
print "* Explicit Bounding Box for odd pages: $::opt_bbox_odd\n" |
| 515 |
if $::opt_debug; |
| 516 |
} |
| 517 |
else { |
| 518 |
die "$Error Parse error (option --bbox-odd \"$::opt_bbox_odd\")!\n"; |
| 519 |
} |
| 520 |
} |
| 521 |
if ($::opt_bbox_even) { |
| 522 |
$::opt_bbox_even =~ s/^\s+//; |
| 523 |
$::opt_bbox_even =~ s/\s+$//; |
| 524 |
$::opt_bbox_even =~ s/\s+/ /; |
| 525 |
if ($::opt_bbox_even =~ /^-?\d*\.?\d+ -?\d*\.?\d+ -?\d*\.?\d+ -?\d*\.?\d+$/) { |
| 526 |
print "* Explicit Bounding Box for even pages: $::opt_bbox_even\n" |
| 527 |
if $::opt_debug; |
| 528 |
} |
| 529 |
else { |
| 530 |
die "$Error Parse error (option --bbox-even \"$::opt_bbox_even\")!\n"; |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
@ARGV <= 2 or die "$Error Too many files!\n"; |
| 535 |
|
| 536 |
### input file |
| 537 |
$inputfile = shift @ARGV; |
| 538 |
|
| 539 |
if ($inputfile eq '-') { |
| 540 |
@ARGV == 1 or die "$Error Missing output file name!\n"; |
| 541 |
print "* Input file: <stdin>\n" if $::opt_debug; |
| 542 |
} |
| 543 |
else { |
| 544 |
if (! -f $inputfile) { |
| 545 |
if (-f "$inputfile.pdf") { |
| 546 |
$inputfile .= ".pdf"; |
| 547 |
} |
| 548 |
else { |
| 549 |
die "$Error Input file `$inputfile' not found!\n"; |
| 550 |
} |
| 551 |
} |
| 552 |
print "* Input file: $inputfile\n" if $::opt_debug; |
| 553 |
} |
| 554 |
|
| 555 |
### output file |
| 556 |
if (@ARGV) { |
| 557 |
$outputfile = shift @ARGV; |
| 558 |
} |
| 559 |
else { |
| 560 |
$outputfile = $inputfile; |
| 561 |
$outputfile =~ s/\.pdf$//i; |
| 562 |
$outputfile .= "-crop.pdf"; |
| 563 |
} |
| 564 |
|
| 565 |
print "* Output file: $outputfile\n" if $::opt_debug; |
| 566 |
|
| 567 |
### margins |
| 568 |
my ($llx, $lly, $urx, $ury) = (0, 0, 0, 0); |
| 569 |
if ($::opt_margins =~ |
| 570 |
/^\s*([\-\.\d]+)\s+([\-\.\d]+)\s+([\-\.\d]+)\s+([\-\.\d]+)\s*$/) { |
| 571 |
($llx, $lly, $urx, $ury) = ($1, $2, $3, $4); |
| 572 |
} |
| 573 |
else { |
| 574 |
if ($::opt_margins =~ /^\s*([\-\.\d]+)\s+([\-\.\d]+)\s*$/) { |
| 575 |
($llx, $lly, $urx, $ury) = ($1, $2, $1, $2); |
| 576 |
} |
| 577 |
else { |
| 578 |
if ($::opt_margins =~ /^\s*([\-\.\d]+)\s*$/) { |
| 579 |
($llx, $lly, $urx, $ury) = ($1, $1, $1, $1); |
| 580 |
} |
| 581 |
else { |
| 582 |
die "$Error Parse error (option --margins)!\n"; |
| 583 |
} |
| 584 |
} |
| 585 |
} |
| 586 |
print "* Margins: $llx $lly $urx $ury\n" if $::opt_debug; |
| 587 |
|
| 588 |
### papersize validation (security) |
| 589 |
if ($::opt_papersize ne '') { |
| 590 |
$::opt_papersize =~ /^[0-9A-Za-z]+$/ |
| 591 |
or die "$Error Invalid papersize ($::opt_papersize)!\n"; |
| 592 |
$papersizes{$::opt_papersize} |
| 593 |
or die "$Error Unknown papersize ($::opt_papersize)," |
| 594 |
. " see Ghostscript's documentation for option `-r'!\n"; |
| 595 |
} |
| 596 |
|
| 597 |
### resolution validation (security) |
| 598 |
if ($::opt_resolution ne '') { |
| 599 |
$::opt_resolution =~ /^\d+(x\d+)?$/ |
| 600 |
or die "$Error Invalid resolution ($::opt_resolution)," |
| 601 |
. " see Ghostscript's documentation!\n"; |
| 602 |
} |
| 603 |
|
| 604 |
### command name validation (security) |
| 605 |
my %cmd = ( |
| 606 |
'gscmd' => \$::opt_gscmd, |
| 607 |
'pdftexcmd' => \$::opt_pdftexcmd, |
| 608 |
'luatexcmd' => \$::opt_luatexcmd, |
| 609 |
'xetexcmd' => \$::opt_xetexcmd |
| 610 |
); |
| 611 |
foreach my $cmd (keys %cmd) { |
| 612 |
my $cmd_ref = $cmd{$cmd}; |
| 613 |
$$cmd_ref =~ s/^\s+//; |
| 614 |
$$cmd_ref =~ s/\s+$//; |
| 615 |
my $val = $$cmd_ref; |
| 616 |
next unless $val; |
| 617 |
next unless $val; |
| 618 |
if ($val =~ /`/) { |
| 619 |
die "$Error Forbidden backtick for option `--$cmd' ($val)!\n"; |
| 620 |
} |
| 621 |
if ($val =~ /\s/) { |
| 622 |
my $err = 0; |
| 623 |
if ($restricted or not $Win) { |
| 624 |
$err = 1; |
| 625 |
} |
| 626 |
else { |
| 627 |
$err = 1 if $val =~ /\.exe.*\s/i; |
| 628 |
$err = 1 if $val =~ /\s[-\/@+]/; |
| 629 |
} |
| 630 |
die "$Error Forbidden whitespace for option `--$cmd' ($val)!\n" if $err; |
| 631 |
} |
| 632 |
} |
| 633 |
if ($restricted) { |
| 634 |
if ($::opt_pdftexcmd and $::opt_pdftexcmd ne 'pdftex') { |
| 635 |
die "$Error pdfTeX program name must not be changed in restricted mode!\n"; |
| 636 |
} |
| 637 |
if ($::opt_xetexcmd and $::opt_xetexcmd ne 'xetex') { |
| 638 |
die "$Error XeTeX program name must not be changed in restricted mode!\n"; |
| 639 |
} |
| 640 |
if ($::opt_luatexcmd and $::opt_luatexcmd ne 'luatex') { |
| 641 |
die "$Error LuaTeX program name must not be changed in restricted mode!\n"; |
| 642 |
} |
| 643 |
if ($::opt_gscmd) { |
| 644 |
$::opt_gscmd =~ /^(gs|mgs|gswin32c|gs386|gsos2|gswin64c)$/ |
| 645 |
or $::opt_gscmd =~ /^gs[\-_]?(\d|\d[\.-_]?\d\d)c?$/ |
| 646 |
or die "$Error: Invalid Ghostscript program name in restricted mode!\n"; |
| 647 |
} |
| 648 |
} |
| 649 |
if ($Win and $::opt_gscmd =~ /\s/) { |
| 650 |
$::opt_gscmd = "\"$::opt_gscmd\""; |
| 651 |
} |
| 652 |
|
| 653 |
### cleanup system |
| 654 |
my @unlink_files = (); |
| 655 |
my $exit_code = 1; |
| 656 |
sub clean { |
| 657 |
print "* Cleanup\n" if $::opt_debug; |
| 658 |
if ($::opt_debug) { |
| 659 |
print "* Temporary files: @unlink_files\n"; |
| 660 |
} |
| 661 |
else { |
| 662 |
for (; @unlink_files>0; ) { |
| 663 |
unlink shift @unlink_files; |
| 664 |
} |
| 665 |
} |
| 666 |
} |
| 667 |
sub cleanup { |
| 668 |
clean(); |
| 669 |
exit($exit_code); |
| 670 |
} |
| 671 |
$SIG{'INT'} = \&cleanup; |
| 672 |
$SIG{'__DIE__'} = \&clean; |
| 673 |
|
| 674 |
### Calculation of BoundingBoxes |
| 675 |
|
| 676 |
# use safe file name for use within cmd line of gs (unknown shell: space, ...) |
| 677 |
# and XeTeX (hash, curly braces, ...) |
| 678 |
my $inputfilesafe = $inputfile; |
| 679 |
if ($inputfile eq '-') { |
| 680 |
$inputfilesafe = "$tmp-stdin.pdf"; |
| 681 |
print "* Temporary input file: $inputfilesafe\n" if $::opt_debug; |
| 682 |
push @unlink_files, $inputfilesafe; |
| 683 |
open(OUT, '>', $inputfilesafe) |
| 684 |
or die sprintf "$Error Cannot write `%s' (%s)!\n", |
| 685 |
$inputfilesafe, exterr; |
| 686 |
binmode(OUT); |
| 687 |
my $size = 0; |
| 688 |
my $len = 0; |
| 689 |
my $buf = ''; |
| 690 |
my $buf_size = 65536; |
| 691 |
do { |
| 692 |
$len = read STDIN, $buf, $buf_size; |
| 693 |
if (not defined $len) { |
| 694 |
my $msg = exterr; |
| 695 |
die "$Error Reading standard input ($msg)!\n"; |
| 696 |
} |
| 697 |
print OUT $buf |
| 698 |
or die sprintf "$Error Writing `%s` (%s)!\n", |
| 699 |
$inputfilesafe, exterr; |
| 700 |
$size += $len; |
| 701 |
} |
| 702 |
while ($len); |
| 703 |
close(OUT); |
| 704 |
print "* File size (STDIN): $size\n" if $::opt_debug; |
| 705 |
} |
| 706 |
elsif (not $inputfile =~ /^[\w\d\.\-\:\/@]+$/) { # /[\s\$~'"#{}%]/ |
| 707 |
$inputfilesafe = "$tmp-img.pdf"; |
| 708 |
push @unlink_files, $inputfilesafe; |
| 709 |
my $symlink_exists = eval { |
| 710 |
local $SIG{'__DIE__'}; |
| 711 |
symlink("", ""); |
| 712 |
1; |
| 713 |
}; |
| 714 |
print "* Input file name `$inputfile' contains special characters.\n" |
| 715 |
. "* " . ($symlink_exists ? "Link" : "Copy") |
| 716 |
. " input file to temporary file `$inputfilesafe'.\n" |
| 717 |
if $::opt_verbose; |
| 718 |
if ($symlink_exists) { |
| 719 |
symlink($inputfile, $inputfilesafe) |
| 720 |
or die sprintf "$Error Link from `%s' to `%s' failed (%s)!\n", |
| 721 |
$inputfile, $inputfilesafe, exterr; |
| 722 |
} |
| 723 |
else { |
| 724 |
use File::Copy; |
| 725 |
copy($inputfile, $inputfilesafe) |
| 726 |
or die sprintf "$Error Copy from `%s' to `%s' failed (%s)\n", |
| 727 |
$inputfile, $inputfilesafe, exterr; |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
if ($::opt_pdfversion eq 'auto') { |
| 732 |
open(IN, '<', $inputfilesafe) |
| 733 |
or die sprintf "!!! Error: Cannot open `%s' (%s)!\n", |
| 734 |
$inputfilesafe, exterr; |
| 735 |
my $buf; |
| 736 |
read(IN, $buf, 1024) |
| 737 |
or die sprintf "!!! Error: Cannot read the header of `%s' failed (%s)!\n", |
| 738 |
$inputfilesafe, exterr; |
| 739 |
close(IN); |
| 740 |
if ($buf =~ /%PDF-((\d).(\d))\s/) { |
| 741 |
$::opt_pdfversion = $1; |
| 742 |
$::opt_pdfmajorversion = $2; |
| 743 |
$::opt_pdfminorversion = $3; |
| 744 |
print "* PDF header: %PDF-$::opt_pdfversion\n" if $::opt_verbose; |
| 745 |
$::opt_pdfminorversion = 2 if ($::opt_pdfmajorversion < 2 && $::opt_pdfminorversion < 2); |
| 746 |
$::opt_pdfversion = $::opt_pdfmajorversion . '.' . $::opt_pdfminorversion; |
| 747 |
} |
| 748 |
else { |
| 749 |
die "!!! Error: Cannot find PDF header of `$inputfilesafe'!\n"; |
| 750 |
} |
| 751 |
} |
| 752 |
print '* Using PDF version: ', |
| 753 |
($::opt_pdfversion ? $::opt_pdfversion : "engine's default"), |
| 754 |
"\n" if $::opt_debug; |
| 755 |
|
| 756 |
my @gsargs = ( |
| 757 |
"-sDEVICE=bbox", |
| 758 |
"-dBATCH", |
| 759 |
"-dNOPAUSE" |
| 760 |
); |
| 761 |
push @gsargs, "-sPAPERSIZE=$::opt_papersize" if $::opt_papersize; |
| 762 |
push @gsargs, "-r$::opt_resolution" if $::opt_resolution; |
| 763 |
push @gsargs, |
| 764 |
"-c", |
| 765 |
"save", |
| 766 |
"pop", |
| 767 |
"-f", |
| 768 |
$inputfilesafe |
| 769 |
; |
| 770 |
|
| 771 |
my $tmpfile = "$tmp.tex"; |
| 772 |
push @unlink_files, $tmpfile; |
| 773 |
open(TMP, ">$tmpfile") |
| 774 |
or die sprintf "$Error Cannot write tmp file `%s' (%s)!\n", |
| 775 |
$tmpfile, exterr; |
| 776 |
print TMP <<'END_TMP'; |
| 777 |
\catcode37 14 % percent |
| 778 |
\catcode33 12 % exclam |
| 779 |
\catcode34 12 % quote |
| 780 |
\catcode35 6 % hash |
| 781 |
\catcode39 12 % apostrophe |
| 782 |
\catcode40 12 % left parenthesis |
| 783 |
\catcode41 12 % right parenthesis |
| 784 |
\catcode45 12 % minus |
| 785 |
\catcode46 12 % period |
| 786 |
\catcode60 12 % less |
| 787 |
\catcode61 12 % equals |
| 788 |
\catcode62 12 % greater |
| 789 |
\catcode64 12 % at |
| 790 |
\catcode91 12 % left square |
| 791 |
\catcode93 12 % right square |
| 792 |
\catcode96 12 % back tick |
| 793 |
\catcode123 1 % left curly brace |
| 794 |
\catcode125 2 % right curly brace |
| 795 |
\catcode126 12 % tilde |
| 796 |
\catcode`\#=6 % |
| 797 |
\escapechar=92 % |
| 798 |
\def\IfUndefined#1#2#3{% |
| 799 |
\begingroup\expandafter\expandafter\expandafter\endgroup |
| 800 |
\expandafter\ifx\csname#1\endcsname\relax |
| 801 |
#2% |
| 802 |
\else |
| 803 |
#3% |
| 804 |
\fi |
| 805 |
} |
| 806 |
END_TMP |
| 807 |
my $pdffilehex = unpack 'H*', $inputfilesafe; |
| 808 |
$pdffilehex = "\U$pdffilehex\E"; |
| 809 |
print TMP "\\def\\pdffilehex{$pdffilehex}\n"; |
| 810 |
print TMP <<'END_TMP'; |
| 811 |
\IfUndefined{pdfunescapehex}{% |
| 812 |
\begingroup |
| 813 |
\gdef\pdffile{}% |
| 814 |
\def\do#1#2{% |
| 815 |
\ifx\relax#2\relax |
| 816 |
\ifx\relax#1\relax |
| 817 |
\else |
| 818 |
\errmessage{Invalid hex string, should not happen!}% |
| 819 |
\fi |
| 820 |
\else |
| 821 |
\lccode`0="#1#2\relax |
| 822 |
\lowercase{% |
| 823 |
\xdef\pdffile{\pdffile0}% |
| 824 |
}% |
| 825 |
\expandafter\do |
| 826 |
\fi |
| 827 |
}% |
| 828 |
\expandafter\do\pdffilehex\relax\relax |
| 829 |
\endgroup |
| 830 |
}{% |
| 831 |
\edef\pdffile{\pdfunescapehex{\pdffilehex}}% |
| 832 |
} |
| 833 |
\immediate\write-1{Input file: \pdffile} |
| 834 |
END_TMP |
| 835 |
if ($::opt_tex eq 'luatex') { |
| 836 |
print TMP <<'END_TMP'; |
| 837 |
\begingroup\expandafter\expandafter\expandafter\endgroup |
| 838 |
\expandafter\ifx\csname directlua\endcsname\relax |
| 839 |
\errmessage{LuaTeX not found!}% |
| 840 |
\else |
| 841 |
\fi |
| 842 |
END_TMP |
| 843 |
} |
| 844 |
|
| 845 |
my $uncompress = $::opt_uncompress ? '0 ' : '9 '; |
| 846 |
if ($::opt_tex eq 'pdftex') { |
| 847 |
print TMP "\\pdfcompresslevel=$uncompress "; |
| 848 |
print TMP <<'END_TMP_HEAD'; |
| 849 |
\pdfoutput=1 % |
| 850 |
\csname pdfmapfile\endcsname{} |
| 851 |
\def\setpdfversion#1#2{% |
| 852 |
\IfUndefined{pdfobjcompresslevel}{% |
| 853 |
}{% |
| 854 |
\ifnum#1=1 % |
| 855 |
\ifnum#2<5 |
| 856 |
\pdfobjcompresslevel=0 % |
| 857 |
\else |
| 858 |
\pdfobjcompresslevel=2 % |
| 859 |
\fi |
| 860 |
\fi |
| 861 |
}% |
| 862 |
\IfUndefined{pdfminorversion}{% |
| 863 |
\IfUndefined{pdfoptionpdfminorversion}{% |
| 864 |
}{% |
| 865 |
\pdfoptionpdfminorversion=#2\relax |
| 866 |
}% |
| 867 |
}{% |
| 868 |
\pdfminorversion=#2\relax |
| 869 |
\IfUndefined{pdfmajorversion}{% |
| 870 |
\ifnum#2=0 \pdfminorversion=5\fi} |
| 871 |
{\pdfmajorversion=#1\relax}% |
| 872 |
}% |
| 873 |
} |
| 874 |
\def\page #1 [#2 #3 #4 #5]{% |
| 875 |
\count0=#1\relax |
| 876 |
\setbox0=\hbox{% |
| 877 |
\pdfximage page #1 mediabox{\pdffile}% |
| 878 |
\pdfrefximage\pdflastximage |
| 879 |
}% |
| 880 |
\pdfhorigin=-#2bp\relax |
| 881 |
\pdfvorigin=#3bp\relax |
| 882 |
\pdfpagewidth=#4bp\relax |
| 883 |
\advance\pdfpagewidth by -#2bp\relax |
| 884 |
\pdfpageheight=#5bp\relax |
| 885 |
\advance\pdfpageheight by -#3bp\relax |
| 886 |
\ht0=\pdfpageheight |
| 887 |
\shipout\box0\relax |
| 888 |
} |
| 889 |
\def\pageclip #1 [#2 #3 #4 #5][#6 #7 #8 #9]{% |
| 890 |
\count0=#1\relax |
| 891 |
\dimen0=#4bp\relax \advance\dimen0 by -#2bp\relax |
| 892 |
\edef\imagewidth{\the\dimen0}% |
| 893 |
\dimen0=#5bp\relax \advance\dimen0 by -#3bp\relax |
| 894 |
\edef\imageheight{\the\dimen0}% |
| 895 |
\pdfximage page #1 mediabox{\pdffile}% |
| 896 |
\setbox0=\hbox{% |
| 897 |
\kern -#2bp\relax |
| 898 |
\lower #3bp\hbox{\pdfrefximage\pdflastximage}% |
| 899 |
}% |
| 900 |
\wd0=\imagewidth\relax |
| 901 |
\ht0=\imageheight\relax |
| 902 |
\dp0=0pt\relax |
| 903 |
\pdfhorigin=#6pt\relax |
| 904 |
\pdfvorigin=#7bp\relax |
| 905 |
\pdfpagewidth=\imagewidth |
| 906 |
\advance\pdfpagewidth by #6bp\relax |
| 907 |
\advance\pdfpagewidth by #8bp\relax |
| 908 |
\pdfpageheight=\imageheight\relax |
| 909 |
\advance\pdfpageheight by #7bp\relax |
| 910 |
\advance\pdfpageheight by #9bp\relax |
| 911 |
\pdfxform0\relax |
| 912 |
\shipout\hbox{\pdfrefxform\pdflastxform}% |
| 913 |
}% |
| 914 |
\def\pageinclude#1{% |
| 915 |
\pdfhorigin=0pt\relax |
| 916 |
\pdfvorigin=0pt\relax |
| 917 |
\pdfximage page #1 mediabox{\pdffile}% |
| 918 |
\setbox0=\hbox{\pdfrefximage\pdflastximage}% |
| 919 |
\pdfpagewidth=\wd0\relax |
| 920 |
\pdfpageheight=\ht0\relax |
| 921 |
\advance\pdfpageheight by \dp0\relax |
| 922 |
\shipout\hbox{% |
| 923 |
\raise\dp0\box0\relax |
| 924 |
}% |
| 925 |
} |
| 926 |
END_TMP_HEAD |
| 927 |
print TMP "\\setpdfversion{$::opt_pdfmajorversion}{$::opt_pdfminorversion}\n" if $::opt_pdfversion; |
| 928 |
} |
| 929 |
elsif ($::opt_tex eq 'luatex') |
| 930 |
{ |
| 931 |
print TMP "\\pdfvariable compresslevel=$uncompress "; |
| 932 |
print TMP <<'END_TMP_HEAD'; |
| 933 |
\outputmode=1 % |
| 934 |
\pdfextension mapfile {} |
| 935 |
\def\setpdfversion#1#2{% |
| 936 |
\ifnum#1=1 % |
| 937 |
\ifnum#2<5 |
| 938 |
\pdfvariable objcompresslevel=0 % |
| 939 |
\else |
| 940 |
\pdfvariable objcompresslevel=2 % |
| 941 |
\fi |
| 942 |
\fi |
| 943 |
\pdfvariable minorversion= #2 |
| 944 |
\pdfvariable majorversion= #1 |
| 945 |
} |
| 946 |
\def\page #1 [#2 #3 #4 #5]{% |
| 947 |
\count0=#1\relax |
| 948 |
\setbox0=\hbox{% |
| 949 |
\saveimageresource page #1 mediabox{\pdffile}% |
| 950 |
\useimageresource\lastsavedimageresourceindex |
| 951 |
}% |
| 952 |
\pdfvariable horigin=-#2bp\relax |
| 953 |
\pdfvariable vorigin=#3bp\relax |
| 954 |
\pagewidth=#4bp\relax |
| 955 |
\advance\pagewidth by -#2bp\relax |
| 956 |
\pageheight=#5bp\relax |
| 957 |
\advance\pageheight by -#3bp\relax |
| 958 |
\ht0=\pageheight |
| 959 |
\shipout\box0\relax |
| 960 |
} |
| 961 |
\def\pageclip #1 [#2 #3 #4 #5][#6 #7 #8 #9]{% |
| 962 |
\count0=#1\relax |
| 963 |
\dimen0=#4bp\relax \advance\dimen0 by -#2bp\relax |
| 964 |
\edef\imagewidth{\the\dimen0}% |
| 965 |
\dimen0=#5bp\relax \advance\dimen0 by -#3bp\relax |
| 966 |
\edef\imageheight{\the\dimen0}% |
| 967 |
\saveimageresource page #1 mediabox{\pdffile}% |
| 968 |
\setbox0=\hbox{% |
| 969 |
\kern -#2bp\relax |
| 970 |
\lower #3bp\hbox{\useimageresource\lastsavedimageresourceindex}% |
| 971 |
}% |
| 972 |
\wd0=\imagewidth\relax |
| 973 |
\ht0=\imageheight\relax |
| 974 |
\dp0=0pt\relax |
| 975 |
\pdfvariable horigin=#6pt\relax |
| 976 |
\pdfvariable vorigin=#7bp\relax |
| 977 |
\pagewidth=\imagewidth |
| 978 |
\advance\pagewidth by #6bp\relax |
| 979 |
\advance\pagewidth by #8bp\relax |
| 980 |
\pageheight=\imageheight\relax |
| 981 |
\advance\pageheight by #7bp\relax |
| 982 |
\advance\pageheight by #9bp\relax |
| 983 |
\saveboxresource0\relax |
| 984 |
\shipout\hbox{\useboxresource\lastsavedboxresourceindex}% |
| 985 |
}% |
| 986 |
\def\pageinclude#1{% |
| 987 |
\pdfvariable horigin=0pt\relax |
| 988 |
\pdfvariable vorigin=0pt\relax |
| 989 |
\saveimageresource page #1 mediabox{\pdffile}% |
| 990 |
\setbox0=\hbox{\useimageresource\lastsavedimageresourceindex}% |
| 991 |
\pagewidth=\wd0\relax |
| 992 |
\pageheight=\ht0\relax |
| 993 |
\advance\pageheight by \dp0\relax |
| 994 |
\shipout\hbox{% |
| 995 |
\raise\dp0\box0\relax |
| 996 |
}% |
| 997 |
} |
| 998 |
END_TMP_HEAD |
| 999 |
print TMP "\\setpdfversion{$::opt_pdfmajorversion}{$::opt_pdfminorversion}\n" if $::opt_pdfversion; |
| 1000 |
} |
| 1001 |
else { # XeTeX |
| 1002 |
print TMP <<'END_TMP_HEAD_A'; |
| 1003 |
\expandafter\ifx\csname XeTeXpdffile\endcsname\relax |
| 1004 |
\errmessage{XeTeX not found or too old!}% |
| 1005 |
\fi |
| 1006 |
\def\setpdfversion#1#2{% |
| 1007 |
\special{pdf:majorversion #1}% |
| 1008 |
\special{pdf:minorversion #2}} |
| 1009 |
|
| 1010 |
\def\page #1 [#2 #3 #4 #5]{% |
| 1011 |
\count0=#1\relax |
| 1012 |
\setbox0=\hbox{% |
| 1013 |
\XeTeXpdffile "\pdffile" page #1 media\relax |
| 1014 |
}% |
| 1015 |
\pdfpagewidth=#4bp\relax |
| 1016 |
\advance\pdfpagewidth by -#2bp\relax |
| 1017 |
\pdfpageheight=#5bp\relax |
| 1018 |
\advance\pdfpageheight by -#3bp\relax |
| 1019 |
\shipout\hbox{% |
| 1020 |
END_TMP_HEAD_A |
| 1021 |
print TMP "\\setpdfversion{$::opt_pdfmajorversion}{$::opt_pdfminorversion}%\n" if $::opt_pdfversion; |
| 1022 |
print TMP <<'END_TMP_HEAD_B'; |
| 1023 |
\kern-1in% |
| 1024 |
\kern-#2bp% |
| 1025 |
\vbox{% |
| 1026 |
\kern-1in% |
| 1027 |
\kern#3bp% |
| 1028 |
\ht0=\pdfpageheight |
| 1029 |
\box0 % |
| 1030 |
}% |
| 1031 |
}% |
| 1032 |
} |
| 1033 |
\def\pageclip #1 [#2 #3 #4 #5][#6 #7 #8 #9]{% |
| 1034 |
\page {#1} [#2 #3 #4 #5]% |
| 1035 |
} |
| 1036 |
\def\pageinclude#1{% |
| 1037 |
\setbox0=\hbox{% |
| 1038 |
\XeTeXpdffile "\pdffile" page #1 media\relax |
| 1039 |
}% |
| 1040 |
\pdfpagewidth=\wd0\relax |
| 1041 |
\pdfpageheight=\ht0\relax |
| 1042 |
\advance\pdfpageheight by \dp0\relax |
| 1043 |
\shipout\hbox{% |
| 1044 |
\kern-1in% |
| 1045 |
\vbox{% |
| 1046 |
\kern-1in% |
| 1047 |
\ht0=\pdfpageheight |
| 1048 |
\box0 % |
| 1049 |
}% |
| 1050 |
}% |
| 1051 |
} |
| 1052 |
END_TMP_HEAD_B |
| 1053 |
} |
| 1054 |
|
| 1055 |
print "* Running Ghostscript for BoundingBox calculation ...\n" |
| 1056 |
if $::opt_verbose; |
| 1057 |
print "* Ghostscript call: $::opt_gscmd @gsargs\n" if $::opt_debug; |
| 1058 |
|
| 1059 |
my @bbox; |
| 1060 |
my @bbox_all; |
| 1061 |
my @bbox_odd; |
| 1062 |
my @bbox_even; |
| 1063 |
if ($::opt_bbox) { |
| 1064 |
$::opt_bbox =~ /([-\d\.]+) ([-\d\.]+) ([-\d\.]+) ([-\d\.]+)/; |
| 1065 |
@bbox_all = ($1, $2, $3, $4); |
| 1066 |
} |
| 1067 |
if ($::opt_bbox_odd) { |
| 1068 |
$::opt_bbox_odd =~ /([-\d\.]+) ([-\d\.]+) ([-\d\.]+) ([-\d\.]+)/; |
| 1069 |
@bbox_odd = ($1, $2, $3, $4); |
| 1070 |
} |
| 1071 |
if ($::opt_bbox_even) { |
| 1072 |
$::opt_bbox_even =~ /([-\d\.]+) ([-\d\.]+) ([-\d\.]+) ([-\d\.]+)/; |
| 1073 |
@bbox_even = ($1, $2, $3, $4); |
| 1074 |
} |
| 1075 |
|
| 1076 |
sub getbbox ($$$$$) { |
| 1077 |
my $page = shift; |
| 1078 |
my $a = shift; |
| 1079 |
my $b = shift; |
| 1080 |
my $c = shift; |
| 1081 |
my $d = shift; |
| 1082 |
if ($page % 2 == 1) { |
| 1083 |
if ($::opt_bbox_odd) { |
| 1084 |
return @bbox_odd; |
| 1085 |
} |
| 1086 |
} |
| 1087 |
else { |
| 1088 |
if ($::opt_bbox_even) { |
| 1089 |
return @bbox_even; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
if ($::opt_bbox) { |
| 1093 |
return @bbox_all; |
| 1094 |
} |
| 1095 |
return ($a, $b, $c, $d); |
| 1096 |
} |
| 1097 |
my $page = 0; |
| 1098 |
my $gs_pipe = "$::opt_gscmd -dSAFER @gsargs 2>&1"; |
| 1099 |
$gs_pipe .= " 1>$null" unless $::opt_verbose; |
| 1100 |
$gs_pipe .= " |"; |
| 1101 |
|
| 1102 |
open(GS, $gs_pipe) |
| 1103 |
or die sprintf "$Error Cannot call Ghostscript: `%s' (%s)!\n", |
| 1104 |
$::opt_gscmd, exterr; |
| 1105 |
my $bb = ($::opt_hires) ? "%%HiResBoundingBox" : "%%BoundingBox"; |
| 1106 |
my $previous_line = 'Previous line'; |
| 1107 |
# Ghostscript workaround for buggy ports that prints |
| 1108 |
# the bounding box data twice on STDERR. |
| 1109 |
while (<GS>) { |
| 1110 |
print $_ if $::opt_verbose; |
| 1111 |
if (/^$bb:\s*(-?[\.\d]+) (-?[\.\d]+) (-?[\.\d]+) (-?[\.\d]+)/o) { |
| 1112 |
} |
| 1113 |
else { |
| 1114 |
$previous_line = $_; |
| 1115 |
next; |
| 1116 |
} |
| 1117 |
next if $previous_line eq $_; |
| 1118 |
$previous_line = $_; |
| 1119 |
$page++; |
| 1120 |
@bbox = getbbox($page, $1, $2, $3, $4); |
| 1121 |
|
| 1122 |
my $empty = 0; |
| 1123 |
$empty = 1 if $bbox[0] >= $bbox[2]; |
| 1124 |
$empty = 1 if $bbox[1] >= $bbox[3]; |
| 1125 |
if ($empty) { |
| 1126 |
print <<"END_WARNING"; |
| 1127 |
|
| 1128 |
!!! Warning: Empty Bounding Box is returned by Ghostscript! |
| 1129 |
!!! Page $page: @bbox |
| 1130 |
!!! Either there is a problem with the page or with Ghostscript. |
| 1131 |
!!! Recovery is tried by embedding the page in its original size. |
| 1132 |
|
| 1133 |
END_WARNING |
| 1134 |
print TMP "\\pageinclude{$page}\n"; |
| 1135 |
next; |
| 1136 |
} |
| 1137 |
|
| 1138 |
print "* Page $page: @bbox\n" if $::opt_verbose; |
| 1139 |
|
| 1140 |
my @bb = ($bbox[0] - $llx, $bbox[1] - $ury, |
| 1141 |
$bbox[2] + $urx, $bbox[3] + $lly); |
| 1142 |
|
| 1143 |
$empty = 0; |
| 1144 |
$empty = 1 if $bb[0] >= $bb[2]; |
| 1145 |
$empty = 1 if $bb[1] >= $bb[3]; |
| 1146 |
if ($empty) { |
| 1147 |
print <<"END_WARNING"; |
| 1148 |
|
| 1149 |
!!! Warning: The final Bounding Box is empty! |
| 1150 |
!!! Page: $page: @bb |
| 1151 |
!!! Probably caused by too large negative margin values. |
| 1152 |
!!! Recovery by ignoring margin values. |
| 1153 |
|
| 1154 |
END_WARNING |
| 1155 |
print TMP "\\page $page [@bbox]\n"; |
| 1156 |
# clipping shouldn't make a difference |
| 1157 |
next; |
| 1158 |
} |
| 1159 |
if ($::opt_clip) { |
| 1160 |
print TMP "\\pageclip $page [@bbox][$llx $lly $urx $ury]\n"; |
| 1161 |
} |
| 1162 |
else { |
| 1163 |
print TMP "\\page $page [@bb]\n"; |
| 1164 |
} |
| 1165 |
} |
| 1166 |
close(GS); |
| 1167 |
|
| 1168 |
if ($? == -1) { |
| 1169 |
die sprintf "$Error Ghostscript failed to execute (%s)!\n", |
| 1170 |
exterr; |
| 1171 |
} |
| 1172 |
elsif ($? & 127) { |
| 1173 |
die sprintf "$Error Ghostscript died with signal %d!\n", |
| 1174 |
($? & 127); |
| 1175 |
} |
| 1176 |
elsif ($? != 0) { |
| 1177 |
die sprintf "$Error Ghostscript exited with error code %d!\n", |
| 1178 |
$? >> 8; |
| 1179 |
} |
| 1180 |
|
| 1181 |
print TMP "\\csname \@\@end\\endcsname\n\\end\n"; |
| 1182 |
close(TMP); |
| 1183 |
|
| 1184 |
if ($page == 0) { |
| 1185 |
die "$Error Ghostscript does not report bounding boxes!\n"; |
| 1186 |
} |
| 1187 |
|
| 1188 |
### Run pdfTeX/XeTeX |
| 1189 |
|
| 1190 |
push @unlink_files, "$tmp.log"; |
| 1191 |
my $cmd; |
| 1192 |
my $texname; |
| 1193 |
if ($::opt_tex eq 'pdftex') { |
| 1194 |
$cmd = $::opt_pdftexcmd; |
| 1195 |
$texname = 'pdfTeX'; |
| 1196 |
} |
| 1197 |
elsif ($::opt_tex eq 'luatex') { |
| 1198 |
$cmd = $::opt_luatexcmd; |
| 1199 |
$texname = 'LuaTeX'; |
| 1200 |
} |
| 1201 |
else { |
| 1202 |
$cmd = $::opt_xetexcmd; |
| 1203 |
$texname = 'XeTeX'; |
| 1204 |
} |
| 1205 |
$cmd = "\"$cmd\"" if $Win and $cmd =~ /\s/; |
| 1206 |
$cmd .= ' -no-shell-escape'; |
| 1207 |
if ($::opt_initex) { |
| 1208 |
$cmd .= ' --ini --etex'; |
| 1209 |
} |
| 1210 |
if ($::opt_verbose) { |
| 1211 |
$cmd .= " -interaction=nonstopmode $tmp"; |
| 1212 |
} |
| 1213 |
else { |
| 1214 |
$cmd .= " -interaction=batchmode $tmp"; |
| 1215 |
} |
| 1216 |
print "* Running $texname ...\n" if $::opt_verbose; |
| 1217 |
print "* $texname call: $cmd\n" if $::opt_debug; |
| 1218 |
if ($::opt_verbose) { |
| 1219 |
system($cmd); |
| 1220 |
} |
| 1221 |
else { |
| 1222 |
`$cmd`; |
| 1223 |
} |
| 1224 |
if ($? == -1) { |
| 1225 |
die sprintf "$Error %s run failed to execute (%s)!\n", |
| 1226 |
$texname, exterr; |
| 1227 |
} |
| 1228 |
elsif ($? & 127) { |
| 1229 |
die sprintf "$Error %s run died with signal %d!\n", |
| 1230 |
$texname, ($? & 127); |
| 1231 |
} |
| 1232 |
elsif ($? != 0) { |
| 1233 |
die sprintf "$Error %s run failed with value %d!\n", |
| 1234 |
$texname, ($? >> 8); |
| 1235 |
} |
| 1236 |
|
| 1237 |
### Check pdf version of temp file |
| 1238 |
if ($::opt_pdfversion) { |
| 1239 |
open(PDF, '+<', "$tmp.pdf") |
| 1240 |
or die sprintf "!!! Error: Cannot open `%s' (%s)!\n", |
| 1241 |
"$tmp.pdf", exterr; |
| 1242 |
my $header; |
| 1243 |
read PDF, $header, 9 |
| 1244 |
or die sprintf "!!! Error: Cannot read header of `%s' (%s)!\n", |
| 1245 |
"$tmp.pdf", exterr; |
| 1246 |
$header =~ /^%PDF-(\d\.\d)\s$/ or die "!!! Error: Cannot find header of `$tmp.pdf'!\n"; |
| 1247 |
if ($1 ne $::opt_pdfversion) { |
| 1248 |
seek PDF, 5, 0 |
| 1249 |
or die sprintf "!!! Error: Cannot seek in `%s' (%s)!\n", |
| 1250 |
"$tmp.pdf", exterr; |
| 1251 |
print PDF $::opt_pdfversion |
| 1252 |
or die sprintf "!!! Error: Cannot write in `%s' (%s)!\n", |
| 1253 |
"$tmp.pdf", exterr; |
| 1254 |
print "* PDF version correction in output file: $::opt_pdfversion\n" |
| 1255 |
if $::opt_debug; |
| 1256 |
} |
| 1257 |
close(PDF); |
| 1258 |
} |
| 1259 |
|
| 1260 |
### Move temp file to output |
| 1261 |
if (!rename("$tmp.pdf", $outputfile)) { |
| 1262 |
use File::Copy; |
| 1263 |
move "$tmp.pdf", $outputfile or |
| 1264 |
die "$Error Cannot move `$tmp.pdf' to `$outputfile'!\n"; |
| 1265 |
} |
| 1266 |
|
| 1267 |
print "==> $page page", (($page == 1) ? "" : "s"), |
| 1268 |
" written on `$outputfile'.\n" if ! $::opt_quiet; |
| 1269 |
|
| 1270 |
$exit_code = 0; |
| 1271 |
cleanup(); |
| 1272 |
|
| 1273 |
__END__ |