blib/lib/PDF/Xtract.pm

filter: Statementstmt 6.1% 19 / 308 Branchbran 0.0% 0 / 128 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 23.5% 4 / 17 Podpod 7.1% 1 / 14 totaltotal 5.1% 25 / 487 CC 83 SCAR 68.0
CC
83
Cov
4%
setEnv
1892.0
makePDF
380.0
savePDFExtract
42.0
CRAP 902.1
↑ Summary
1 # 1. Ensure all file names are quoted
2 # 2. Try keep bare minimum processing so that user programs will not get loaded with stuff they dont need!
3 # 3. Comment-out performance tester, it is only usefull during development/testing.
4
5 package PDF::Xtract;
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # use Time::HiRes qw(gettimeofday);
10
11 1 $VERSION = '0.08';
12
13 1 my ( $balance, $trailerObject, $RootObject, $EncryptedObject, $InfoObject, $TempExtractFile, $LastExtractFile, $CatalogPages);
14 1 my ( %vars, %objval, %parent, %Referals, %IncludeObjects, %kids, %page, %BabyCountOfObject, %BabiesOfObject );
15
16 1 my $CRLF = '[ \t\r\n\f\0]'."*(?:\015|\012|(?:\015\012))";
17 # ---------------------------------------------------
18 =head1 NAME
19
20 PDF::Xtract - Extracting sub PDF documents from a multi page PDF document, much faster than PDF::Extract!
21
22 =head1 SYNOPSIS
23
24 Please read Manual-PDF-Xtract.pdf in the distribution for detailed documentation.
25
26 use PDF::Xtract;
27 $pdf=new PDF::Xtract;
28 @pages=(10..30,5,7); # Defining pages to be extracted ( 10 to 30, 5 and 7 - in the order required for output).
29 $pages=\@pages;
30 $pdf->savePDFExtract( PDFDoc=>"c:/Docs/my.pdf", PDFSaveAs="out.pdf", PDFPages=>$pages ); # Saves extracted pages to "out.pdf"
31
32 print "Content-Type text/plain\n\n<xmp>", $pdf->getPDFExtract; # May be useful for a web-site!
33
34 OR
35
36 # Extract and save, in the current directory, all the pages in a PDF document with nice names.
37 use PDF::Xtract;
38 $pdf=new PDF::Xtract( PDFDoc=>"test.pdf" );
39 @tmp=$pdf->getPDFExtractVariables(PDFPageCountIn);
40 $PageCount=${$tmp[0]};
41 print STDERR "Total Pages = $PageCount\n";
42 $tmp=length($PageCount);
43 for ( $CurPage=1; $CurPage <= $PageCount; $CurPage++ ) {
44 @CurPage=($CurPage); $CurRef=\@CurPage;
45 $index=sprintf("%0${tmp}d",$CurPage);
46 $pdf->savePDFExtract( PDFPages=>$CurRef,PDFSaveAs=>"$index.pdf" );
47 }
48
49 =head1 DESCRIPTION
50
51 PDF Xtract module is derived from Noel Sharrok's PDF::Extract module, but a MUCH faster one. It is a group
52 of methods that allow the user to extract required pages as a new PDF document from a pre-existing PDF document.
53
54 PDF::Xtract is published as a separate module, because of some significant differences with PDF::Extract in variables
55 and functions implemented. While the code, for most part is a shameless copy of PDF::Extract, there are certain
56 changes in the logic that allow this module to be much much faster with large PDF files.
57
58 Notable differences between Xtract and Extract are also highlighted in this document
59
60 With PDF::Xtract one can:-
61
62 =over 4
63
64 =item *
65 Associate a PDF document to a PDF::Xtract object.
66
67 =item *
68 Get total number of pages in PDF document.
69
70 =item *
71 Extract required pages from a PDF document , as a new PDF document, in any specified page number order.
72
73 =item *
74 Specify name of file to save extracted PDF document.
75
76 =back
77
78 =cut
79 # ----------------------------- The Public Methods --------------------------------
80
81 # We can put the following 2 lines around a block to see the time taken to execute that.
82 # my $start=&lt;
83 # print STDERR "Timer says : ThisBloc: ",&lt-$start,"\n";
84
85 1 ▶sub new {
Subroutine new: called CC=2 SCAR=8.7
Pod documented
86 1 my $this = shift;
87 1 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
88 1 my $self = {};
89 1 bless $self, $class;
90 # Have a temperory file for storing output...
91 1 (undef,$TempExtractFile)=File::Temp->tempfile("xtract.tmp.XXXX");
92 0 $vars{PDFErrorLevel}=3;
93 0 $self->setEnv(@_);
94 0 return $self;
95 }
96
97 0 ▶sub getPDFExtract{
Subroutine getPDFExtract: not called CC=3 SCAR=24.8
Pod undocumented
98 0 local undef $/;
99 0 &setEnv(@_);
100 0 ▶ unless ( -f "$LastExtractFile" ) {&error(3,"No extract available at this time"); return 0}
Branch: 1 branch
BranchTrueFalseTotal
unless (-f "$LastExtractFile") 0 0 0
101 0 ▶ open ( tmp,"$LastExtractFile" ) or return undef; binmode tmp;
Branch: 1 branch
BranchTrueFalseTotal
open tmp, "$LastExtractFile" or return undef 0 0 0
102 0 return <tmp>;
103 }
104
105 0 ▶sub savePDFExtract{
Subroutine savePDFExtract: not called CC=6 SCAR=37.4
Pod undocumented
106 # Work expected by this is already done! (if possible); so just move the $TempExtractFile to $vars{PDFSaveAs}
107 0 &setEnv(@_);
108
109 0 ▶ if ( $vars{PDFSaveAs} eq $vars{PDFDoc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars{"PDFSaveAs"} eq $vars{"PDFDoc"}) 0 0 0
110 0 &error(3,"Attempt to clobber input file @ savePDFExtract!!");
111 0 return 0;
112 }
113
114 0 ▶ print STDERR "Info: Please read docs! Xtract autosaves pages to known PDFSaveAS\n" if ( $vars{PDFVerbose} );
Branch: 1 branch
BranchTrueFalseTotal
if $vars{"PDFVerbose"} 0 0 0
115
116 0 ▶ if ( ! $vars{PDFSaveAs} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $vars{'PDFSaveAs'}) { } 0 0 0
elsif ("$vars{'PDFSaveAs'}" eq "$LastExtractFile") { } 0 0 0
elsif (-f "$LastExtractFile") { } 0 0 0
117 0 &error(3,"No file name specified via PDFSaveAs, so savePDFExtract cant do anything!"); }
118 elsif ( "$vars{PDFSaveAs}" eq "$LastExtractFile" ) {
119 0 &error(1,"Redundant operation! Extract already there in $vars{PDFSaveAs}"); return 1;
120 }
121 elsif ( -f "$LastExtractFile" ) {
122 0 rename "$LastExtractFile","$vars{PDFSaveAs}";
123 0 $LastExtractFile="$vars{PDFSaveAs}";
124 0 return 1; }
125 else {
126 0 &error(3,"No extract available at this time!");}
127 }
128
129 0 ▶sub getPDFExtractVariables {
Subroutine getPDFExtractVariables: not called CC=2 SCAR=17.9
Pod undocumented
130 0 my @var; my $i;
131 0 shift;
132 0 foreach my $key (@_) {
133 0 $var[$i++]=$vars{$key};
134 }
135 0 @var;
136 }
137
138 0 ▶sub getVars { &getPDFExtractVariables(@_); }
Subroutine getVars: not called CC=1 SCAR=6.9
Pod undocumented
139
140 0 ▶sub setPDFExtractVariables {
Subroutine setPDFExtractVariables: not called CC=1 SCAR=6.9
Pod undocumented
141 0 my @var;
142 0 &setEnv( @_ );
143 0 shift;
144 0 my %var=@_;
145 0 &getVars( undef, keys %var);
146 }
147
148 0 ▶sub setVars { &setPDFExtractVariables(@_); }
Subroutine setVars: not called CC=1 SCAR=6.9
Pod undocumented
149
150 # ----------------------------- The Private Functions --------------------------------
151
152 0 ▶sub setEnv {
Subroutine setEnv: not called CC=43 SCAR=75.5
Pod undocumented
153 0 my (undef,%PDF)=@_;
154
155 0 ▶ if ($PDF{"PDFDebug"} || $vars{PDFDebug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PDF{"PDFDebug"} or $vars{"PDFDebug"}) 0 0 0
Condition0%0/3
$PDF{"PDFDebug"} or $vars{"PDFDebug"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$PDF{"PDFDebug"} or $vars{"PDFDebug"}
$PDF{"PDFDebug"} $vars{"PDFDebug"}
Truth table0%0/3
A or B
A= $PDF{"PDFDebug"}B= $vars{"PDFDebug"}
ABresult
1X1
011
000
156 0 $vars{PDFDebug}=$PDF{PDFDebug};
157 0 print STDERR "These variables are to be set\n";
158 0 foreach my $key (keys %PDF) { print STDERR "\t$key=\"$PDF{$key}\"\n"; }
159 }
160
161 0 ▶ if ( $PDF{PDFReadSize} ) { $vars{PDFReadSize}=$PDF{PDFReadSize} }
Branch: 1 branch
BranchTrueFalseTotal
if ($PDF{"PDFReadSize"}) 0 0 0
162 0 ▶ if ( $PDF{PDFWriteSize} ) { $vars{PDFWriteSize}=$PDF{PDFWriteSize} }
Branch: 1 branch
BranchTrueFalseTotal
if ($PDF{"PDFWriteSize"}) 0 0 0
163 0 ▶ if ( $PDF{RetainPDFComments} ) { $vars{RetainPDFComments}=$PDF{RetainPDFComments} }
Branch: 1 branch
BranchTrueFalseTotal
if ($PDF{"RetainPDFComments"}) 0 0 0
164 0 ▶ if ( $PDF{PDFVerbose} ) { $vars{PDFVerbose}=$PDF{PDFVerbose} }; # Put errors to STDERR too
Branch: 1 branch
BranchTrueFalseTotal
if ($PDF{"PDFVerbose"}) 0 0 0
165 0 ▶ if ( $PDF{PDFErrorLevel} ) { $vars{PDFErrorLevel}=$PDF{PDFErrorLevel} }; # Errors of higher gravity are
Branch: 1 branch
BranchTrueFalseTotal
if ($PDF{"PDFErrorLevel"}) 0 0 0
166 # reported
167 0 ▶ if ( $PDF{PDFErrorSize} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PDF{"PDFErrorSize"}) 0 0 0
168 0 $vars{PDFErrorSize}=$PDF{PDFErrorSize};
169 0 ▶ if ( $vars{PDFError} ) { splice @{$vars{PDFError}},0,-$vars{PDFErrorSize} }
Branch: 1 branch
BranchTrueFalseTotal
if ($vars{"PDFError"}) 0 0 0
170 }; # Size of array holding errors
171
172 0 ▶ if ( $PDF{PDFClean} ) { $vars{PDFClean}=$PDF{PDFClean} }; # Generate output only of there is no error
Branch: 1 branch
BranchTrueFalseTotal
if ($PDF{"PDFClean"}) 0 0 0
173
174 0 ▶ if ($PDF{PDFDoc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PDF{"PDFDoc"}) 0 0 0
175 # SS: Major changes here!
176 # File read mode changed to slurp, understanding the document etc are here.
177 # Initialisations:
178 0 $vars{PDFPageCountIn}=$vars{PDFPageCountOut}=$vars{PDFPageCountErr}=undef;
179 0 $vars{PDFPagesFound}=$vars{PDFPagesNotFound}={};
180
181 0 ▶ unless ( -f "$PDF{PDFDoc}" ) { &error(3,"PDF document \"$PDF{PDFDoc}\" not found",__FILE__,__LINE__); }
Branch: 1 branch
BranchTrueFalseTotal
unless (-f qq[$PDF{"PDFDoc"}]) 0 0 0
182 0 my $tmp=join(undef,stat("$PDF{PDFDoc}"));
183 0 ▶ if ( $vars{PDFDocStat} eq $tmp ) { # SS: Y should you bother if the file is same!
Branch: 1 branch
BranchTrueFalseTotal
if ($vars{'PDFDocStat'} eq $tmp) { } 0 0 0
184 0 ▶ print STDERR "PDFXtract: You are re-setting object to same PDF Document! I am ignoring it.\n" if ( $vars{PDFVerbose}>0 );
Branch: 1 branch
BranchTrueFalseTotal
if $vars{"PDFVerbose"} > 0 0 0 0
185 } else {
186 # A new document is being processed.
187 0 $vars{PDFDocStat}=$tmp;
188 0 ▶ if ( ! open FILE, "$PDF{PDFDoc}" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not open(FILE, "$PDF{'PDFDoc'}")) { } 0 0 0
189 0 &error(3,"Can't open PDF document \"$PDF{PDFDoc}\" to read\n",__FILE__,__LINE__);
190 } else {
191 #----------------------------------
192 0 my $CarryOver; my $ChunkCount;
193 0 binmode FILE;
194 0 ▶ while(read FILE,my $str,$vars{PDFReadSize}?$vars{PDFReadSize}:1024000) {
Branch: 2 branches
BranchTrueFalseTotal
if (read FILE, my $str, $vars{"PDFReadSize"} ? $vars{"PDFReadSize"} : 1024000) 0 0 0
$vars{'PDFReadSize'} ? : 0 0 0
195 0 ▶ $ChunkCount++; print STDERR "Reading chunk number $ChunkCount\r" if $vars{PDFVerbose};
Branch: 1 branch
BranchTrueFalseTotal
if $vars{"PDFVerbose"} 0 0 0
196 0 my @tmp; my $pieces=(@tmp=split(/endobj\s*$CRLF/,"$CarryOver$str",-1)); undef $CarryOver;
197 0 my $count=0;
198 0 foreach $tmp (@tmp) {
199 0 $count++;
200 0 ▶ if ( $count == $pieces ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == $pieces) { } 0 0 0
201 0 $CarryOver=$tmp; last;
202 } else {
203 0 ▶ if ( $tmp=~/^(.*?)(\d+)(\s+\d+\s+obj)$CRLF(.*)$/si ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp =~ /^(.*?)(\d+)(\s+\d+\s+obj)$CRLF(.*)$/is) { } 0 0 0
204 0 $balance.=$1; my $obj=$2; $objval{$obj}="$obj$3\015\012$4endobj\015\012\015\012";
205 0 my $ref='(?:\s*\d+\s+\d+\s+R\s*)';
206 0 my @allRefs=($objval{$obj}=~/\/(\S+?[\[\s]+${ref}+[\]\s]?)/gs);
207 0 ▶ next unless @allRefs;
Branch: 1 branch
BranchTrueFalseTotal
unless @allRefs 0 0 0
208 0 foreach ( @allRefs ) {
209 0 ▶ if ( /^kids/is ) { push (@{$kids{$obj}},&findRefs($_)); }
Branch: 2 branches
BranchTrueFalseTotal
if (/^kids/is) { } 0 0 0
elsif (/^parent/is) { } 0 0 0
210 0 elsif ( /^parent/is ) { $parent{$obj}=(&findRefs($_))[0] }
211 0 else { push (@{$Referals{$obj}},&findRefs($_)); }
212 }
213 0 } else { $balance.=$tmp; }
214 }
215 }
216 0 ▶ } $balance.=$CarryOver; print STDERR "\n" if $vars{PDFVerbose};
Branch: 1 branch
BranchTrueFalseTotal
if $vars{"PDFVerbose"} 0 0 0
217 #----------------------------------
218 0 $vars{"PDFDoc"}=$PDF{"PDFDoc"};
219 # SS: Understand the document .....
220 #----------------------------------
221 # getTrailer, Info etc.
222 0 ▶ if ( $balance=~/(trailer\s*<<.*?>>\s*)/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($balance =~ /(trailer\s*<<.*?>>\s*)/s) 0 0 0
223 0 $trailerObject=$1;
224 0 $trailerObject=~s/\/Size\s+\d+/\/Size __Size__/s;
225 0 $trailerObject=~s/\/Prev.*?$CRLF//s;
226
227 0 ▶ if ( $trailerObject=~/\/Root\s+(\d+)\s+0\s+R/s ) { $RootObject=$1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($trailerObject =~ m[/Root\s+(\d+)\s+0\s+R]s) 0 0 0
228 0 ▶ if ( $trailerObject=~/\/Encrypt\s+(\d+)\s+0\s+R/s ) { $EncryptedObject=$1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($trailerObject =~ m[/Encrypt\s+(\d+)\s+0\s+R]s) 0 0 0
229 0 ▶ if ( $trailerObject=~/\/Info\s+(\d+)\s+0\s+R/s ) { $InfoObject=$1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($trailerObject =~ m[/Info\s+(\d+)\s+0\s+R]s) 0 0 0
230 }
231 0 ▶ if ( $objval{$RootObject}=~/\/Pages\s+(\d+)\s+0\s+R/s ) { $CatalogPages=$1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($objval{$RootObject} =~ m[/Pages\s+(\d+)\s+0\s+R]s) 0 0 0
232
233 0 &getPages($CatalogPages);
234 }
235 }
236 }
237
238 0 ▶ if ( exists $PDF{PDFSaveAs} ) { # we also want to be able to set PDFSaveAs to nothing ("")
Branch: 1 branch
BranchTrueFalseTotal
if (exists $PDF{"PDFSaveAs"}) 0 0 0
239 0 $vars{"PDFSaveAs"}=$PDF{"PDFSaveAs"};
240 }
241
242 0 ▶ if ( $PDF{PDFPages} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PDF{"PDFPages"}) 0 0 0
243
244 0 $vars{PDFPageCountOut}=$vars{PDFPageCountErr}=undef;
245 0 $vars{PDFPagesFound}=$vars{PDFPagesNotFound}={};
246
247 # SS: Major change. We plan to accept only array as an input (well, a reference to an array!)
248 0 ▶ my $tmp=ref($PDF{PDFPages}); $tmp=$tmp?$tmp:"Not even a reference!";
Branch: 1 branch
BranchTrueFalseTotal
$tmp ? : 0 0 0
249 0 ▶ unless ( $tmp eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmp eq "ARRAY") 0 0 0
250 0 &error(3,"Value of PDFPages has to be an array reference, now it is $tmp, No output possible.");
251 0 return 1; }
252 0 my @tmp=@{$PDF{PDFPages}};
253 0 ▶ unless ( @tmp ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@tmp) 0 0 0
254 0 &error(3,"Can't get PDF Pages. No page numbers were set with 'PDFPages' ",__FILE__,__LINE__);
255 }
256 0 @{$vars{PDFPages}}=@tmp;
257 0 $vars{PDFPagesFound}=""; # $vars{PDFExtract}="";
258 0 %IncludeObjects=(); # %IncludeObjects=undef;
259 0 %BabyCountOfObject=();
260
261 0 &getPDFDoc(@{$vars{PDFPages}});
262 0 &makePDF;
263 }
264
265 0 ▶ if ( $PDF{"PDFDebug"} || $vars{PDFDebug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PDF{"PDFDebug"} or $vars{"PDFDebug"}) 0 0 0
Condition0%0/3
$PDF{"PDFDebug"} or $vars{"PDFDebug"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$PDF{"PDFDebug"} or $vars{"PDFDebug"}
$PDF{"PDFDebug"} $vars{"PDFDebug"}
Truth table0%0/3
A or B
A= $PDF{"PDFDebug"}B= $vars{"PDFDebug"}
ABresult
1X1
011
000
266 0 print "These variables have been set\n";
267 0 foreach my $key (keys %vars) { print "\t$key=\"$vars{$key}\"\n"; }
268
269 0 delete($PDF{PDFDebug});
270 }
271
272 # A little buggy, but easier to read in other Xtract environment vars.
273 # Allows populating any variables with name starting with "My" to the object's space.
274 0 ▶ foreach ( keys %PDF ) { $vars{$_}=$PDF{$_} if ( /^My/ ) }; %PDF=();
Branch: 1 branch
BranchTrueFalseTotal
if /^My/ 0 0 0
275 }
276
277 #------------------------------------ support Routines --------------------------------------------
278
279 0 ▶sub error {
Subroutine error: not called CC=5 SCAR=34.0
Pod undocumented
280 # Populates an array of maximum size PDFErrorSize
281 0 my %error_level=(0=>"Silly", 1=>"Info", 2=>"Warn", 3=>"Error");
282
283 0 my ($error_level,$error)=@_;
284 0 ▶ return 0 unless ( $error_level >= $vars{PDFErrorLevel} ); # Ignore those errors below set errorlevel
Branch: 1 branch
BranchTrueFalseTotal
unless $error_level >= $vars{"PDFErrorLevel"} 0 0 0
285 0 ▶ if ( $vars{PDFError} && ( @{$vars{PDFError}} >= $vars{PDFErrorSize}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars{"PDFError"} and @{$vars{"PDFError"};} >= $vars{"PDFErrorSize"}) 0 0 0
Condition0%0/3
$vars{"PDFError"} and @{$vars{"PDFError"};} >= $vars{"PDFErrorSize"}
!ll&&!rl&&r
000
MC/DC0%0/2
$vars{"PDFError"} and @{$vars{"PDFError"};} >= $vars{"PDFErrorSize"}
$vars{"PDFError"} @{$vars{"PDFError"};} >= $vars{"PDFErrorSize"}
Truth table0%0/3
A and B
A= $vars{"PDFError"}B= @{$vars{"PDFError"};} >= $vars{"PDFErrorSize"}
ABresult
0X0
100
111
286 0 shift @{$vars{PDFError}};
287 }
288 0 my $error_string="$error_level{$error_level}: $error";
289 0 push ( @{$vars{PDFError}},"$error_string");
290 0 ▶ print STDERR "$error_string\n" if ( $vars{PDFVerbose} );
Branch: 1 branch
BranchTrueFalseTotal
if $vars{"PDFVerbose"} 0 0 0
291 0 return 1;
292 }
293
294 #------------------------------------ PDF Page Routines --------------------------------------------
295
296 0 ▶sub getPages {
Subroutine getPages: not called CC=3 SCAR=24.8
Pod undocumented
297 # Populates Page Number -> Page Object map (%page)
298 0 my @tmp=(shift); my $pageno;
299 0 ▶ while ( @tmp ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tmp) 0 0 0
300 0 my $obj=int(shift @tmp);
301 0 ▶ if ( $kids{$obj} ) { unshift (@tmp,@{$kids{$obj}}); }
Branch: 1 branch
BranchTrueFalseTotal
if ($kids{$obj}) { } 0 0 0
302 0 else { $page{++$pageno}=$obj;
303 }
304 }
305 0 $vars{PDFPageCountIn}=$pageno;
306 }
307
308 0 ▶sub Includes{
Subroutine Includes: not called CC=4 SCAR=30.0
Pod undocumented
309 # Populates the hash %Includes with (object id)->(objectes refered by object id).
310 0 my @getRefs=@_;
311 0 foreach my $obj ( @getRefs ) {
312 0 foreach my $refered (@{$Referals{$obj}}) {
313 0 ▶ next if ( $IncludeObjects{$refered} );
Branch: 1 branch
BranchTrueFalseTotal
if $IncludeObjects{$refered} 0 0 0
314 0 $IncludeObjects{$refered}++;
315 0 &Includes($refered);
316 }
317 }
318 }
319
320 0 ▶sub getPDFDoc {
Subroutine getPDFDoc: not called CC=5 SCAR=34.0
Pod undocumented
321 # Key function!!
322 # For the given set of pages, generate the page tree for output.
323
324 # Initialisations
325 0 my @pickPages=@_;
326 0 %BabiesOfObject=();
327 0 $vars{PDFPagesFound}=$vars{PDFPageCountOut}=$vars{PDFPageCountErr}=0;
328 0 $vars{PDFPagesFound}=$vars{PDFPagesNotFound}=();
329 0 my $tmp; my $err=0;
330
331 0 foreach my $pageno ( @pickPages ) {
332 0 ▶ unless ( $page{$pageno} ) { # if the page object is not in %page
Branch: 1 branch
BranchTrueFalseTotal
unless ($page{$pageno}) 0 0 0
333 0 $err++;
334 0 &error(2,"Page No. $pageno is not there in the given PDF file($vars{PDFDoc})");
335 0 $vars{PDFPageCountErr}++; push(@{$vars{PDFPagesNotFound}},$pageno);
336 0 next;
337 }
338 0 $tmp=$page{$pageno};
339 0 $vars{PDFPageCountOut}++; push(@{$vars{PDFPagesFound}},$pageno);
340 0 &Includes($tmp); # Will add refered objs. to %IncludeObjects
341 0 ▶ while( $parent{$tmp} ) { # Contruct/Adjust the object tree for this page
Branch: 1 branch
BranchTrueFalseTotal
if ($parent{$tmp}) 0 0 0
342 # Array because, it is important to keep the kids order.
343 0 ▶ unless ( grep { /^$tmp$/ } @{$BabiesOfObject{$parent{$tmp}}} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {/^$tmp$/;} @{$BabiesOfObject{$parent{$tmp}};}) 0 0 0
344 0 push (@{$BabiesOfObject{$parent{$tmp}}},$tmp);
345 0 $IncludeObjects{$parent{$tmp}}++;
346 }
347 0 $BabyCountOfObject{$parent{$tmp}}++;
348 0 $tmp=$parent{$tmp};
349 }
350 0 $IncludeObjects{$page{$pageno}}++;
351 }
352 0 return $err;
353 }
354
355 0 ▶sub makePDF {
Subroutine makePDF: not called CC=19 SCAR=59.4
Pod undocumented
356
357 # Check attempts to clobber input file.
358 0 ▶ if ( $vars{PDFSaveAs} eq $vars{PDFDoc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars{"PDFSaveAs"} eq $vars{"PDFDoc"}) 0 0 0
359 0 &error(3,"Attempt to clobber input file - check PDFSaveAs!!");
360 0 return 0;
361 }
362
363 0 ▶ if ( $vars{PDFClean} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars{"PDFClean"}) 0 0 0
364 0 ▶ if ( $vars{PDFPageCountErr}>0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars{"PDFPageCountErr"} > 0) 0 0 0
365 0 &error(3,"PDFClean: $vars{PDFPageCountErr} pages to be extracted were not found");
366 0 close FILE; unlink $LastExtractFile; return undef;
367 }
368 }
369 0 ▶ if ( $vars{PDFPageCountOut}<1 ) { # Clean-up and return if we haven't got any page!
Branch: 1 branch
BranchTrueFalseTotal
if ($vars{"PDFPageCountOut"} < 1) 0 0 0
370 0 &error(3,"No pages could be extracted!");
371 0 close FILE; unlink $LastExtractFile; return undef;
372 }
373
374 0 ▶ my $eol=sprintf "\n"; if ( length ($eol) <2 ) { $eol=" $eol" }
Branch: 1 branch
BranchTrueFalseTotal
if (length $eol < 2) 0 0 0
375
376 # Decide the file to hold the extracted PDF Stream
377 0 close FILE; # unlink "$LastExtractFile"; #######
378 0 ▶ $LastExtractFile=$vars{PDFSaveAs}?$vars{PDFSaveAs}:$TempExtractFile;
Branch: 1 branch
BranchTrueFalseTotal
$vars{'PDFSaveAs'} ? : 0 0 0
379 0 ▶ open(FILE,">$LastExtractFile") or die; binmode FILE;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">$LastExtractFile" or die 0 0 0
380
381 0 my %XrefBlock=();
382 0 $objval{$RootObject}="$RootObject 0 obj\n<<\n/Type /Catalog\n/Pages $CatalogPages 0 R\n>>\nendobj\n";
383 0 $IncludeObjects{$RootObject}++; # print STDERR "Root ($RootObject) Added\n";
384 0 $IncludeObjects{$InfoObject}++; # print STDERR "Info ($InfoObject) Added\n";
385
386 0 (my $CurrentBalance=$balance)=~s/\%\%EOF.*//s;
387 0 $CurrentBalance=~s/^(.*?)$CRLF(.*?)$CRLF//s;
388
389 0 print FILE "$1\015\012$2\015\012";
390 0 my $startXref=length("$1\015\015$2\015\012");
391
392 0 ▶ if ( $vars{RetainPDFComments} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vars{"RetainPDFComments"}) 0 0 0
393 0 ▶ while ( $CurrentBalance=~/\s*(%.*?$CRLF.*?$CRLF)/gs ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CurrentBalance =~ /\s*(%.*?$CRLF.*?$CRLF)/gs) 0 0 0
394 0 print FILE "$1";
395 0 $startXref+=length($1);
396 }
397 }
398 0 my $xref=undef;
399 0 my $xref="xxxxxxxxxx 65535 f$eol";
400 0 $vars{PDFObjCountOut}=undef;
401
402 0 ▶ my %addobj; my %objsize; my $accumulation; $vars{PDFWriteSize}=$vars{PDFWriteSize}?$vars{PDFWriteSize}:1024000;
Branch: 1 branch
BranchTrueFalseTotal
$vars{'PDFWriteSize'} ? : 0 0 0
403 0 my $ExpectObjID=1; # Next Object ID
404 0 my $lastObjID=undef; my $counter=0; my $CurXrefBlock=0;
405 0 $XrefBlock{0}[1]="0000000000 65535 f \015\012";
406 0 foreach my $objid ( sort {$a<=>$b} keys %IncludeObjects ) {
407 0 $lastObjID=$objid;
408 0 $addobj{$objid}=$objval{$objid};my $babiesofobject=undef;
409 0 foreach ( @{$BabiesOfObject{$objid}} ) { $babiesofobject.="$_ 0 R "; }
410 0 ▶ if ( $babiesofobject ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($babiesofobject) 0 0 0
411 0 $addobj{$objid}=~s/\/Kids\s+.*?\]/\/Kids \[$babiesofobject\]/s;
412 0 $addobj{$objid}=~s/\/Count\s+\d+/\/Count $BabyCountOfObject{$objid}/s;
413 }
414
415 0 ▶ if ( int($objid) > $ExpectObjID ) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $objid > $ExpectObjID) { } 0 0 0
416 0 my $tmp=$ExpectObjID;
417 0 for ( $ExpectObjID..int($objid)-1 ) {
418 0 $xref.="xxxxxxxxxx 00001 f$eol";
419 0 my $x=sprintf ("%0.10d",$ExpectObjID);
420 0 $xref=~s/xxxxxxxxxx/$x/;
421 }
422 0 $xref.=sprintf("%0.10d %0.5d n$eol",$startXref,0);
423 } else {
424 0 $xref.=sprintf("%0.10d %0.5d n$eol",$startXref,0);
425 }
426
427 0 $ExpectObjID=$objid+1;
428
429 0 $objsize{$objid}=length($addobj{$objid});
430 0 $startXref+=$objsize{$objid};
431 0 $accumulation+=$objsize{$objid};
432
433 0 ▶ if ( $accumulation > $vars{PDFWriteSize} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($accumulation > $vars{"PDFWriteSize"}) 0 0 0
434 0 foreach ( sort {$2<=>$b} keys %addobj ) { print FILE "$addobj{$_}"; delete $addobj{$_} };
435 0 $accumulation=0;
436 0 %addobj=();
437 }
438 0 $vars{PDFObjCountOut}++;
439 }
440 0 foreach ( sort {$a<=>$b} keys %addobj ) { print FILE "$addobj{$_}"; delete $addobj{$_} };
441 0 $lastObjID++;
442
443 0 $xref=~s/xxxxxxxxxx/0000000000/;
444 0 print FILE "xref\0120 $lastObjID\n$xref";
445
446 0 (my $CurrentTrailer=$trailerObject)=~s/__Size__/$lastObjID/s;
447 0 print FILE "\015\012$CurrentTrailer\015\012startxref\015\012$startXref\015\012\%\%EOF\015\012";
448 0 close FILE;
449 }
450
451 0 ▶sub findRefs{
Subroutine findRefs: not called CC=1 SCAR=6.9
Pod undocumented
452 0 return (/(?:(\d+)\s+\d+\s+R\s*)/gsi);
453 }
454
455 #sub lt{
456 # # Stuff used while debugging and performance checks
457 # my @timer=gettimeofday();
458 # my $timeNow=$timer[0]+$timer[1]/1000000;
459 # return $timeNow;
460 #}
461 =head1 AUTHOR
462
463 Sunil S, sunils_at_hpcl_co_in
464
465 Created by modifying PDF::Extract module by Noel Sharrock (http://www.lgmedia.com.au/PDF/Extract.asp)
466 (Without PDF::Extract this would not be there!)
467
468 Many thanx to inspiration by my collegues at Hindustan Petroleum Corporation Limited, Mumbai, India.
469
470 =head1 COPYRIGHT
471
472 Copyright (c) 2005 by Sunil S. All rights reserved.
473
474
475 =head1 LICENSE
476
477 This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself,
478 i.e., under the terms of the ``Artistic License'' or the ``GNU General Public License''.
479
480 The C library at the core of this Perl module can additionally be redistributed and/or modified
481 under the terms of the ``GNU Library General Public License''.
482
483 =head1 DISCLAIMER
484
485 This package is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
486 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
487
488 See the ``GNU General Public License'' for more details.
489
490 PDF::Xtract - Extracting sub PDF documents from a multipage PDF document
491
492
493 =cut
494
495 1 1;
496
497 =head1 Notes
498
499
500 Fri Jun 17 00:01:21 IST 2005
501
502 Version 0.08
503
504 Output does not open properly in Acrobat Reader properly ! Xref table writing changed back to
505 what Noel was doing.
506
507 Fri Apr 15 21:15:17 IST 2005
508
509 Version 0.07
510
511 Trying to accelerate output generation, which seems to be the remaining bottleneck.
512
513 Thu Apr 7 15:02:22 IST 2005
514
515 Version 0.06
516
517 Noticed that Xtract fails with very large PDFs (>400MB). It is now fixed by changing the way the file is read and
518 understood. Fringe benefit: module uses less memory than before. Additional variable is introduced: PDFReadSize,
519 specify the number of bytes to read at a time when reading the input file.
520
521 Thu Mar 10 15:02:47 IST 2005
522
523 Noticed a problem with include objects! Work around done.
524
525 Thu Feb 20 15:02:47 IST 2005
526
527 Operational sequences within the module is being changed. New organisation will be as below:
528
529 Essentioal variable for doing anything is PDFDoc.
530 Extraction and making of document will run as and when PDFPages is defined. It will be generated into
531 the disk file named as PDFSaveAs if one exist, else will be taken to default extract file named as
532 $TempExtractFile.
533
534 Populating the PDFExtract is now secondary! If some one ask for that, we will return the content of the
535 file $TempExtractFile
↑ Summary