Debian Codegraph @

scripts/pdfstamp

raw ◀ ▶
1 Debian patch touch this file
1 #!/usr/bin/perl
2
3 use 5.006;
4 use strict;
5
•6 =head1 NAME
7
8 pdfstamp.plx - Adds the given text in a given fonts, size to all pages
9 at given location
10
11 =head1 DESCRIPTION
12
13 Adds the given string to the infile .pdf file at the given location,
14 font and size.
15
16 =head1 SYNOPSIS
17
18 pdfstamp [-f font] [-l locx,locy] [-s size] infile string
19
20 -f font Font name from the standard fonts [Helvetica]
21 -l locx,locy Location in points from bottom left of page [0,0]
22 -s size Font size to print at [11]
23 -t ttfile TrueType font file to use (instead of -f)
24
25 =cut
26
27 use Text::PDF::File;
28 use Text::PDF::SFont;
29 use Text::PDF::Utils;
30
31 use Getopt::Std;
32 our ($opt_t,$opt_f,$opt_p,$opt_s,$opt_l);
33 getopts('f:l:ps:t:');
34
35 our $VERSION = 0.02; # MJPH 23-JUL-2001 Re-order to stamp on the top
36
37 unless ((defined $ARGV[1] || $opt_p) && -f $ARGV[0])
38 {
39 die <<'EOT';
40 pdfstamp [-f font] [-l locx,locy] [-s size] infile string
41 Adds the given string to the infile .pdf file at the given location, font and
42 size.
43
44 -f font Font name from the standard fonts [Helvetica]
45 -l locx,locy Location in points from bottom left of page [0,0]
46 -p Use the page number as the string
47 -s size Font size to print at [11]
48 -t ttfile TrueType font file to use (instead of -f)
49 EOT
50 }
51
52 require Text::PDF::TTFont if ($opt_t);
53
54 $opt_f = 'Helvetica' unless $opt_f;
55 $opt_s = 11 unless $opt_s;
56 $opt_l =~ s/,\s*/ /o;
57 $opt_l = "0 0" unless $opt_l;
58
59 my ($pdf, $root, $pgs, $fpgins, $spgins, @pglist);
60
61 $pdf = Text::PDF::File->open($ARGV[0], 1);
62 $root = $pdf->{'Root'}->realise;
63 $pgs = $root->{'Pages'}->realise;
64
65 $fpgins = PDFDict(); $pdf->new_obj($fpgins);
66 $spgins = PDFDict(); $pdf->new_obj($spgins);
67 $fpgins->{' stream'} = "q";
68 $spgins->{' stream'} = "Q";
69
70 @pglist = proc_pages($pdf, $pgs);
71
72 my $max = 0;
73 foreach my $p (@pglist)
74 {
75 my $dict = $p->find_prop('Resources');
76 if (defined $dict && defined $dict->{'Font'})
77 {
78 foreach my $k (keys %{$dict->{'Font'}})
79 {
80 next unless $k =~ m/^ap([0-9]+)/o;
81 my $val = $1;
82 $max = $val if $val > $max;
83 }
84 }
85 }
86
87 $max++;
88 my $font;
89 if ($opt_t)
90 { $font = Text::PDF::TTFont->new($pdf, $opt_t, "ap$max", -subset => 1) || die "Can't work with font $opt_t"; }
91 else
92 { $font = Text::PDF::SFont->new($pdf, $opt_f, "ap$max") || die "Can't create font $opt_f"; }
93 my $stream = PDFDict();
94 $stream->{' stream'} = "BT 1 0 0 1 $opt_l Tm /ap$max $opt_s Tf " . $font->out_text($ARGV[1]) . " Tj ET";
95 $pdf->new_obj($stream);
96 my $count = 1;
97 foreach my $p (@pglist)
98 {
99 my ($s) = $stream;
100 if ($opt_p)
101 {
102 $s = PDFDict();
103 $s->{' stream'} = "BT 1 0 0 1 $opt_l Tm /ap$max $opt_s Tf " . $font->out_text($count++) . " Tj ET";
104 $pdf->new_obj($s);
105 }
106 $p->add_font($font, $pdf);
107 $p->{Contents} = PDFArray($fpgins, $p->{Contents}->elementsof, $spgins, $s);
108 $pdf->out_obj($p);
109 }
110
111 $pdf->close_file;
112
113 sub proc_pages
114 {
115 my ($pdf, $pgs) = @_;
116 my ($pg, $pgref, @pglist, $pcount);
117
118 foreach $pgref ($pgs->{'Kids'}->elementsof)
119 {
120 $pg = $pdf->read_obj($pgref);
121 if ($pg->{'Type'}->val =~ m/^Pages$/oi)
122 { push(@pglist, proc_pages($pdf, $pg)); }
123 else
124 {
125 $pgref->{' pnum'} = $pcount++;
126 push (@pglist, $pgref);
127 }
128 }
129 (@pglist);
130 }
131