File: pdfstamp

package info (click to toggle)
libtext-pdf-perl 0.29a-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 360 kB
  • ctags: 185
  • sloc: perl: 3,431; makefile: 11
file content (116 lines) | stat: -rwxr-xr-x 2,935 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/perl

=head1 NAME

pdfstamp.plx - Adds the given text in a given fonts, size to all pages
    at given location

=head1 DESCRIPTION

Adds the given string to the infile .pdf file at the given location,
    font and size.

=head1 SYNOPSIS

    pdfstamp [-f font] [-l locx,locy] [-s size] infile string

    -f font     Font name from the standard fonts [Helvetica]
    -l locx,locy    Location in points from bottom left of page [0,0]
    -s size     Font size to print at             [11]
    -t ttfile   TrueType font file to use (instead of -f)

=cut

use Text::PDF::File;
use Text::PDF::SFont;
use Text::PDF::Utils;

use Getopt::Std;

getopts('f:l:s:t:');

$VERSION = 0.02;    #   MJPH    23-JUL-2001     Re-order to stamp on the top

unless (defined $ARGV[1] && -f $ARGV[0])
{
    die <<'EOT';
    pdfstamp [-f font] [-l locx,locy] [-s size] infile string
Adds the given string to the infile .pdf file at the given location, font and
size.

    -f font     Font name from the standard fonts [Helvetica]
    -l locx,locy    Location in points from bottom left of page [0,0]
    -s size     Font size to print at             [11]
    -t ttfile   TrueType font file to use (instead of -f)
EOT
}

require Text::PDF::TTFont if ($opt_t);

$opt_f = 'Helvetica' unless $opt_f;
$opt_s = 11 unless $opt_s;
$opt_l =~ s/,\s*/ /o;
$opt_l = "0 0" unless $opt_l;

$pdf = Text::PDF::File->open($ARGV[0], 1);
$root = $pdf->{'Root'}->realise;
$pgs = $root->{'Pages'}->realise;

$fpgins = PDFDict(); $pdf->new_obj($fpgins);
$spgins = PDFDict(); $pdf->new_obj($spgins);
$fpgins->{' stream'} = "q";
$spgins->{' stream'} = "Q";

@pglist = proc_pages($pdf, $pgs);

$max = 0;
foreach $p (@pglist)
{
    $dict = $p->find_prop('Resources');
    if (defined $dict && defined $dict->{'Font'})
    {
        foreach $k (keys %{$dict->{'Font'}})
        {
            next unless $k =~ m/^ap([0-9]+)/o;
            $val = $1;
            $max = $val if $val > $max;
        }
    }
}

$max++;
if ($opt_t)
{ $font = Text::PDF::TTFont->new($pdf, $opt_t, "ap$max", -subset => 1) || die "Can't work with font $opt_t"; }
else
{ $font = Text::PDF::SFont->new($pdf, $opt_f, "ap$max") || die "Can't create font $opt_f"; }
$stream = PDFDict();
$stream->{' stream'} = "BT 1 0 0 1 $opt_l Tm /ap$max $opt_s Tf " . $font->out_text($ARGV[1]) . " Tj ET";
$pdf->new_obj($stream);
foreach $p (@pglist)
{
    $p->add_font($font, $pdf);
    $p->{Contents} = PDFArray($fpgins, $p->{Contents}->elementsof, $spgins, $stream);
    $pdf->out_obj($p);
}

$pdf->close_file;

sub proc_pages
{
    my ($pdf, $pgs) = @_;
    my ($pg, $pgref, @pglist);

    foreach $pgref ($pgs->{'Kids'}->elementsof)
    {
        $pg = $pdf->read_obj($pgref);
        if ($pg->{'Type'}->val =~ m/^Pages$/oi)
        { push(@pglist, proc_pages($pdf, $pg)); }
        else
        {
            $pgref->{' pnum'} = $pcount++;
            push (@pglist, $pgref);
        }
    }
    (@pglist);
}