3 // FPDI - Version 1.4.2
5 // Copyright 2004-2011 Setasign - Jan Slabon
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
20 if (!defined ('PDF_TYPE_NULL'))
21 define ('PDF_TYPE_NULL', 0);
22 if (!defined ('PDF_TYPE_NUMERIC'))
23 define ('PDF_TYPE_NUMERIC', 1);
24 if (!defined ('PDF_TYPE_TOKEN'))
25 define ('PDF_TYPE_TOKEN', 2);
26 if (!defined ('PDF_TYPE_HEX'))
27 define ('PDF_TYPE_HEX', 3);
28 if (!defined ('PDF_TYPE_STRING'))
29 define ('PDF_TYPE_STRING', 4);
30 if (!defined ('PDF_TYPE_DICTIONARY'))
31 define ('PDF_TYPE_DICTIONARY', 5);
32 if (!defined ('PDF_TYPE_ARRAY'))
33 define ('PDF_TYPE_ARRAY', 6);
34 if (!defined ('PDF_TYPE_OBJDEC'))
35 define ('PDF_TYPE_OBJDEC', 7);
36 if (!defined ('PDF_TYPE_OBJREF'))
37 define ('PDF_TYPE_OBJREF', 8);
38 if (!defined ('PDF_TYPE_OBJECT'))
39 define ('PDF_TYPE_OBJECT', 9);
40 if (!defined ('PDF_TYPE_STREAM'))
41 define ('PDF_TYPE_STREAM', 10);
42 if (!defined ('PDF_TYPE_BOOLEAN'))
43 define ('PDF_TYPE_BOOLEAN', 11);
44 if (!defined ('PDF_TYPE_REAL'))
45 define ('PDF_TYPE_REAL', 12);
47 require_once('pdf_context.php');
49 if (!class_exists('pdf_parser', false)) {
67 * @var object pdf_context-Instance
84 * PDF version of the loaded document
90 * For reading encrypted documents and xref/objectstreams are in use
94 var $readPlain = true;
99 * @param string $filename Source-Filename
101 function pdf_parser($filename) {
102 $this->filename
= $filename;
104 $this->f
= @fopen
($this->filename
, 'rb');
107 $this->error(sprintf('Cannot open %s !', $filename));
109 $this->getPDFVersion();
111 $this->c
= new pdf_context($this->f
);
114 $this->xref
= array();
115 $this->pdf_read_xref($this->xref
, $this->pdf_find_xref());
117 // Check for Encryption
118 $this->getEncryption();
121 $this->pdf_read_root();
125 * Close the opened file
127 function closeFile() {
128 if (isset($this->f
) && is_resource($this->f
)) {
135 * Print Error and die
137 * @param string $msg Error-Message
139 function error($msg) {
140 die('<b>PDF-Parser Error:</b> ' . $msg);
144 * Check Trailer for Encryption
146 function getEncryption() {
147 if (isset($this->xref
['trailer'][1]['/Encrypt'])) {
148 $this->error('File is encrypted!');
157 function pdf_find_root() {
158 if ($this->xref
['trailer'][1]['/Root'][0] != PDF_TYPE_OBJREF
) {
159 $this->error('Wrong Type of Root-Element! Must be an indirect reference');
162 return $this->xref
['trailer'][1]['/Root'];
168 function pdf_read_root() {
170 $this->root
= $this->pdf_resolve_object($this->c
, $this->pdf_find_root());
176 * And reset the PDF Version used in FPDI if needed
178 function getPDFVersion() {
180 preg_match('/\d\.\d/',fread($this->f
, 16), $m);
182 $this->pdfVersion
= $m[0];
183 return $this->pdfVersion
;
187 * Find the xref-Table
189 function pdf_find_xref() {
192 $stat = fseek ($this->f
, -$toRead, SEEK_END
);
196 $data = fread($this->f
, $toRead);
198 $pos = strlen($data) - strpos(strrev($data), strrev('startxref'));
199 $data = substr($data, $pos);
201 if (!preg_match('/\s*(\d+).*$/s', $data, $matches)) {
202 $this->error('Unable to find pointer to xref table');
205 return (int) $matches[1];
211 * @param array $result Array of xref-table
212 * @param integer $offset of xref-table
214 function pdf_read_xref(&$result, $offset) {
215 $o_pos = $offset-min(20, $offset);
216 fseek($this->f
, $o_pos); // set some bytes backwards to fetch errorious docs
218 $data = fread($this->f
, 100);
220 $xrefPos = strrpos($data, 'xref');
222 if ($xrefPos === false) {
223 fseek($this->f
, $offset);
224 $c = new pdf_context($this->f
);
225 $xrefStreamObjDec = $this->pdf_read_value($c);
227 if (is_array($xrefStreamObjDec) && isset($xrefStreamObjDec[0]) && $xrefStreamObjDec[0] == PDF_TYPE_OBJDEC
) {
228 $this->error(sprintf('This document (%s) probably uses a compression technique which is not supported by the free parser shipped with FPDI.', $this->filename
));
230 $this->error('Unable to find xref table.');
234 if (!isset($result['xref_location'])) {
235 $result['xref_location'] = $o_pos +
$xrefPos;
236 $result['max_object'] = 0;
240 $bytesPerCycle = 100;
242 fseek($this->f
, $o_pos = $o_pos +
$xrefPos +
4); // set the handle directly after the "xref"-keyword
243 $data = fread($this->f
, $bytesPerCycle);
245 while (($trailerPos = strpos($data, 'trailer', max($bytesPerCycle * $cylces++
, 0))) === false && !feof($this->f
)) {
246 $data .= fread($this->f
, $bytesPerCycle);
249 if ($trailerPos === false) {
250 $this->error('Trailer keyword not found after xref table');
253 $data = substr($data, 0, $trailerPos);
256 preg_match_all("/(\r\n|\n|\r)/", substr($data, 0, 100), $m); // check the first 100 bytes for linebreaks
258 $differentLineEndings = count(array_unique($m[0]));
259 if ($differentLineEndings > 1) {
260 $lines = preg_split("/(\r\n|\n|\r)/", $data, -1, PREG_SPLIT_NO_EMPTY
);
262 $lines = explode($m[0][1], $data);
265 $data = $differentLineEndings = $m = null;
266 unset($data, $differentLineEndings, $m);
268 $linesCount = count($lines);
272 for ($i = 0; $i < $linesCount; $i++
) {
273 $line = trim($lines[$i]);
275 $pieces = explode(' ', $line);
279 $start = (int)$pieces[0];
280 $end = $start +
(int)$pieces[1];
281 if ($end > $result['max_object'])
282 $result['max_object'] = $end;
285 if (!isset($result['xref'][$start]))
286 $result['xref'][$start] = array();
288 if (!array_key_exists($gen = (int) $pieces[1], $result['xref'][$start])) {
289 $result['xref'][$start][$gen] = $pieces[2] == 'n' ?
(int) $pieces[0] : null;
294 $this->error('Unexpected data in xref table');
299 $lines = $pieces = $line = $start = $end = $gen = null;
300 unset($lines, $pieces, $line, $start, $end, $gen);
302 fseek($this->f
, $o_pos +
$trailerPos +
7);
304 $c = new pdf_context($this->f
);
305 $trailer = $this->pdf_read_value($c);
310 if (!isset($result['trailer'])) {
311 $result['trailer'] = $trailer;
314 if (isset($trailer[1]['/Prev'])) {
315 $this->pdf_read_xref($result, $trailer[1]['/Prev'][1]);
327 * @param object $c pdf_context
328 * @param string $token a Token
331 function pdf_read_value(&$c, $token = null) {
332 if (is_null($token)) {
333 $token = $this->pdf_read_token($c);
336 if ($token === false) {
342 // This is a hex string.
343 // Read the value, then the terminator
349 $match = strpos ($c->buffer
, '>', $pos);
351 // If you can't find it, try
352 // reading more data from the stream
354 if ($match === false) {
355 if (!$c->increase_length()) {
362 $result = substr ($c->buffer
, $c->offset
, $match - $c->offset
);
363 $c->offset
= $match +
1;
365 return array (PDF_TYPE_HEX
, $result);
370 // This is a dictionary.
374 // Recurse into this function until we reach
375 // the end of the dictionary.
376 while (($key = $this->pdf_read_token($c)) !== '>>') {
377 if ($key === false) {
381 if (($value = $this->pdf_read_value($c)) === false) {
385 // Catch missing value
386 if ($value[0] == PDF_TYPE_TOKEN
&& $value[1] == '>>') {
387 $result[$key] = array(PDF_TYPE_NULL
);
391 $result[$key] = $value;
394 return array (PDF_TYPE_DICTIONARY
, $result);
401 // Recurse into this function until we reach
402 // the end of the array.
403 while (($token = $this->pdf_read_token($c)) !== ']') {
404 if ($token === false) {
408 if (($value = $this->pdf_read_value($c, $token)) === false) {
415 return array (PDF_TYPE_ARRAY
, $result);
423 for (; $openBrackets != 0 && $pos < $c->length
; $pos++
) {
424 switch (ord($c->buffer
[$pos])) {
431 case 0x5C: // backslash
435 } while($openBrackets != 0 && $c->increase_length());
437 $result = substr($c->buffer
, $c->offset
, $pos - $c->offset
- 1);
440 return array (PDF_TYPE_STRING
, $result);
443 $o_pos = ftell($c->file
)-strlen($c->buffer
);
444 $o_offset = $c->offset
;
446 $c->reset($startpos = $o_pos +
$o_offset);
448 $e = 0; // ensure line breaks in front of the stream
449 if ($c->buffer
[0] == chr(10) ||
$c->buffer
[0] == chr(13))
451 if ($c->buffer
[1] == chr(10) && $c->buffer
[0] != chr(10))
454 if ($this->actual_obj
[1][1]['/Length'][0] == PDF_TYPE_OBJREF
) {
455 $tmp_c = new pdf_context($this->f
);
456 $tmp_length = $this->pdf_resolve_object($tmp_c, $this->actual_obj
[1][1]['/Length']);
457 $length = $tmp_length[1][1];
459 $length = $this->actual_obj
[1][1]['/Length'][1];
463 $c->reset($startpos +
$e,$length);
468 $c->reset($startpos +
$e +
$length +
9); // 9 = strlen("endstream")
470 return array(PDF_TYPE_STREAM
, $v);
473 if (is_numeric ($token)) {
474 // A numeric token. Make sure that
475 // it is not part of something else.
476 if (($tok2 = $this->pdf_read_token ($c)) !== false) {
477 if (is_numeric ($tok2)) {
479 // Two numeric tokens in a row.
480 // In this case, we're probably in
481 // front of either an object reference
482 // or an object specification.
483 // Determine the case and return the data
484 if (($tok3 = $this->pdf_read_token ($c)) !== false) {
487 return array (PDF_TYPE_OBJDEC
, (int) $token, (int) $tok2);
489 return array (PDF_TYPE_OBJREF
, (int) $token, (int) $tok2);
491 // If we get to this point, that numeric value up
492 // there was just a numeric value. Push the extra
493 // tokens back into the stack and return the value.
494 array_push ($c->stack
, $tok3);
498 array_push ($c->stack
, $tok2);
501 if ($token === (string)((int)$token))
502 return array (PDF_TYPE_NUMERIC
, (int)$token);
504 return array (PDF_TYPE_REAL
, (float)$token);
505 } else if ($token == 'true' ||
$token == 'false') {
506 return array (PDF_TYPE_BOOLEAN
, $token == 'true');
507 } else if ($token == 'null') {
508 return array (PDF_TYPE_NULL
);
510 // Just a token. Return it.
511 return array (PDF_TYPE_TOKEN
, $token);
519 * @param object $c pdf_context
520 * @param array $obj_spec The object-data
521 * @param boolean $encapsulate Must set to true, cause the parsing and fpdi use this method only without this para
523 function pdf_resolve_object(&$c, $obj_spec, $encapsulate = true) {
524 // Exit if we get invalid data
525 if (!is_array($obj_spec)) {
530 if ($obj_spec[0] == PDF_TYPE_OBJREF
) {
532 // This is a reference, resolve it
533 if (isset($this->xref
['xref'][$obj_spec[1]][$obj_spec[2]])) {
535 // Save current file position
536 // This is needed if you want to resolve
537 // references while you're reading another object
538 // (e.g.: if you need to determine the length
541 $old_pos = ftell($c->file
);
543 // Reposition the file pointer and
544 // load the object header.
546 $c->reset($this->xref
['xref'][$obj_spec[1]][$obj_spec[2]]);
548 $header = $this->pdf_read_value($c);
550 if ($header[0] != PDF_TYPE_OBJDEC ||
$header[1] != $obj_spec[1] ||
$header[2] != $obj_spec[2]) {
551 $toSearchFor = $obj_spec[1] . ' ' . $obj_spec[2] . ' obj';
552 if (preg_match('/' . $toSearchFor . '/', $c->buffer
)) {
553 $c->offset
= strpos($c->buffer
, $toSearchFor) +
strlen($toSearchFor);
557 $this->error("Unable to find object ({$obj_spec[1]}, {$obj_spec[2]}) at expected location");
561 // If we're being asked to store all the information
562 // about the object, we add the object ID and generation
563 // number for later use
565 $this->actual_obj
=& $result;
569 'obj' => $obj_spec[1],
570 'gen' => $obj_spec[2]
574 // Now simply read the object data until
575 // we encounter an end-of-object marker
577 $value = $this->pdf_read_value($c);
578 if ($value === false ||
count($result) > 4) {
579 // in this case the parser coudn't find an endobj so we break here
583 if ($value[0] == PDF_TYPE_TOKEN
&& $value[1] === 'endobj') {
592 if (isset($result[2][0]) && $result[2][0] == PDF_TYPE_STREAM
) {
593 $result[0] = PDF_TYPE_STREAM
;
606 * Reads a token from the file
608 * @param object $c pdf_context
611 function pdf_read_token(&$c)
613 // If there is a token available
614 // on the stack, pop it out and
617 if (count($c->stack
)) {
618 return array_pop($c->stack
);
621 // Strip away any whitespace
624 if (!$c->ensure_content()) {
627 $c->offset +
= strspn($c->buffer
, " \n\r\t", $c->offset
);
628 } while ($c->offset
>= $c->length
- 1);
630 // Get the first character in the stream
632 $char = $c->buffer
[$c->offset++
];
641 // This is either an array or literal string
642 // delimiter, Return it
649 // This could either be a hex string or
650 // dictionary delimiter. Determine the
651 // appropriate case and return the token
653 if ($c->buffer
[$c->offset
] == $char) {
654 if (!$c->ensure_content()) {
658 return $char . $char;
665 // This is a comment - jump over it!
669 $match = preg_match("/(\r\n|\r|\n)/", $c->buffer
, $m, PREG_OFFSET_CAPTURE
, $pos);
671 if (!$c->increase_length()) {
678 $c->offset
= $m[0][1]+
strlen($m[0][0]);
680 return $this->pdf_read_token($c);
685 // This is "another" type of token (probably
686 // a dictionary entry or a numeric value)
687 // Find the end and return it.
689 if (!$c->ensure_content()) {
695 // Determine the length of the token
697 $pos = strcspn($c->buffer
, " %[]<>()\r\n\t/", $c->offset
);
699 if ($c->offset +
$pos <= $c->length
- 1) {
702 // If the script reaches this point,
703 // the token may span beyond the end
704 // of the current buffer. Therefore,
705 // we increase the size of the buffer
706 // and try again--just to be safe.
708 $c->increase_length();
712 $result = substr($c->buffer
, $c->offset
- 1, $pos +
1);