PDF/Parse.pm untested

filter: Statementstmt 0.0% 0 / 169 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 266 CC 24 SCAR 33.3
CC
24
Cov
0%
LoadPageSubtree
56.0
PageSize
42.0
ReadCrossReference_pass1
30.0
CRAP 27.9
1 #
2 # PDF::Parse.pm, version 1.11 February 2000 antro
3 #
4 # Copyright (c) 1998 - 2000 Antonio Rosella Italy antro@tiscalinet.it, Johannes Blach dw235@yahoo.com
5 #
6 # Free usage under the same Perl Licence condition.
7 #
8  
9 package PDF::Parse;
10  
11 $PDF::Parse::VERSION = "1.11";
12  
13 =pod
14  
15 =head1 NAME
16  
17 PDF::Parse - Library with parsing functions for PDF library
18  
19 =head1 SYNOPSIS
20  
21 use PDF::Parse;
22  
23 $pdf->TargetFile($filename);
24 $pdf->LoadPageInfo;
25  
26 $version = $pdf->Version;
27 $bool = $pdf->IsaPDF;
28 $bool = $pdf->IscryptPDF;
29  
30 $info = $pdf->GetInfo ($key);
31 $pagenum = $pdf->Pages;
32  
33 @size = $pdf->PageSize ($page);
34 # or
35 @size = $pdf->PageSize;
36  
37 $rotation = $pdf->PageRotation ($page);
38 # or
39 $rotation = $pdf->PageRotation;
40  
41 =head1 DESCRIPTION
42  
43 The main purpose of the PDF::Parse library is to provide parsing functions
44 for the more general PDF library.
45  
46 =head1 Methods
47  
48 The available methods are:
49  
50 =cut
51  
52 require 5.005;
53 require PDF::Core;
54  
55 use strict;
56 use Carp;
57 use Exporter ();
58  
59 use vars qw(@ISA @EXPORT_OK);
60  
61 @ISA = qw(Exporter PDF::Core);
62  
63 @EXPORT_OK = qw( LoadPageInfo GetInfo TargetFile
64 Pages PageSize PageRotation IsaPDF
65 Version IscryptPDF );
66  
67 #################################################################
68 sub ReadCrossReference_pass1 {
69 my $fd = shift;
70 my $offset=shift;
71 my $self=shift;
72  
73 my $initial_number;
74 my $obj_counter=0;
75 my $global_obj_counter=0;
76 my $buf;
77  
78 binmode $fd;
79  
80 $_=PDF::Core::PDFGetline ($fd,\$offset);
81  
82 die "Can't read cross-reference section, according to trailer\n" if ! /xref\r?\n?/ ;
83  
84 while () {
85 $_=PDF::Core::PDFGetline ($fd,\$offset);
86 s/^\n//;
87 s/^\r//;
88 last if (m/\btrailer\b/) ;
89 #
90 # An Object
91 #
92 /^\d+\s+\d+\s+n\r?\n?/ && do { my $buf =$_;
93 my $ind = $initial_number + ($obj_counter++);
94 ( not defined $self->{Objects}[$ind] )&&
95 do { $self->{Objects}[$ind] = int substr($buf,0,10);
96 $self->{Gen_Num}[$ind] = int substr($buf,11,5);
97 };
98 $_=$buf;
99 s/^.{18}//;
100 next ;
101 };
102 #
103 # A Freed Object
104 #
105 /^\d+\s+\d+\s+f\r?\n?/ && do { my $buf =$_;
106 my $objects_generation_nr = substr($buf,11,5);
107 my $Num=substr($buf,0,10);
108 my $ind = $initial_number + ($obj_counter++);
109 # $ind = $ind . "_" . $objects_generation_nr;
110 $self->{Objects}[$ind] = - $Num;
111 $self->{Gen_Num}[$ind] = $objects_generation_nr;
112 $_=$buf;
113 s/^.{18}//;
114 next ;
115 };
116 #
117 # A subsection
118 #
119 /^\d+\s+\d+\r?\n?/ && do {
120 my $buf = $_ ;
121 $initial_number = $buf;
122 $initial_number=~ s/^(\d+)\s+\d+\r?\n?.*/$1/;
123 $global_obj_counter += $obj_counter;
124 $obj_counter=0;
125 next ;
126 };
127 }
128  
129 $global_obj_counter +=$obj_counter;
130 #
131 # Now the trailer for updates
132 #
133  
134 #
135 # Skip to start of dictionary.
136 #
137 until (m/<</)
138 {
139 $_=PDF::Core::PDFGetline ($fd,\$offset);
140 }
141  
142 #
143 # Read the dictionary
144 #
145 my %trailer = ( PDF::Core::PDFGetPrimitive ($fd, $offset) );
146  
147 if ($self->{"Trailer"}{"/Root"} eq "")
148 {
149 $self->{"Trailer"} = \%trailer;
150 #
151 # This code is here for backward compatibility only. If the content
152 # of the root trailer is needed, use $self->{"Trailer"} instead.
153 #
154 $self->{"Cross_Reference_Size"} = $trailer{"/Size"};
155 $self->{"Root_Object"} = $trailer{"/Root"};
156 $self->{"Crypt_Object"} = $trailer{"/Encrypt"};
157 }
158 if ($trailer{"/Prev"} =~ m/^\d+$/)
159 {
160 $self->{"Updated"} = 1;
161 my $old_seek = tell $fd;
162 $global_obj_counter += ReadCrossReference_pass1 ($fd,
163 $trailer{"/Prev"}, $self );
164 seek $fd, $old_seek, 0;
165 }
166  
167  
168 return $global_obj_counter;
169 }
170  
171 #################################################################
172 sub LoadPageSubtree (\*$;%)
173 {
174 my $self = shift;
175 my $ref = shift;
176 my %inheritance = @_ ;
177  
178 my $data = $self->GetObject ($ref);
179  
180 # Check which attributes are inherited. Adobe did not add any new
181 # inherited attributes in version 1.2 or later, so this list is
182 # complete.
183  
184 # Do simple values.
185 foreach my $key ("/Rotate", "/Dur", "/Hid", "/Trans",
186 "/MediaBox", "/CropBox")
187 {
188 if (defined ($data->{$key}))
189 {
190 # Check if it is an indirect reference
191 if ($data->{$key} =~ m/^\d+ \d+ R$/)
192 {
193 my $dataref = $data->{$key};
194 do
195 {
196 $dataref = $self->GetObject ($dataref);
197 }
198 while ($dataref =~ m/^\d+ \d+ R$/);
199  
200 if (UNIVERSAL::isa ($data, "ARRAY"))
201 {
202 $inheritance{$key} = [];
203 foreach my $i (@{$data})
204 {
205 # Each element may be a reference.
206 while ($i =~ m/^\d+ \d+ R$/)
207 {
208 $i = $self->GetObject ($i);
209 }
210  
211 push @{$inheritance{$key}}, $i;
212 }
213 }
214 else
215 {
216 $inheritance{$key} = $dataref;
217 }
218 }
219 else
220 {
221 $inheritance{$key} = $data->{$key};
222 }
223 }
224 }
225  
226 # If this objects contains ressources, replace information in inheritance
227 $inheritance{"Resource_Object"} = $data->{"/Resources"}
228 if (defined ($data->{"/Resources"}));
229  
230 if ($data->{"/Type"} eq "/Pages")
231 {
232 # It's just an intermediate Node
233 foreach my $kid (@{$data->{"/Kids"}})
234 {
235 $self->LoadPageSubtree ($kid, %inheritance);
236 }
237 }
238 elsif ($data->{"/Type"} eq "/Page")
239 {
240 # We have a real page!
241 $inheritance{"Page_Object"} = $ref;
242 push @{$self->{"Page"}}, +{ %inheritance };
243 }
244 else
245 {
246 # Strange stuff. Complain and discard.
247 carp "While loading pages got object of type '", $data->{"/Type"}, "'";
248 }
249 }
250  
251 #################################################################
252 =pod
253  
254 =head2 TargetFile ( filename )
255  
256 This method links the filename to the pdf descriptor and parses all
257 kind of header information.
258  
259 =cut
260  
261 sub TargetFile {
262 my $self = shift;
263 my $file = shift;
264  
265 croak "Already linked to the file ",$self->{File_Name},"\n"
266 if $self->{File_Name} ;
267
268 my $offset;
269  
270 if ( $file ) {
271 open(FILE, "< $file") or croak "can't open $file: $!";
272 binmode FILE;
273 $self->{File_Name} = $file ;
274 $self->{File_Handler} = \*FILE;
275 my $buf;
276 read(FILE,$buf,4);
277 if ( $buf ne "%PDF" ) {
278 print "File $_[0] is not PDF compliant !\n" if $PDF::Verbose ;
279 return 0 ;
280 }
281 read(FILE,$buf,4);
282 $buf =~ s/-//;
283 $self->{Header}= $buf;
284 seek FILE,-50,2;
285 read( FILE, $offset, 50 );
286 $offset =~ s/[^s]*startxref\r?\n?(\d*)\r?\n?%%EOF\r?\n?/$1/;
287  
288 $self->{"Last_XRef_Offset"} = $offset;
289 ReadCrossReference_pass1 (\*FILE, $offset, $self);
290 $self->{"Info"} = $self->GetObject ($self->{"Trailer"}{"/Info"});
291 $self->{"Catalog"} = $self->GetObject ($self->{"Trailer"}{"/Root"});
292 $self->{"PageTree"} = $self->GetObject ($self->{"Catalog"}{"/Pages"});
293 return 1;
294 } else {
295 croak "I need a file name (!)";
296 }
297 }
298  
299 #################################################################
300 =pod
301  
302 =head2 LoadPageInfo
303  
304 This function loads the information for all pages. This process can
305 take some time for big PDF-files.
306  
307 =cut
308  
309 sub LoadPageInfo (\*)
310 {
311 my $self = shift;
312  
313 # Reset Page Array
314 $#{$self->{"Page"}} = -1;
315  
316 # Recurse
317 $self->LoadPageSubtree ($self->{"Catalog"}{"/Pages"});
318 }
319  
320  
321  
322 #################################################################
323 =pod
324  
325 =head2 Version
326  
327 Returns the PDF version used for writing the object file.
328  
329 =cut
330  
331 sub Version {
332 return ($_[0]->{Header});
333 }
334  
335 #################################################################
336 =pod
337  
338 =head2 IsaPDF
339  
340 Returns true, if the file could be parsed and is a PDF-file.
341  
342 =cut
343  
344 sub IsaPDF {
345 return ($_[0]->{Header} != undef) ;
346 }
347  
348 #################################################################
349 =pod
350  
351 =head2 IscryptPDF
352  
353 Returns true if the PDF contains a crypt object. This indicates that
354 the data of the PDF-File is encrypted. In this case, not all function
355 work as expected.
356  
357 =cut
358  
359 sub IscryptPDF {
360 return ($_[0]->{Crypt_Object} != undef) ;
361 }
362  
363 #################################################################
364 =pod
365  
366 =head2 GetInfo ( key )
367  
368 Returns the various information contained in the info section of a PDF
369 file (if present). A PDF file can have:
370  
371 a title ==> GetInfo ("Title")
372 a subject ==> GetInfo ("Subject")
373 an author ==> GetInfo("Author")
374 a creation date ==> GetInfo("CreationDate")
375 a creator ==> GetInfo("Creator")
376 a producer ==> GetInfo("Producer")
377 a modification date ==> GetInfo("ModDate")
378 some keywords ==> GetInfo("Keywords")
379  
380 =cut
381  
382 sub GetInfo (\*$)
383 {
384 my $self = shift;
385 my $type = shift;
386  
387 return PDF::Core::UnQuoteString ($self->{"Info"}{"/" . $type})
388 }
389  
390 #################################################################
391 =pod
392  
393 =head2 Pages
394  
395 Returns the number of pages of the PDF-file.
396  
397 =cut
398  
399 sub Pages
400 {
401 my $self = shift;
402  
403 return $self->{"PageTree"}{"/Count"};
404 }
405  
406 #################################################################
407 =pod
408  
409 =head2 PageSize ( [ page ] )
410  
411 Returns the size of a page in the PDF-file. If no parameter is given,
412 the default size of the root page will be returned. This value may be
413 overridden for any page.
414  
415 If the size of an individual page is requested and the page data is
416 not already loaded, the method B<LoadPageInfo> will be executed. This
417 may take some time for large PDF-files. The size of the root page is
418 always available and will never execute B<LoadPageInfo>.
419  
420 =cut
421  
422 sub PageSize (;$)
423 {
424 my $self = shift;
425 my $page = shift;
426  
427 if ($page > 0)
428 {
429 return undef if ($page > $self->{"PageTree"}{"/Count"});
430 $self->LoadPageInfo unless ($#{$self->{"Page"}} >= 0);
431
432 return @{$self->{"Page"}[$page - 1]{"/MediaBox"}}
433 if (defined $self->{"Page"}[$page - 1]{"/MediaBox"});
434 }
435 else
436 {
437 return @{$self->{"PageTree"}{"/MediaBox"}}
438 if (defined $self->{"PageTree"}{"/MediaBox"});
439 }
440  
441 return undef;
442 }
443  
444 #################################################################
445 =pod
446  
447 =head2 PageRotation ( [ page ] )
448  
449 Returns the rotation of a page in the PDF-file. If no parameter is given,
450 the default rotation of the root page will be returned. This value may be
451 overridden for any page.
452  
453 If the rotation of an individual page is requested and the page data is
454 not already loaded, the method B<LoadPageInfo> will be executed. This
455 may take some time for large PDF-files. The rotation of the root page is
456 always available and will never execute B<LoadPageInfo>.
457  
458 =cut
459 sub PageRotation (;$)
460 {
461 my $self = shift;
462 my $page = shift;
463  
464 my $rotate = 0;
465  
466 if ($page > 0)
467 {
468 return undef if ($page > $self->{"PageTree"}{"/Count"});
469 $self->LoadPageInfo unless ($#{$self->{"Page"}} >= 0);
470
471 $rotate = $self->{"Page"}[$page - 1]{"/Rotate"};
472 }
473 else
474 {
475 $rotate = $self->{"PageTree"}{"/Rotate"};
476 }
477  
478 print "Rotation ", 0 + $rotate if ($PDF::Verbose);
479  
480 return 0 + $rotate;
481 }
482 #################################################################
483 1;
484 __END__