| 1 |
#!/usr/bin/perl |
| 2 |
|
| •3 |
=head1 NAME |
| 4 |
|
| 5 |
pdfbklt.plx - Turns documents into booklets |
| 6 |
|
| 7 |
=head1 DESCRIPTION |
| 8 |
|
| 9 |
Converts a PDF file into a booklet. It edits the pdffile to add the |
| 10 |
modifications at the end. |
| 11 |
|
| 12 |
=head1 SYNOPSIS |
| 13 |
|
| 14 |
PDFBKLT [-b num/size] [-p num] [-q] [-r] [-s size] pdffile |
| 15 |
|
| 16 |
-b num/size Specifies which page contains the output page size details [1] |
| 17 |
or gives the dimensions of the page in pts (x;y) or A4,ltr,lgl,A5 |
| 18 |
-l Flag to indicate linear scaling |
| 19 |
-p num Specifies the number of pages on the output page (1, 2, 4) [2] |
| 20 |
If 4, can be 4s to flip on short edge |
| 21 |
-q Quiet (no on screen messages) |
| 22 |
-r Rotates the output (-p 2 rotates automatically, -r rotates back) |
| 23 |
-s size Specifies the the location of the actual text on a page: |
| 24 |
2 half page right and left (big gutter) |
| 25 |
2r half page always on right |
| 26 |
2l half page always on left |
| 27 |
4 1/4 page right and left bottom (very big gutter) |
| 28 |
4t 1/4 page right and left top (very big gutter) |
| 29 |
4r/4l/4rt/4lt 1/4 page always on right or left bottom or top |
| 30 |
location as a single string: minx,miny,maxx,maxy in pts |
| 31 |
|
| 32 |
=cut |
| 33 |
|
| 34 |
use Text::PDF::File; |
| 35 |
use Text::PDF::Utils; |
| 36 |
use Getopt::Std; |
| 37 |
|
| 38 |
|
| 39 |
$version = "1.8"; # MJPH 17-AUG-2009 add -u gutter for -p 2 |
| 40 |
# $version = "1.7"; # MJPH 29-FEB-2008 add -g -p r, fix contents problem for empty pages |
| 41 |
# $version = "1.6"; # MJPH 13-JAN-2003 -s2b option supported |
| 42 |
# $version = "1.505"; # MJPH 3-AUG-2002 -s comma separated. Allow -ve values in -s |
| 43 |
# merge errors store something, at least! |
| 44 |
# $version = "1.504"; # MJPH 27-JUN-2002 Use CropBox over MediaBox |
| 45 |
# $version = "1.503"; # MJPH 19-FEB-2002 Fix -p1 positioning (again!) |
| 46 |
# $version = "1.502"; # MJPH 18-JUN-2001 Add support for -p4s |
| 47 |
# $version = "1.501"; # MJPH 2-MAY-2001 Correct positioning of -s;;; type pages |
| 48 |
# $version = "1.500"; # MJPH 26-JUL-2000 Correct positioning in some cases and add landscape sizes |
| 49 |
# $version = "1.401"; # MJPH 28-JUN-2000 Debug content lists and -r |
| 50 |
# $version = "1.4"; # MJPH 1-FEB-2000 Add -l and preset paper sizes |
| 51 |
# $version = "1.302"; # MJPH 11-DEC-1999 Make sure updated root output |
| 52 |
# $version = "1.302"; # MJPH 30-NOV-1999 Update to use Text::PDF |
| 53 |
# $version = "1.300"; # MJPH 15-JUN-1999 Remove outlines, etc. when making booklet |
| 54 |
# $version = "1.201"; # MJPH 29-DEC-1998 Add test in merge_dict for $v2 not present |
| 55 |
# $version = "1.200"; # MJPH 6-NOV-1998 Merging external resources and change -r |
| 56 |
# $version = "1.101"; # MJPH 13-OCT-1998 Debug resource merging not being output |
| 57 |
# $version = "1.100"; # MJPH 3-AUG-1998 Support new PDF library |
| 58 |
|
| 59 |
getopts("b:g:h:lp:qrs:u:"); |
| 60 |
|
| 61 |
if (!defined $ARGV[0]) |
| 62 |
{ |
| 63 |
die <<"EOT"; |
| 64 |
PDFBKLT [-b num/size] [-l] [-p num] [-g num] [-q] [-r] [-s size] pdffile |
| 65 |
|
| 66 |
(c) M. Hosken. Version: $version |
| 67 |
|
| 68 |
Converts a PDF file into a booklet. It edits the pdffile to add the |
| 69 |
modifications at the end. |
| 70 |
|
| 71 |
-b num/size Specifies which page contains the output page size details [1] |
| 72 |
or gives the dimensions of the page in pts (x;y) or A4,ltr,lgl,A5 |
| 73 |
-g num[;offset] Makes up page groups, or signatures with num pages in each |
| 74 |
and offset pages in the first signature. |
| 75 |
num and offset must be multiples of 4 |
| 76 |
-l Flag to indicate linear scaling |
| 77 |
-p num Specifies the number of pages on the output page (1, 2, 4) [2] |
| 78 |
If 4, can be 4s to flip on short edge. Append r if right to left. |
| 79 |
-q Quiet (no on screen messages) |
| 80 |
-r Rotates the output (-p 2 rotates automatically, -r rotates back) |
| 81 |
-s size Specifies the the location of the actual text on a page: |
| 82 |
2 half page right and left (big gutter) |
| 83 |
2r half page always on right |
| 84 |
2l half page always on left |
| 85 |
2b dual half pages on right and left |
| 86 |
assumes last page opposite first |
| 87 |
4 1/4 page right and left bottom (very big gutter) |
| 88 |
4t 1/4 page right and left top (very big gutter) |
| 89 |
4r/4l/4rt/4lt 1/4 page always on right or left bottom or top |
| 90 |
location as a single string: minx,miny,maxx,maxy in pts |
| 91 |
-u width Specify the gutter size (for 2 pages per output page only) |
| 92 |
EOT |
| 93 |
} |
| 94 |
|
| 95 |
%sizes=( |
| 96 |
'a4' => '595;842', |
| 97 |
'a4l' => '842;595', |
| 98 |
'ltr' => '612;792', |
| 99 |
'ltrl' => '792;612', |
| 100 |
'lgl' => '612;1008', |
| 101 |
'lgll' => '1008;612', |
| 102 |
); |
| 103 |
$opt_u = 0 unless defined $opt_u; |
| 104 |
$opt_b = $sizes{lc($opt_b)} if defined $sizes{lc($opt_b)}; |
| 105 |
print "Reading file\n" unless $opt_q; |
| 106 |
$p = Text::PDF::File->open($ARGV[0], 1) || die "Can't open $ARGV[0]"; |
| 107 |
$r = $p->{'Root'}->realise; |
| 108 |
$pgs = $r->{'Pages'}->realise; |
| 109 |
$pgcount = $pgs->{'Count'}->val; |
| 110 |
|
| 111 |
foreach (qw(Outlines Dests Threads AcroForm PageLabels StructTreeRoot OpenAction PageMode)) |
| 112 |
{ |
| 113 |
if (defined $r->{$_}) |
| 114 |
{ |
| 115 |
delete $r->{$_}; |
| 116 |
$p->out_obj($r); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
$pgcount = 0; |
| 121 |
$rtl = ($opt_p =~ s/r$//o); |
| 122 |
proc_pages($pgs); |
| 123 |
|
| 124 |
if ($opt_s =~ m/b$/o) |
| 125 |
{ |
| 126 |
for ($i = 0; $i < $pgcount; $i++) |
| 127 |
{ |
| 128 |
my ($pnum) = ($i == 0) ? 0 : 2 * $i - 1; |
| 129 |
my ($ref) = $pglist[$pnum]->copy($p, undef, 1, $p, 'clip' => ["^/Contents", '^/Resources/[^/]+/.*']); |
| 130 |
my ($npnum) = ($i == 0) ? -1 : $pnum + 1; |
| 131 |
$r->{'Pages'}->add_page($ref, $npnum); |
| 132 |
if ($npnum == -1) |
| 133 |
{ |
| 134 |
push (@pglist, $ref); |
| 135 |
$pglist[$npnum]{' pnum'} = $pgcount * 2; |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
splice(@pglist, $npnum, 0, $ref); |
| 140 |
$pglist[$npnum]{' pnum'} = $npnum; |
| 141 |
} |
| 142 |
$ref->{' pnum'} = $pnum; |
| 143 |
print STDERR '.' unless $opt_q; |
| 144 |
} |
| 145 |
$pgcount *= 2; |
| 146 |
# $p->append_file; |
| 147 |
# exit(1); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
$opt_p = 2 unless defined $opt_p; |
| 152 |
$opt_r = !$opt_r if (($opt_p == 2 && $opt_s !~ /^2[rltb]*$/oi) || ($opt_p != 2 && $opt_s =~ /^2[rlt]*$/oi)); |
| 153 |
|
| 154 |
if ($opt_b =~ m/^([0-9]+)\;([0-9]+)/oi) |
| 155 |
{ @pbox = (0, 0, $1, $2); } |
| 156 |
else |
| 157 |
{ |
| 158 |
$opt_b = 1 unless defined $opt_b; |
| 159 |
$opt_b--; |
| 160 |
|
| 161 |
foreach $n ($pglist[$opt_b]->find_prop('MediaBox')->elementsof) |
| 162 |
{ push(@pbox, $n->val); } |
| 163 |
} |
| 164 |
|
| 165 |
$fpgins = PDFDict(); $p->new_obj($fpgins); |
| 166 |
$spgins = PDFDict(); $p->new_obj($spgins); |
| 167 |
$fpgins->{' stream'} = "q"; |
| 168 |
$spgins->{' stream'} = "Q"; |
| 169 |
|
| 170 |
unless ($opt_q) |
| 171 |
{ |
| 172 |
print "\nThere are $pgcount pages\n"; |
| 173 |
print "Page box (pt) = $pbox[0], $pbox[1], $pbox[2], $pbox[3]\n"; |
| 174 |
} |
| 175 |
|
| 176 |
if ($opt_g) |
| 177 |
{ |
| 178 |
my ($num, $offset) = split(/[\s;,:]/, $opt_g); |
| 179 |
my ($i); |
| 180 |
$offset ||= $num; |
| 181 |
for ($i = $offset; $i < $pgcount; $i += $num) |
| 182 |
{ push (@jobs, $i); } |
| 183 |
} |
| 184 |
push(@jobs, ($opt_p == 1) ? $pgcount : int(($pgcount + 3) / 4) * 4); |
| 185 |
if ($opt_p > 1) |
| 186 |
{ |
| 187 |
for ($i = $pgcount; $i < int(($pgcount + 3) /4) * 4; $i++) |
| 188 |
{ |
| 189 |
if ($rtl) |
| 190 |
{ unshift (@pglist, undef); } |
| 191 |
else |
| 192 |
{ push (@pglist, undef); } |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
for ($jn = 0; $jn < scalar @jobs; $jn++) |
| 197 |
{ |
| 198 |
my ($first) = $jn ? $jobs[$jn - 1] : 0; |
| 199 |
my ($last) = $jobs[$jn]; |
| 200 |
|
| 201 |
for ($i = 0; $i < ($last - $first) / $opt_p; $i++) |
| 202 |
{ |
| 203 |
if ($opt_p == 1) |
| 204 |
{ |
| 205 |
next if $i >= $pgcount; |
| 206 |
@pl = ($pglist[$i + $first]); |
| 207 |
$m = $i + 1; |
| 208 |
} |
| 209 |
elsif ($opt_p == 2) |
| 210 |
{ |
| 211 |
@pl = ($pglist[$i + $first], $pglist[$last - $i - 1]); |
| 212 |
@pl = ($pl[1], $pl[0]) if ($i & 1); |
| 213 |
$m = ($first + $i + 1) . ", " . ($last - $i); |
| 214 |
} else # $opt_p == 4 |
| 215 |
{ |
| 216 |
@pl = ($pglist[2 * $i + $first]); |
| 217 |
# do these in a special order with increasing difficulty of passing if() requirement |
| 218 |
push (@pl, $pglist[2 * $i + 1 + $first]) if (2 * $i + 1 + $first < $pgcount); |
| 219 |
push (@pl, $pglist[$last - 2 * $i - 2]) if ($last - 2 * $i - 2 < $pgcount); |
| 220 |
push (@pl, $pglist[$last - 2 * $i - 1]) if ($last - 2 * $i <= $pgcount); |
| 221 |
$m = (2 * $i + 1 + $first) . ", " . (2 * $i + 2 + $first) . ", " . ($last - 2 * $i - 1) . ", " . ($last - 2 * $i); |
| 222 |
} |
| 223 |
|
| 224 |
print "Merging " . $m . "\n" unless $opt_q; |
| 225 |
merge_pages(@pl); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
$p->append_file; |
| 230 |
|
| 231 |
sub proc_pages |
| 232 |
{ |
| 233 |
my ($pgs) = @_; |
| 234 |
my ($pgref); |
| 235 |
|
| 236 |
foreach $pgref ($pgs->{'Kids'}->elementsof) |
| 237 |
{ |
| 238 |
print STDERR "." unless $opt_q; |
| 239 |
$pgref->realise; |
| 240 |
if ($pgref->{'Type'}->val =~ m/^Pages$/oi) |
| 241 |
{ proc_pages($pgref); } |
| 242 |
else |
| 243 |
{ |
| 244 |
if ($rtl) |
| 245 |
{ unshift (@pglist, $pgref); } |
| 246 |
else |
| 247 |
{ push (@pglist, $pgref); } |
| 248 |
$pgref->{' pnum'} = $pgcount++; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
sub merge_pages |
| 254 |
{ |
| 255 |
my (@pr) = @_; |
| 256 |
my ($s, $j, $bp); |
| 257 |
|
| 258 |
$s = undef; |
| 259 |
for ($j = 0; $j <= $#pr; $j++) |
| 260 |
{ |
| 261 |
my ($n, $is, $rl, $bt, $xs, $ys, $scale, $scalestr, $scalestrr, $id, $i); |
| 262 |
my (@slist, @s, $s1, $s2, $p2, $min, $k); |
| 263 |
next unless defined $pr[$j]; |
| 264 |
@prbox = (); @clipbox = (); |
| 265 |
foreach $n (($pr[$j]->find_prop('CropBox') || $pr[$j]->find_prop('MediaBox'))->elementsof) |
| 266 |
{ push(@prbox, $n->val); } |
| 267 |
$is = 1; |
| 268 |
if ($opt_s =~ m/^(-?[0-9]+),(-?[0-9]+),(-?[0-9]+),(-?[0-9]+)/o) |
| 269 |
{ @prbox = ($1, $2, $3, $4); } |
| 270 |
elsif ($opt_s =~ m/^([0-9])(.?)(.?)$/o) |
| 271 |
{ |
| 272 |
$is = $1; |
| 273 |
$rl = lc($2); |
| 274 |
$bt = lc($3); |
| 275 |
# $rl = ($pr[$j]->{' pnum'} & 1) ? "l" : "r" unless ($rl =~ m/[rl]/o); |
| 276 |
$rl = ($j & 1) ? "l" : "r" unless ($rl =~ m/[rl]/o); |
| 277 |
if ($rl eq "r") |
| 278 |
{ $prbox[1] = $prbox[3] - (($prbox[3] - $prbox[1]) / $is); } |
| 279 |
elsif ($rl eq "l") |
| 280 |
{ $prbox[3] = $prbox[1] + (($prbox[3] - $prbox[1]) / $is); } |
| 281 |
|
| 282 |
if ($bt eq "t") |
| 283 |
{ $prbox[2] = $prbox[0] + (($prbox[2] - $prbox[0]) * 2 / $is); } |
| 284 |
elsif ($is == 4) |
| 285 |
{ $prbox[0] = $prbox[2] - (($prbox[2] - $prbox[0]) * 2 / $is); } |
| 286 |
elsif ($opt_s =~ m/b$/o) |
| 287 |
{ @clipbox = ($prbox[0], $prbox[1], $prbox[2] - $prbox[0], $prbox[3] - $prbox[1]); } |
| 288 |
} |
| 289 |
elsif ($opt_s) |
| 290 |
{ die "Illegal -s value of $opt_s"; } |
| 291 |
$id = join(',', @prbox) . ",$opt_p"; |
| 292 |
if (!defined $scache{$id}) |
| 293 |
{ |
| 294 |
@slist = (); |
| 295 |
$xs = ($pbox[2] - $pbox[0]) / ($prbox[2] - $prbox[0]); |
| 296 |
# $ys = ($pbox[3] - $pbox[1]) / ($prbox[3] - $prbox[1]); |
| 297 |
$ys = ($pbox[3] - $pbox[1] - $opt_u*2) / ($prbox[3] - $prbox[1]); |
| 298 |
$scale = ($prbox[3] - $prbox[1]) / ($prbox[2] - $prbox[0]); |
| 299 |
$rot = (($opt_p == 1 || $opt_p == 4) && $is == 2) |
| 300 |
|| ($opt_p == 2 && $is != 2); |
| 301 |
|
| 302 |
if ($opt_l) |
| 303 |
{ |
| 304 |
if ($xs < ($opt_p == 2 ? .5 : 1) * ($opt_r ? $scale * $scale : 1) * $ys) |
| 305 |
{ $ys = $xs / ($opt_r ? $scale * $scale : 1) / ($opt_p == 2 ? .5 : 1); } |
| 306 |
else |
| 307 |
{ $xs = $ys * ($opt_r ? $scale * $scale : 1) * ($opt_p == 2 ? .5 : 1); } |
| 308 |
} |
| 309 |
|
| 310 |
if ($opt_p == 1 && $is != 2) # portrait to portrait |
| 311 |
{ |
| 312 |
|
| 313 |
$slist[0] = cm($xs, 0, 0, $ys, |
| 314 |
# $pbox[0] - ($xs * $prbox[0]), $pbox[1] - ($ys * $prbox[1])); |
| 315 |
.5 * ($pbox[2] + $pbox[0] - $xs * ($prbox[2] + $prbox[0])), |
| 316 |
.5 * ($pbox[3] + $pbox[1] - $ys * ($prbox[3] + $prbox[1])), @clipbox); |
| 317 |
} elsif ($opt_p == 1) # landscape on portrait to portrait |
| 318 |
{ |
| 319 |
$slist[0] = cm(0, -$scale * $ys, $xs / $scale, 0, |
| 320 |
$pbox[0] - $xs * $prbox[1] / $scale, $pbox[1] + $scale * $ys * $prbox[2], @clipbox); |
| 321 |
} elsif ($opt_p == 2 && $is != 2) # portrait source on portrait |
| 322 |
{ |
| 323 |
@scalestr = (0, 0.5 * $ys * $scale, -$xs / $scale, 0, |
| 324 |
0.5 * ($xs * ($prbox[3] + $prbox[1]) / $scale + $pbox[2])); |
| 325 |
$slist[0] = cm(@scalestr, |
| 326 |
# 0.5 * (-$ys * $scale * $prbox[0] + $pbox[1] + $pbox[3])); |
| 327 |
.25 * (3 * ($pbox[1] + $pbox[3]) - $ys * $scale * ($prbox[2] + $prbox[0])) + $opt_u*0.5, @clipbox); |
| 328 |
$slist[1] = cm(@scalestr, |
| 329 |
# -0.5 * $ys * $scale * $prbox[0] + $pbox[1]); |
| 330 |
.25 * (3 * $pbox[1] + $pbox[3] - $ys * $scale * ($prbox[2] + $prbox[0])) - $opt_u*0.5, @clipbox); |
| 331 |
} elsif ($opt_p == 2) # double page landscape on portrait |
| 332 |
{ |
| 333 |
@scalestr = ($xs, 0, 0, 0.5 * $ys, .5 * ($pbox[2] - $xs * ($prbox[2] - $prbox[0]))); |
| 334 |
# -$xs * $prbox[0] + $pbox[0]); |
| 335 |
# $slist[0] = cm(@scalestr, 0.5 * (-$ys * $prbox[1] + $pbox[1] + $pbox[3]), @clipbox); |
| 336 |
# $slist[1] = cm(@scalestr, -0.5 * $ys * $prbox[1] + $pbox[1], @clipbox); |
| 337 |
$slist[0] = cm(@scalestr, .25 * (3 * $pbox[3] + $pbox[1] - $ys * ($prbox[3] - $prbox[1])) - $prbox[1], @clipbox); |
| 338 |
$slist[1] = cm(@scalestr, .25 * (3 * $pbox[1] + $pbox[3] - $ys * ($prbox[3] - $prbox[1])) - $prbox[1], @clipbox); |
| 339 |
} elsif ($opt_p == 4 && $is == 1) # true portrait |
| 340 |
{ |
| 341 |
$a = .5 * $xs; $b = .5 * $ys; |
| 342 |
$slist[0] = cm($a, 0, 0, $b, |
| 343 |
-$a * $prbox[0] + 0.5 * ($pbox[0] + $pbox[2]), -$b * $prbox[1] + $pbox[1], @clipbox); |
| 344 |
$slist[2] = cm(-$a, 0, 0, -$b, |
| 345 |
$a * $prbox[0] + 0.5 * ($pbox[0] + $pbox[2]), $b * $prbox[1] + $pbox[3], @clipbox); |
| 346 |
if ($opt_p =~ /s/o) |
| 347 |
{ |
| 348 |
$slist[1] = cm($a, 0, 0, $b, |
| 349 |
-$a * $prbox[0] + 0.5 * ($pbox[0] + $pbox[2]), -$b * $prbox[1] + 0.5 * ($pbox[1] + $pbox[3]), @clipbox); |
| 350 |
$slist[3] = cm(-$a, 0, 0, -$b, |
| 351 |
$a * $prbox[0] + 0.5 * ($pbox[0] + $pbox[2]), $b * $prbox[1] + 0.5 * ($pbox[1] + $pbox[3]), @clipbox); |
| 352 |
} |
| 353 |
else |
| 354 |
{ |
| 355 |
$slist[1] = cm(-$a, 0, 0, -$b, |
| 356 |
$a * $prbox[0] + $pbox[2], $b * $prbox[1] + $pbox[3], @clipbox); |
| 357 |
$slist[3] = cm($a, 0, 0, $b, |
| 358 |
-$a * $prbox[0] + $pbox[0], -$b * $prbox[1] + $pbox[1], @clipbox); |
| 359 |
} |
| 360 |
} elsif ($opt_p == 4) |
| 361 |
{ |
| 362 |
$a = .5 * $ys * $scale; $b = .5 * $xs / $scale; |
| 363 |
$slist[0] = cm(0, -$a, $b, 0, |
| 364 |
-$b * $prbox[1] + 0.5 * ($pbox[0] + $pbox[2]), $a * $prbox[2] + $pbox[1], @clipbox); |
| 365 |
$slist[2] = cm(0, $a, -$b, 0, |
| 366 |
$b * $prbox[1] + 0.5 * ($pbox[0] + $pbox[2]), -$a * $prbox[2] + $pbox[3], @clipbox); |
| 367 |
if ($opt_p =~ /s/o) |
| 368 |
{ |
| 369 |
$slist[1] = cm(0, -$a, $b, 0, |
| 370 |
-$b * $prbox[1] + 0.5 * ($pbox[0] + $pbox[2]), $a * $prbox[2] + 0.5 * ($pbox[1] + $pbox[3]), @clipbox); |
| 371 |
$slist[3] = cm(0, $a, -$b, 0, |
| 372 |
$b * $prbox[1] + 0.5 * ($pbox[0] + $pbox[2]), -$a * $prbox[2] + 0.5 * ($pbox[1] + $pbox[3]), @clipbox); |
| 373 |
} |
| 374 |
else |
| 375 |
{ |
| 376 |
$slist[1] = cm(0, $a, -$b, 0, |
| 377 |
$b * $prbox[1] + $pbox[2], -$a * $prbox[2] + $pbox[3], @clipbox); |
| 378 |
$slist[3] = cm(0, -$a, $b, 0, |
| 379 |
-$b * $prbox[1] + $pbox[0], $a * $prbox[2] + $pbox[1], @clipbox); |
| 380 |
} |
| 381 |
} |
| 382 |
$scache{$id} = [@slist]; |
| 383 |
} |
| 384 |
@s = $pr[$j]->{'Contents'}->elementsof if (defined $pr[$j]->{'Contents'}); |
| 385 |
|
| 386 |
if (!defined $s) |
| 387 |
{ |
| 388 |
$min = 100000; |
| 389 |
for ($k = 0; $k <= $#pr; $k++) |
| 390 |
{ |
| 391 |
next unless defined $pr[$k]; |
| 392 |
if ($pr[$k]->{' pnum'} < $min) |
| 393 |
{ |
| 394 |
$bp = $pr[$k]; |
| 395 |
$min = $pr[$k]->{' pnum'}; |
| 396 |
} |
| 397 |
} |
| 398 |
$s = PDFArray(); |
| 399 |
$bp->{' Contents'} = $p->new_obj($s); |
| 400 |
$bp->{'Rotate'} = PDFNum(90) if ($opt_r); |
| 401 |
} |
| 402 |
next unless defined $pr[$j]; |
| 403 |
$s->add_elements($fpgins) unless $j == $#pr; |
| 404 |
$s->add_elements($scache{$id}[$j]) unless ($opt_h == 17 && $j == $#pr); |
| 405 |
$s->add_elements(@s); |
| 406 |
$s->add_elements($spgins) unless $j == $#pr; |
| 407 |
|
| 408 |
if ($pr[$j] ne $bp) |
| 409 |
{ |
| 410 |
$pr2 = $pr[$j]; |
| 411 |
$s1 = $bp->find_prop('Resources'); |
| 412 |
$s2 = $pr2->find_prop('Resources'); |
| 413 |
$bp->{'Resources'} = merge_dict($s1, $s2) unless ($s1 eq $s2); |
| 414 |
|
| 415 |
$p->free_obj($pr2); |
| 416 |
while (defined $pr2->{'Parent'}) |
| 417 |
{ |
| 418 |
$temp = $pr2->{'Parent'}; |
| 419 |
$temp->{'Kids'}->removeobj($pr2) |
| 420 |
if ($pr2->{'Type'}{'val'} eq 'Page' || $pr2->{'Kids'}->elementsof <= 0); |
| 421 |
$temp->{'Count'}{'val'}--; |
| 422 |
$pr2 = $temp; |
| 423 |
if ($pr2->{'Kids'}->elementsof <= 0) |
| 424 |
{ |
| 425 |
print "Killing a tree! $pr2->{'num'}\n" unless $opt_q; |
| 426 |
$p->free_obj($pr2); |
| 427 |
} else |
| 428 |
{ $p->out_obj($pr2); } |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
return 1 unless defined $bp; |
| 433 |
$bp->{'Contents'} = delete $bp->{' Contents'}; |
| 434 |
foreach (qw(Annots Thumb Beeds CropBox)) |
| 435 |
{ delete $bp->{$_} if defined $bp->{$_}; } |
| 436 |
$bp->bbox(@pbox); |
| 437 |
$p->out_obj($bp); |
| 438 |
} |
| 439 |
|
| 440 |
sub merge_dict |
| 441 |
{ |
| 442 |
my ($p1, $p2) = @_; |
| 443 |
|
| 444 |
return $p1 if (ref $p1 eq "Text::PDF::Objind" && ref $p2 eq "Text::PDF::Objind" |
| 445 |
&& $p->{' objects'}{$p1->uid}[0] eq $p->{' objects'}{$p2->uid}[0]); |
| 446 |
# $p1 = $p->read_obj($p1) if (ref $p1 eq "Text::PDF::Objind"); |
| 447 |
# $p2 = $p->read_obj($p2) if (ref $p2 eq "Text::PDF::Objind"); |
| 448 |
$p1->realise; |
| 449 |
$p2->realise; |
| 450 |
|
| 451 |
my ($k, $v1, $v2); |
| 452 |
my (@a1, @a2, %a1); |
| 453 |
|
| 454 |
if ($p1->{' isvisited'}) |
| 455 |
{ |
| 456 |
warn "circular reference!"; |
| 457 |
return $p1; |
| 458 |
} |
| 459 |
|
| 460 |
$p1->{' isvisited'} = 1; |
| 461 |
$p2->{' isvisited'} = 1; |
| 462 |
|
| 463 |
foreach $k (keys %{$p1}) |
| 464 |
{ |
| 465 |
next if ($k =~ m/^\s/oi); |
| 466 |
$v1 = $p1->{$k}; |
| 467 |
$v2 = $p2->{$k}; |
| 468 |
next if $v1 eq $v2 || !defined $v2; # !defined added v1.201 |
| 469 |
if (ref $v1 eq "Text::PDF::Objind" || ref $v2 eq "Text::PDF::Objind") |
| 470 |
{ |
| 471 |
$v1->realise if (ref $v1 eq "Text::PDF::Objind"); |
| 472 |
$v2->realise if (ref $v2 eq "Text::PDF::Objind"); |
| 473 |
} |
| 474 |
next unless defined $v2; |
| 475 |
|
| 476 |
# assume $v1 & $v2 are of the same type |
| 477 |
if (ref $v1 eq "Text::PDF::Array") |
| 478 |
{ |
| 479 |
# merge contents of array uniquely (the array is a set) |
| 480 |
@a1 = $v1->elementsof; |
| 481 |
@a2 = $v2->elementsof; |
| 482 |
map { $a1{$_} = 1; } @a1; |
| 483 |
push (@a1, grep (!$a1{$_}, @a2)); |
| 484 |
$p1->{$k} = PDFArray(@a1); |
| 485 |
} elsif (ref $v1 eq "Text::PDF::Dict") |
| 486 |
# { $p1->{$k} = merge_dict($v1->val, $v2->val); } |
| 487 |
{ $p1->{$k} = merge_dict($v1, $v2); } |
| 488 |
elsif ($v1->val ne $v2->val) |
| 489 |
{ |
| 490 |
warn "Inconsistent dictionaries at $k with " . $v1->val . " and " . $v2->val; |
| 491 |
$p1->{$k} = $v1; |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
foreach $k (grep (!defined $p1->{$_} && $_ !~ m/^\s/oi, keys %{$p2})) |
| 496 |
{ $p1->{$k} = $p2->{$k}; } |
| 497 |
$p->out_obj($p1) if $p1->is_obj($p); |
| 498 |
delete $p1->{' isvisited'}; |
| 499 |
delete $p2->{' isvisited'}; |
| 500 |
return $p1; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
sub cm |
| 505 |
{ |
| 506 |
my (@a) = @_; |
| 507 |
my ($res, $r, $str); |
| 508 |
|
| 509 |
foreach $r (@a) |
| 510 |
{ $r = int($r) if (abs($r - int($r)) < 1e-6); } |
| 511 |
$str = "$a[6] $a[7] $a[8] $a[9] re W n" if (defined $a[6]); |
| 512 |
return undef if ($a[0] == 1 && $a[1] == 0 && $a[2] == 0 && $a[3] == 1 && $a[4] == 0 && $a[5] == 0 && $str eq ''); |
| 513 |
|
| 514 |
$res = PDFDict(); |
| 515 |
$p->new_obj($res); |
| 516 |
$res->{' stream'} = "$a[0] $a[1] $a[2] $a[3] $a[4] $a[5] cm $str"; |
| 517 |
$res; |
| 518 |
} |
| 519 |
|
| 520 |
sub copy_page |
| 521 |
{ |
| 522 |
my ($page) = @_; |
| 523 |
return undef unless $page; |
| 524 |
my ($res) = $page->copy; |
| 525 |
$p->new_obj($res); |
| 526 |
|
| 527 |
$res; |
| 528 |
} |