Debian Codegraph @

scripts/pdfbklt

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