upgrade to version 1.4.2

This commit is contained in:
wlx 2011-11-03 02:33:37 +00:00
parent 3e10abd046
commit f6ce576e7f
9 changed files with 1293 additions and 850 deletions

View File

@ -0,0 +1,101 @@
<?php
//
// FPDI - Version 1.4.2
//
// Copyright 2004-2011 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
if (!defined('ORD_z'))
define('ORD_z',ord('z'));
if (!defined('ORD_exclmark'))
define('ORD_exclmark', ord('!'));
if (!defined('ORD_u'))
define('ORD_u', ord('u'));
if (!defined('ORD_tilde'))
define('ORD_tilde', ord('~'));
if (!class_exists('FilterASCII85', false)) {
class FilterASCII85 {
function error($msg) {
die($msg);
}
function decode($in) {
$out = '';
$state = 0;
$chn = null;
$l = strlen($in);
for ($k = 0; $k < $l; ++$k) {
$ch = ord($in[$k]) & 0xff;
if ($ch == ORD_tilde) {
break;
}
if (preg_match('/^\s$/',chr($ch))) {
continue;
}
if ($ch == ORD_z && $state == 0) {
$out .= chr(0) . chr(0) . chr(0) . chr(0);
continue;
}
if ($ch < ORD_exclmark || $ch > ORD_u) {
return $this->error('Illegal character in ASCII85Decode.');
}
$chn[$state++] = $ch - ORD_exclmark;
if ($state == 5) {
$state = 0;
$r = 0;
for ($j = 0; $j < 5; ++$j)
$r = $r * 85 + $chn[$j];
$out .= chr($r >> 24);
$out .= chr($r >> 16);
$out .= chr($r >> 8);
$out .= chr($r);
}
}
$r = 0;
if ($state == 1)
return $this->error('Illegal length in ASCII85Decode.');
if ($state == 2) {
$r = $chn[0] * 85 * 85 * 85 * 85 + ($chn[1]+1) * 85 * 85 * 85;
$out .= chr($r >> 24);
}
else if ($state == 3) {
$r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + ($chn[2]+1) * 85 * 85;
$out .= chr($r >> 24);
$out .= chr($r >> 16);
}
else if ($state == 4) {
$r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + $chn[2] * 85 * 85 + ($chn[3]+1) * 85 ;
$out .= chr($r >> 24);
$out .= chr($r >> 16);
$out .= chr($r >> 8);
}
return $out;
}
function encode($in) {
return $this->error("ASCII85 encoding not implemented.");
}
}
}

View File

@ -0,0 +1,33 @@
<?php
//
// FPDI - Version 1.4.2
//
// Copyright 2004-2011 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
require_once('FilterASCII85.php');
class FilterASCII85_FPDI extends FilterASCII85 {
var $fpdi;
function FilterASCII85_FPDI(&$fpdi) {
$this->fpdi =& $fpdi;
}
function error($msg) {
$this->fpdi->error($msg);
}
}

View File

@ -0,0 +1,157 @@
<?php
//
// FPDI - Version 1.4.2
//
// Copyright 2004-2011 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
if (!class_exists('FilterLZW', false)) {
class FilterLZW {
var $sTable = array();
var $data = null;
var $dataLength = 0;
var $tIdx;
var $bitsToGet = 9;
var $bytePointer;
var $bitPointer;
var $nextData = 0;
var $nextBits = 0;
var $andTable = array(511, 1023, 2047, 4095);
function error($msg) {
die($msg);
}
/**
* Method to decode LZW compressed data.
*
* @param string data The compressed data.
*/
function decode($data) {
if($data[0] == 0x00 && $data[1] == 0x01) {
$this->error('LZW flavour not supported.');
}
$this->initsTable();
$this->data = $data;
$this->dataLength = strlen($data);
// Initialize pointers
$this->bytePointer = 0;
$this->bitPointer = 0;
$this->nextData = 0;
$this->nextBits = 0;
$oldCode = 0;
$string = '';
$uncompData = '';
while (($code = $this->getNextCode()) != 257) {
if ($code == 256) {
$this->initsTable();
$code = $this->getNextCode();
if ($code == 257) {
break;
}
$uncompData .= $this->sTable[$code];
$oldCode = $code;
} else {
if ($code < $this->tIdx) {
$string = $this->sTable[$code];
$uncompData .= $string;
$this->addStringToTable($this->sTable[$oldCode], $string[0]);
$oldCode = $code;
} else {
$string = $this->sTable[$oldCode];
$string = $string . $string[0];
$uncompData .= $string;
$this->addStringToTable($string);
$oldCode = $code;
}
}
}
return $uncompData;
}
/**
* Initialize the string table.
*/
function initsTable() {
$this->sTable = array();
for ($i = 0; $i < 256; $i++)
$this->sTable[$i] = chr($i);
$this->tIdx = 258;
$this->bitsToGet = 9;
}
/**
* Add a new string to the string table.
*/
function addStringToTable ($oldString, $newString='') {
$string = $oldString.$newString;
// Add this new String to the table
$this->sTable[$this->tIdx++] = $string;
if ($this->tIdx == 511) {
$this->bitsToGet = 10;
} else if ($this->tIdx == 1023) {
$this->bitsToGet = 11;
} else if ($this->tIdx == 2047) {
$this->bitsToGet = 12;
}
}
// Returns the next 9, 10, 11 or 12 bits
function getNextCode() {
if ($this->bytePointer == $this->dataLength) {
return 257;
}
$this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
$this->nextBits += 8;
if ($this->nextBits < $this->bitsToGet) {
$this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
$this->nextBits += 8;
}
$code = ($this->nextData >> ($this->nextBits - $this->bitsToGet)) & $this->andTable[$this->bitsToGet-9];
$this->nextBits -= $this->bitsToGet;
return $code;
}
function encode($in) {
$this->error("LZW encoding not implemented.");
}
}
}

View File

@ -0,0 +1,33 @@
<?php
//
// FPDI - Version 1.4.2
//
// Copyright 2004-2011 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
require_once('FilterLZW.php');
class FilterLZW_FPDI extends FilterLZW {
var $fpdi;
function FilterLZW_FPDI(&$fpdi) {
$this->fpdi =& $fpdi;
}
function error($msg) {
$this->fpdi->error($msg);
}
}

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.3
// FPDI - Version 1.4.2
//
// Copyright 2004-2009 Setasign - Jan Slabon
// Copyright 2004-2011 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -17,7 +17,7 @@
// limitations under the License.
//
define('FPDI_VERSION','1.3');
define('FPDI_VERSION', '1.4.2');
// Check for TCPDF and remap TCPDF to FPDF
if (class_exists('TCPDF', false)) {
@ -71,9 +71,12 @@ class FPDI extends FPDF_TPL {
*/
var $lastUsedPageBox;
/**
* Cache for imported pages/template ids
* @var array
*/
var $_importedPages = array();
/**
* Set a source-file
*
@ -82,13 +85,40 @@ class FPDI extends FPDF_TPL {
*/
function setSourceFile($filename) {
$this->current_filename = $filename;
$fn =& $this->current_filename;
if (!isset($this->parsers[$fn]))
$this->parsers[$fn] = new fpdi_pdf_parser($fn, $this);
$this->current_parser =& $this->parsers[$fn];
if (!isset($this->parsers[$filename]))
$this->parsers[$filename] = $this->_getPdfParser($filename);
$this->current_parser =& $this->parsers[$filename];
return $this->parsers[$fn]->getPageCount();
return $this->parsers[$filename]->getPageCount();
}
/**
* Returns a PDF parser object
*
* @param string $filename
* @return fpdi_pdf_parser
*/
function _getPdfParser($filename) {
return new fpdi_pdf_parser($filename, $this);
}
/**
* Get the current PDF version
*
* @return string
*/
function getPDFVersion() {
return $this->PDFVersion;
}
/**
* Set the PDF version
*
* @return string
*/
function setPDFVersion($version = '1.3') {
$this->PDFVersion = $version;
}
/**
@ -102,26 +132,20 @@ class FPDI extends FPDF_TPL {
return $this->error('Please import the desired pages before creating a new template.');
}
$fn =& $this->current_filename;
$fn = $this->current_filename;
// check if page already imported
$pageKey = $fn.((int)$pageno).$boxName;
$pageKey = $fn . '-' . ((int)$pageno) . $boxName;
if (isset($this->_importedPages[$pageKey]))
return $this->_importedPages[$pageKey];
$parser =& $this->parsers[$fn];
$parser->setPageno($pageno);
$this->tpl++;
$this->tpls[$this->tpl] = array();
$tpl =& $this->tpls[$this->tpl];
$tpl['parser'] =& $parser;
$tpl['resources'] = $parser->getPageResources();
$tpl['buffer'] = $parser->getContent();
if (!in_array($boxName, $parser->availableBoxes))
return $this->Error(sprintf('Unknown box: %s', $boxName));
$pageboxes = $parser->getPageBoxes($pageno);
$pageboxes = $parser->getPageBoxes($pageno, $this->k);
/**
* MediaBox
@ -137,9 +161,17 @@ class FPDI extends FPDF_TPL {
if (!isset($pageboxes[$boxName]))
return false;
$this->lastUsedPageBox = $boxName;
$box = $pageboxes[$boxName];
$this->tpl++;
$this->tpls[$this->tpl] = array();
$tpl =& $this->tpls[$this->tpl];
$tpl['parser'] =& $parser;
$tpl['resources'] = $parser->getPageResources();
$tpl['buffer'] = $parser->getContent();
$tpl['box'] = $box;
// To build an array that can be used by PDF_TPL::useTemplate()
@ -149,8 +181,6 @@ class FPDI extends FPDF_TPL {
$tpl['x'] = 0;
$tpl['y'] = 0;
$page =& $parser->pages[$parser->pageno];
// handle rotated pages
$rotation = $parser->getPageRotation($pageno);
$tpl['_rotationAngle'] = 0;
@ -162,6 +192,9 @@ class FPDI extends FPDF_TPL {
$tpl['w'] = $steps % 2 == 0 ? $_w : $_h;
$tpl['h'] = $steps % 2 == 0 ? $_h : $_w;
if ($angle < 0)
$angle += 360;
$tpl['_rotationAngle'] = $angle * -1;
}
@ -170,28 +203,54 @@ class FPDI extends FPDF_TPL {
return $this->tpl;
}
/**
* Returns the last used page box
*
* @return string
*/
function getLastUsedPageBox() {
return $this->lastUsedPageBox;
}
function useTemplate($tplidx, $_x = null, $_y = null, $_w = 0, $_h = 0, $adjustPageSize = false) {
if ($adjustPageSize == true && is_null($_x) && is_null($_y)) {
$size = $this->getTemplateSize($tplidx, $_w, $_h);
$format = array($size['w'], $size['h']);
if ($format[0]!=$this->CurPageFormat[0] || $format[1]!=$this->CurPageFormat[1]) {
$this->w=$format[0];
$this->h=$format[1];
$orientation = $size['w'] > $size['h'] ? 'L' : 'P';
$size = array($size['w'], $size['h']);
if (is_subclass_of($this, 'TCPDF')) {
$this->setPageFormat($size, $orientation);
} else {
$size = $this->_getpagesize($size);
if($orientation!=$this->CurOrientation || $size[0]!=$this->CurPageSize[0] || $size[1]!=$this->CurPageSize[1])
{
// New size or orientation
if($orientation=='P')
{
$this->w = $size[0];
$this->h = $size[1];
}
else
{
$this->w = $size[1];
$this->h = $size[0];
}
$this->wPt = $this->w*$this->k;
$this->hPt = $this->h*$this->k;
$this->PageBreakTrigger = $this->h-$this->bMargin;
$this->CurPageFormat=$format;
$this->CurOrientation = $orientation;
$this->CurPageSize = $size;
$this->PageSizes[$this->page] = array($this->wPt, $this->hPt);
}
}
}
$this->_out('q 0 J 1 w 0 j 0 G 0 g'); // reset standard values
$s = parent::useTemplate($tplidx, $_x, $_y, $_w, $_h);
$this->_out('Q');
return $s;
}
@ -273,7 +332,7 @@ class FPDI extends FPDF_TPL {
break;
case -270:
$tx = $tpl['box']['ury'];
$ty = 0;
$ty = -$tpl['box']['llx'];
break;
}
}
@ -323,8 +382,14 @@ class FPDI extends FPDF_TPL {
$nN = $this->n; // TCPDF: rem new "n"
$this->n = $cN; // TCPDF: reset to current "n"
if (is_subclass_of($this, 'TCPDF')) {
$p = $this->_getrawstream($p);
$this->_out('/Length ' . strlen($p) . ' >>');
$this->_out("stream\n" . $p . "\nendstream");
} else {
$this->_out('/Length ' . strlen($p) . ' >>');
$this->_putstream($p);
}
$this->_out('endobj');
$this->n = $nN; // TCPDF: reset to new "n"
}
@ -346,6 +411,8 @@ class FPDI extends FPDF_TPL {
$this->_out($obj_id . ' 0 obj');
$this->_current_obj_id = $obj_id; // for later use with encryption
}
return $obj_id;
}
/**
@ -435,6 +502,7 @@ class FPDI extends FPDF_TPL {
$this->_out($value[2][1]);
$this->_out('endstream');
break;
case PDF_TYPE_HEX:
$this->_straightOut('<' . $value[1] . '>');
break;
@ -500,5 +568,4 @@ class FPDI extends FPDF_TPL {
}
return false;
}
}

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.3
// FPDI - Version 1.4.2
//
// Copyright 2004-2009 Setasign - Jan Slabon
// Copyright 2004-2011 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -28,27 +28,19 @@
*/
class FPDF extends TCPDF {
function __get($name) {
switch ($name) {
case 'PDFVersion':
return $this->PDFVersion;
case 'k':
return $this->k;
default:
// Error handling
$this->Error('Cannot access protected property '.get_class($this).':$'.$name.' / Undefined property: '.get_class($this).'::$'.$name);
function _putstream($s) {
$this->_out($this->_getstream($s));
}
function _getxobjectdict() {
$out = parent::_getxobjectdict();
if (count($this->tpls)) {
foreach($this->tpls as $tplidx => $tpl) {
$out .= sprintf('%s%d %d 0 R', $this->tplprefix, $tplidx, $tpl['n']);
}
}
function __set($name, $value) {
switch ($name) {
case 'PDFVersion':
$this->PDFVersion = $value;
break;
default:
// Error handling
$this->Error('Cannot access protected property '.get_class($this).':$'.$name.' / Undefined property: '.get_class($this).'::$'.$name);
}
return $out;
}
/**
@ -61,21 +53,25 @@ class FPDF extends TCPDF {
case PDF_TYPE_STRING:
if ($this->encrypted) {
$value[1] = $this->_unescape($value[1]);
$value[1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[1]);
$value[1] = $this->_encrypt_data($this->_current_obj_id, $value[1]);
$value[1] = $this->_escape($value[1]);
}
break;
case PDF_TYPE_STREAM:
if ($this->encrypted) {
$value[2][1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[2][1]);
$value[2][1] = $this->_encrypt_data($this->_current_obj_id, $value[2][1]);
$value[1][1]['/Length'] = array(
PDF_TYPE_NUMERIC,
strlen($value[2][1])
);
}
break;
case PDF_TYPE_HEX:
if ($this->encrypted) {
$value[1] = $this->hex2str($value[1]);
$value[1] = $this->_RC4($this->_objectkey($this->_current_obj_id), $value[1]);
$value[1] = $this->_encrypt_data($this->_current_obj_id, $value[1]);
// remake hexstring of encrypted string
$value[1] = $this->str2hex($value[1]);

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.3
// FPDI - Version 1.4.2
//
// Copyright 2004-2009 Setasign - Jan Slabon
// Copyright 2004-2011 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -213,6 +213,11 @@ class fpdi_pdf_parser extends pdf_parser {
if (isset($obj[1][1]['/Filter'])) {
$_filter = $obj[1][1]['/Filter'];
if ($_filter[0] == PDF_TYPE_OBJREF) {
$tmpFilter = $this->pdf_resolve_object($this->c, $_filter);
$_filter = $tmpFilter[1];
}
if ($_filter[0] == PDF_TYPE_TOKEN) {
$filters[] = $_filter;
} else if ($_filter[0] == PDF_TYPE_ARRAY) {
@ -225,30 +230,35 @@ class fpdi_pdf_parser extends pdf_parser {
foreach ($filters AS $_filter) {
switch ($_filter[1]) {
case '/FlateDecode':
case '/Fl':
// $stream .= "\x0F\x0D"; // in an errorious stream this suffix could work
// $stream .= "\x0A";
// $stream .= "\x0D";
if (function_exists('gzuncompress')) {
$stream = (strlen($stream) > 0) ? @gzuncompress($stream) : '';
} else {
$this->fpdi->error(sprintf('To handle %s filter, please compile php with zlib support.',$_filter[1]));
$this->error(sprintf('To handle %s filter, please compile php with zlib support.',$_filter[1]));
}
if ($stream === false) {
$this->fpdi->error('Error while decompressing stream.');
$this->error('Error while decompressing stream.');
}
break;
case '/LZWDecode':
include_once('filters/FilterLZW_FPDI.php');
$decoder = new FilterLZW_FPDI($this->fpdi);
$stream = $decoder->decode($stream);
break;
case '/ASCII85Decode':
include_once('filters/FilterASCII85_FPDI.php');
$decoder = new FilterASCII85_FPDI($this->fpdi);
$stream = $decoder->decode($stream);
break;
case null:
$stream = $stream;
break;
default:
if (preg_match('/^\/[a-z85]*$/i', $_filter[1], $filterName) && @include_once('decoders'.$_filter[1].'.php')) {
$filterName = substr($_filter[1],1);
if (class_exists($filterName)) {
$decoder = new $filterName($this->fpdi);
$stream = $decoder->decode($stream);
} else {
$this->fpdi->error(sprintf('Unsupported Filter: %s',$_filter[1]));
}
} else {
$this->fpdi->error(sprintf('Unsupported Filter: %s',$_filter[1]));
}
$this->error(sprintf('Unsupported Filter: %s',$_filter[1]));
}
}
@ -262,9 +272,10 @@ class fpdi_pdf_parser extends pdf_parser {
*
* @param array $page a /Page
* @param string $box_index Type of Box @see $availableBoxes
* @param float Scale factor from user space units to points
* @return array
*/
function getPageBox($page, $box_index) {
function getPageBox($page, $box_index, $k) {
$page = $this->pdf_resolve_object($this->c, $page);
$box = null;
if (isset($page[1][1][$box_index]))
@ -277,37 +288,44 @@ class fpdi_pdf_parser extends pdf_parser {
if (!is_null($box) && $box[0] == PDF_TYPE_ARRAY) {
$b =& $box[1];
return array('x' => $b[0][1]/$this->fpdi->k,
'y' => $b[1][1]/$this->fpdi->k,
'w' => abs($b[0][1]-$b[2][1])/$this->fpdi->k,
'h' => abs($b[1][1]-$b[3][1])/$this->fpdi->k,
'llx' => $b[0][1]/$this->fpdi->k,
'lly' => $b[1][1]/$this->fpdi->k,
'urx' => $b[2][1]/$this->fpdi->k,
'ury' => $b[3][1]/$this->fpdi->k,
return array('x' => $b[0][1] / $k,
'y' => $b[1][1] / $k,
'w' => abs($b[0][1] - $b[2][1]) / $k,
'h' => abs($b[1][1] - $b[3][1]) / $k,
'llx' => min($b[0][1], $b[2][1]) / $k,
'lly' => min($b[1][1], $b[3][1]) / $k,
'urx' => max($b[0][1], $b[2][1]) / $k,
'ury' => max($b[1][1], $b[3][1]) / $k,
);
} else if (!isset ($page[1][1]['/Parent'])) {
return false;
} else {
return $this->getPageBox($this->pdf_resolve_object($this->c, $page[1][1]['/Parent']), $box_index);
return $this->getPageBox($this->pdf_resolve_object($this->c, $page[1][1]['/Parent']), $box_index, $k);
}
}
function getPageBoxes($pageno) {
return $this->_getPageBoxes($this->pages[$pageno-1]);
}
/**
* Get all Boxes from /Page
* Get all page boxes by page no
*
* @param int The page number
* @param float Scale factor from user space units to points
* @return array
*/
function getPageBoxes($pageno, $k) {
return $this->_getPageBoxes($this->pages[$pageno - 1], $k);
}
/**
* Get all boxes from /Page
*
* @param array a /Page
* @return array
*/
function _getPageBoxes($page) {
function _getPageBoxes($page, $k) {
$boxes = array();
foreach($this->availableBoxes AS $box) {
if ($_box = $this->getPageBox($page,$box)) {
if ($_box = $this->getPageBox($page, $box, $k)) {
$boxes[$box] = $_box;
}
}
@ -353,11 +371,18 @@ class fpdi_pdf_parser extends pdf_parser {
*/
function read_pages(&$c, &$pages, &$result) {
// Get the kids dictionary
$kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']);
$_kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']);
if (!is_array($kids))
$this->fpdi->Error('Cannot find /Kids in current /Page-Dictionary');
foreach ($kids[1] as $v) {
if (!is_array($_kids))
$this->error('Cannot find /Kids in current /Page-Dictionary');
if ($_kids[1][0] == PDF_TYPE_ARRAY) {
$kids = $_kids[1][1];
} else {
$kids = $_kids[1];
}
foreach ($kids as $v) {
$pg = $this->pdf_resolve_object ($c, $v);
if ($pg[1][1]['/Type'][1] === '/Pages') {
// If one of the kids is an embedded
@ -378,7 +403,6 @@ class fpdi_pdf_parser extends pdf_parser {
*/
function getPDFVersion() {
parent::getPDFVersion();
$this->fpdi->PDFVersion = max($this->fpdi->PDFVersion, $this->pdfVersion);
$this->fpdi->setPDFVersion(max($this->fpdi->getPDFVersion(), $this->pdfVersion));
}
}

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.3
// FPDI - Version 1.4.2
//
// Copyright 2004-2009 Setasign - Jan Slabon
// Copyright 2004-2011 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -17,6 +17,8 @@
// limitations under the License.
//
if (!class_exists('pdf_context', false)) {
class pdf_context {
/**
@ -86,7 +88,11 @@ class pdf_context {
} else if ($this->_mode == 0) {
$totalLength = $this->length + $l;
do {
$this->buffer .= fread($this->file, $totalLength-$this->length);
$toRead = $totalLength - $this->length;
if ($toRead < 1)
break;
$this->buffer .= fread($this->file, $toRead);
} while ((($this->length = strlen($this->buffer)) != $totalLength) && !feof($this->file));
return true;
@ -95,3 +101,4 @@ class pdf_context {
}
}
}
}

View File

@ -1,8 +1,8 @@
<?php
//
// FPDI - Version 1.3
// FPDI - Version 1.4.2
//
// Copyright 2004-2009 Setasign - Jan Slabon
// Copyright 2004-2011 Setasign - Jan Slabon
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -46,6 +46,8 @@ if (!defined ('PDF_TYPE_REAL'))
require_once('pdf_context.php');
if (!class_exists('pdf_parser', false)) {
class pdf_parser {
/**
@ -84,6 +86,13 @@ class pdf_parser {
*/
var $pdfVersion;
/**
* For reading encrypted documents and xref/objectstreams are in use
*
* @var boolean
*/
var $readPlain = true;
/**
* Constructor
*
@ -102,6 +111,7 @@ class pdf_parser {
$this->c = new pdf_context($this->f);
// Read xref-Data
$this->xref = array();
$this->pdf_read_xref($this->xref, $this->pdf_find_xref());
// Check for Encryption
@ -115,7 +125,7 @@ class pdf_parser {
* Close the opened file
*/
function closeFile() {
if (isset($this->f)) {
if (isset($this->f) && is_resource($this->f)) {
fclose($this->f);
unset($this->f);
}
@ -148,6 +158,7 @@ class pdf_parser {
if ($this->xref['trailer'][1]['/Root'][0] != PDF_TYPE_OBJREF) {
$this->error('Wrong Type of Root-Element! Must be an indirect reference');
}
return $this->xref['trailer'][1]['/Root'];
}
@ -201,16 +212,24 @@ class pdf_parser {
* @param integer $offset of xref-table
*/
function pdf_read_xref(&$result, $offset) {
fseek($this->f, $o_pos = $offset-20); // set some bytes backwards to fetch errorious docs
$o_pos = $offset-min(20, $offset);
fseek($this->f, $o_pos); // set some bytes backwards to fetch errorious docs
$data = fread($this->f, 100);
$xrefPos = strpos($data, 'xref');
$xrefPos = strrpos($data, 'xref');
if ($xrefPos === false) {
fseek($this->f, $offset);
$c = new pdf_context($this->f);
$xrefStreamObjDec = $this->pdf_read_value($c);
if (is_array($xrefStreamObjDec) && isset($xrefStreamObjDec[0]) && $xrefStreamObjDec[0] == PDF_TYPE_OBJDEC) {
$this->error(sprintf('This document (%s) probably uses a compression technique which is not supported by the free parser shipped with FPDI.', $this->filename));
} else {
$this->error('Unable to find xref table.');
}
}
if (!isset($result['xref_location'])) {
$result['xref_location'] = $o_pos + $xrefPos;
@ -420,7 +439,6 @@ class pdf_parser {
return array (PDF_TYPE_STRING, $result);
case 'stream':
$o_pos = ftell($c->file)-strlen($c->buffer);
$o_offset = $c->offset;
@ -489,11 +507,9 @@ class pdf_parser {
} else if ($token == 'null') {
return array (PDF_TYPE_NULL);
} else {
// Just a token. Return it.
return array (PDF_TYPE_TOKEN, $token);
}
}
}
@ -507,7 +523,8 @@ class pdf_parser {
function pdf_resolve_object(&$c, $obj_spec, $encapsulate = true) {
// Exit if we get invalid data
if (!is_array($obj_spec)) {
return false;
$ret = false;
return $ret;
}
if ($obj_spec[0] == PDF_TYPE_OBJREF) {
@ -531,12 +548,20 @@ class pdf_parser {
$header = $this->pdf_read_value($c);
if ($header[0] != PDF_TYPE_OBJDEC || $header[1] != $obj_spec[1] || $header[2] != $obj_spec[2]) {
$toSearchFor = $obj_spec[1] . ' ' . $obj_spec[2] . ' obj';
if (preg_match('/' . $toSearchFor . '/', $c->buffer)) {
$c->offset = strpos($c->buffer, $toSearchFor) + strlen($toSearchFor);
// reset stack
$c->stack = array();
} else {
$this->error("Unable to find object ({$obj_spec[1]}, {$obj_spec[2]}) at expected location");
}
}
// If we're being asked to store all the information
// about the object, we add the object ID and generation
// number for later use
$result = array();
$this->actual_obj =& $result;
if ($encapsulate) {
$result = array (
@ -544,8 +569,6 @@ class pdf_parser {
'obj' => $obj_spec[1],
'gen' => $obj_spec[2]
);
} else {
$result = array();
}
// Now simply read the object data until
@ -672,6 +695,7 @@ class pdf_parser {
// Determine the length of the token
$pos = strcspn($c->buffer, " %[]<>()\r\n\t/", $c->offset);
if ($c->offset + $pos <= $c->length - 1) {
break;
} else {
@ -692,3 +716,4 @@ class pdf_parser {
}
}
}
}