| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package PDF::Tk; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 1499 | use IO::All; | 
|  | 1 |  |  |  |  | 18727 |  | 
|  | 1 |  |  |  |  | 11 |  | 
| 4 | 1 |  |  | 1 |  | 1068 | use IPC::Open2; | 
|  | 1 |  |  |  |  | 3239 |  | 
|  | 1 |  |  |  |  | 44 |  | 
| 5 | 1 |  |  | 1 |  | 1489 | use File::Temp qw/tempdir tempfile/; | 
|  | 1 |  |  |  |  | 15425 |  | 
|  | 1 |  |  |  |  | 58 |  | 
| 6 | 1 |  |  | 1 |  | 6 | use Cwd; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 46 |  | 
| 7 | 1 |  |  | 1 |  | 4 | use strict; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 1063 |  | 
| 8 |  |  |  |  |  |  | our $VERSION='0.02'; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | sub new { | 
| 11 | 0 |  |  | 0 | 1 |  | my ($proto,%options)=@_; | 
| 12 | 0 |  | 0 |  |  |  | my $class= ref $proto || $proto; | 
| 13 | 0 |  |  |  |  |  | my $self=\%options; | 
| 14 | 0 |  |  |  |  |  | bless $self,$class; | 
| 15 | 0 | 0 | 0 |  |  |  | die "Can't take both a file and document argument" | 
| 16 |  |  |  |  |  |  | if ($self->{file} && $self->{document}); | 
| 17 | 0 |  | 0 |  |  |  | $self->{pdftk} ||= "/usr/bin/pdftk"; | 
| 18 | 0 | 0 |  |  |  |  | $self->_get_document if $self->{file}; | 
| 19 | 0 | 0 |  |  |  |  | die "Can't find executablable ".$self->{pdftk} | 
| 20 |  |  |  |  |  |  | unless -x $self->{pdftk}; | 
| 21 | 0 |  |  |  |  |  | return $self; | 
| 22 |  |  |  |  |  |  | } | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub _get_document { | 
| 25 | 0 |  |  | 0 |  |  | my $self=shift; | 
| 26 | 0 |  |  |  |  |  | my @targets; | 
| 27 | 0 | 0 |  |  |  |  | if (ref($self->{file}) eq "ARRAY") { | 
| 28 | 0 |  |  |  |  |  | warn "Setting together an arrayref"; | 
| 29 | 0 |  |  |  |  |  | foreach my $file (@{$self->{file}}) { | 
|  | 0 |  |  |  |  |  |  | 
| 30 | 0 | 0 |  |  |  |  | if (ref($file) eq "SCALAR") { | 
| 31 | 0 |  |  |  |  |  | local $/; | 
| 32 | 0 |  |  |  |  |  | my ($fh,$filename)=tempfile(); | 
| 33 | 0 |  |  |  |  |  | print $fh $$file; # put data in a file | 
| 34 | 0 |  |  |  |  |  | $file=$filename; # set the filename | 
| 35 | 0 |  |  |  |  |  | push @targets,$filename; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  | } | 
| 38 | 0 |  |  |  |  |  | $self->call_pdftk($self->{file},\($self->{document}),"cat"); | 
| 39 | 0 | 0 |  |  |  |  | die "Could not load ".$self->{file} unless $self->{document}; | 
| 40 |  |  |  |  |  |  | } else { | 
| 41 | 0 |  |  |  |  |  | $self->{document} = io($self->{file})->binary->all; | 
| 42 | 0 | 0 |  |  |  |  | die "Could not load ".$self->{file} unless $self->{document}; | 
| 43 |  |  |  |  |  |  | } | 
| 44 | 0 |  |  |  |  |  | delete $self->{file}; | 
| 45 | 0 |  |  |  |  |  | unlink @targets; | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | sub call_pdftk { | 
| 49 | 0 |  |  | 0 | 1 |  | my ($self,$input,$output,@args)=@_; | 
| 50 | 0 |  |  |  |  |  | local $/; | 
| 51 | 0 | 0 | 0 |  |  |  | if (ref $input eq "SCALAR" &&  ref $output eq "SCALAR") { | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 52 | 0 |  |  |  |  |  | my ($rdfh,$wrfh); | 
| 53 | 0 | 0 |  |  |  |  | my $pid=open2($rdfh,$wrfh,$self->{pdftk},"-",@args,"output","-") | 
| 54 |  |  |  |  |  |  | or die "pdftk - @args - failed: $?"; | 
| 55 | 0 |  |  |  |  |  | print $wrfh $$input; | 
| 56 | 0 |  |  |  |  |  | close $wrfh; | 
| 57 | 0 |  |  |  |  |  | $$output=<$rdfh>; | 
| 58 | 0 |  |  |  |  |  | close  $rdfh; | 
| 59 | 0 |  |  |  |  |  | waitpid $pid,0; | 
| 60 |  |  |  |  |  |  | } elsif (ref $input eq "SCALAR") { | 
| 61 | 0 |  |  |  |  |  | my $fh; | 
| 62 | 0 | 0 |  |  |  |  | open($fh,"|-",$self->{pdftk},"-",@args,"output",$output) | 
| 63 |  |  |  |  |  |  | or die $self->{pdftk}." - @args output $output failed: $?"; | 
| 64 | 0 |  |  |  |  |  | print $fh $$input; | 
| 65 | 0 |  |  |  |  |  | close $fh; | 
| 66 |  |  |  |  |  |  | } elsif (ref $output eq "SCALAR") { | 
| 67 | 0 |  |  |  |  |  | my $fh; | 
| 68 | 0 | 0 |  |  |  |  | open($fh,"-|",$self->{pdftk},(ref $input eq "ARRAY" ? @$input : $input),@args,"output","-") | 
|  |  | 0 |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | or die "pdftk $input @args - failed: $?"; | 
| 70 | 0 |  |  |  |  |  | $$output=<$fh>; | 
| 71 | 0 |  |  |  |  |  | close $fh; | 
| 72 |  |  |  |  |  |  | } else { | 
| 73 | 0 | 0 |  |  |  |  | system($self->{pdftk},(ref $input eq "ARRAY" ? @$input : $input),@args,"output",$output) == 0 | 
|  |  | 0 |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | or die "pdftk $input @args $output failed: $?"; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub document { | 
| 79 | 0 |  |  | 0 | 1 |  | my ($self,$doc)=@_; | 
| 80 | 0 | 0 |  |  |  |  | if ($doc) { $self->{document}=$doc; } | 
|  | 0 |  |  |  |  |  |  | 
| 81 | 0 |  |  |  |  |  | else      { return $self->{document}; } | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | sub pages { | 
| 85 | 0 |  |  | 0 | 1 |  | my $self=shift; | 
| 86 | 0 |  |  |  |  |  | my $tmpdir=tempdir; | 
| 87 | 0 |  |  |  |  |  | my ($pdftk,@pages); | 
| 88 | 0 |  |  |  |  |  | chdir $tmpdir; | 
| 89 | 0 |  |  |  |  |  | $self->call_pdftk(\($self->{document}),'%d.pdf','burst'); | 
| 90 | 0 |  |  |  |  |  | my $page=1; | 
| 91 | 0 |  |  |  |  |  | while (-f "./$page.pdf") { | 
| 92 | 0 |  |  |  |  |  | push @pages,io(cwd."/$page.pdf")->binary->all; | 
| 93 | 0 |  |  |  |  |  | unlink (cwd."/$page.pdf"); | 
| 94 | 0 |  |  |  |  |  | $page++; | 
| 95 |  |  |  |  |  |  | } | 
| 96 | 0 |  |  |  |  |  | unlink ("doc_data.txt"); | 
| 97 | 0 |  |  |  |  |  | chdir "/"; | 
| 98 | 0 |  |  |  |  |  | rmdir $tmpdir; | 
| 99 | 0 | 0 |  |  |  |  | return (wantarray ? @pages :\@pages); | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | sub page { | 
| 103 | 0 |  |  | 0 | 1 |  | my ($self,$page)=@_; | 
| 104 | 0 |  |  |  |  |  | my $tmpdir=tempdir; | 
| 105 | 0 |  |  |  |  |  | chdir $tmpdir; | 
| 106 | 0 |  |  |  |  |  | $self->call_pdftk(\($self->{document}),'%d.pdf','burst'); | 
| 107 | 0 |  |  |  |  |  | my $res=io(cwd."/$page.pdf")->binary->all; | 
| 108 | 0 |  |  |  |  |  | unlink <*.pdf>; | 
| 109 | 0 |  |  |  |  |  | unlink ("doc_data.txt"); | 
| 110 | 0 |  |  |  |  |  | chdir "/"; | 
| 111 | 0 |  |  |  |  |  | rmdir $tmpdir; | 
| 112 | 0 |  |  |  |  |  | return $res; | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | sub docinfo { | 
| 117 | 0 |  |  | 0 | 1 |  | my ($self,$arg)=@_; | 
| 118 | 0 | 0 |  |  |  |  | unless ($self->{documentinfo}) { | 
| 119 | 0 |  |  |  |  |  | my $documentinfo; | 
| 120 | 0 |  |  |  |  |  | $self->call_pdftk(\($self->{document}),\$documentinfo,"dump_data"); | 
| 121 | 0 |  |  |  |  |  | my @lines=split "\n",$documentinfo; | 
| 122 | 0 |  |  |  |  |  | my %documentinfo; | 
| 123 | 0 |  |  |  |  |  | while (my $line=shift @lines) { | 
| 124 | 0 |  |  |  |  |  | my ($key,$val)=split m/\:\s*/,$line; | 
| 125 | 0 | 0 |  |  |  |  | if ($key eq "InfoKey") { | 
| 126 | 0 |  |  |  |  |  | $key=$val; | 
| 127 | 0 |  |  |  |  |  | $line=shift @lines; | 
| 128 | 0 |  |  |  |  |  | ($val)=$line=~m/InfoValue\:\s*(.+)/; | 
| 129 |  |  |  |  |  |  | } | 
| 130 | 0 |  |  |  |  |  | $documentinfo{lc($key)}=$val; | 
| 131 |  |  |  |  |  |  | } | 
| 132 | 0 |  |  |  |  |  | $self->{documentinfo}=\%documentinfo; | 
| 133 |  |  |  |  |  |  | } | 
| 134 | 0 | 0 |  |  |  |  | return $self->{documentinfo}->{$arg}if ($arg); | 
| 135 | 0 |  |  |  |  |  | return $self->{documentinfo}; | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | 1; | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | =head1 NAME | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | PDF::Tk - Perl integration for the pdf toolkit (pdftk) | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | use PDF::Tk; | 
| 148 |  |  |  |  |  |  | my $doc=PDF::Tk->new(file=>["/tmp/my1.pdf","/tmp/my2.pdf"]); | 
| 149 |  |  |  |  |  |  | my @parts=$doc->pages(); | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | This module is a interface for the command line pdftk command. | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | =head1 METHODS | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | =over 4 | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | =item new | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | The constructor for the pdftk module. Takes a hash of arguments | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | document - a scalar containing a PDF document, | 
| 164 |  |  |  |  |  |  | file - either a PDF filename or a arrayref of filenames. | 
| 165 |  |  |  |  |  |  | pdftf - path to the pdftk binary, defaults to "/usr/bin/pdftk" | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | note that document and file are mutually exclusive! | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | =item call_pdftk | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | Calls up pdftk command, takes input, output and pdftk operation as arguments | 
| 172 |  |  |  |  |  |  | input and output can either be files or scalar refs. input can also be an | 
| 173 |  |  |  |  |  |  | array ref of files | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | =item pages | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | returns an array in list context, or arrayref, containing the content of all | 
| 178 |  |  |  |  |  |  | pages in the document. | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | =item page | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | Takes a page as an argument, and returns the contents of that page. | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | =item docinfo | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | If you provide an argument, it will return that value (lower cased), or | 
| 187 |  |  |  |  |  |  | else it will return a hash of values; | 
| 188 |  |  |  |  |  |  | Common values are B ,B, B,B, B, | 
| 189 |  |  |  |  |  |  | B, B, B, B. | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | =item document | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | Accessor for the actual document. | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | =back | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | L | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | =head1 AUTHOR | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | Marcus Ramberg, Emarcus@mediaflex.noE | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | Copyright 2004 by Mediaflex A/S. | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify | 
| 210 |  |  |  |  |  |  | it under the same terms as Perl itself. | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | =cut |