<?php
//
-// FPDI - Version 1.4.4
+// FPDI - Version 1.5.4
//
-// Copyright 2004-2013 Setasign - Jan Slabon
+// Copyright 2004-2015 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.
// 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('~'));
+/**
+ * Class FilterASCII85
+ */
+class FilterASCII85
+{
+ /**
+ * Decode ASCII85 encoded string
+ *
+ * @param string $in
+ * @return string
+ * @throws Exception
+ */
+ public function decode($in)
+ {
+ $ord = array(
+ '~' => ord('~'),
+ 'z' => ord('z'),
+ 'u' => ord('u'),
+ '!' => ord('!')
+ );
-if (!class_exists('FilterASCII85', false)) {
+ $out = '';
+ $state = 0;
+ $chn = null;
- 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);
- }
+ $l = strlen($in);
+
+ for ($k = 0; $k < $l; ++$k) {
+ $ch = ord($in[$k]) & 0xff;
+
+ if ($ch == $ord['~']) {
+ break;
}
- $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);
+ if (preg_match('/^\s$/',chr($ch))) {
+ continue;
}
- 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);
+ if ($ch == $ord['z'] && $state == 0) {
+ $out .= chr(0) . chr(0) . chr(0) . chr(0);
+ continue;
+ }
+ if ($ch < $ord['!'] || $ch > $ord['u']) {
+ throw new Exception('Illegal character in ASCII85Decode.');
}
- else if ($state == 4) {
- $r = $chn[0] * 85 * 85 * 85 * 85 + $chn[1] * 85 * 85 * 85 + $chn[2] * 85 * 85 + ($chn[3]+1) * 85 ;
+
+ $chn[$state++] = $ch - $ord['!'];
+
+ 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);
}
-
- return $out;
}
-
- function encode($in) {
- return $this->error("ASCII85 encoding not implemented.");
+ $r = 0;
+
+ if ($state == 1) {
+ throw new Exception('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;
+ }
+
+ /**
+ * NOT IMPLEMENTED
+ *
+ * @param string $in
+ * @return string
+ * @throws LogicException
+ */
+ public function encode($in)
+ {
+ throw new LogicException("ASCII85 encoding not implemented.");
}
-}
+}
\ No newline at end of file
--- /dev/null
+<?php\r
+//\r
+// FPDI - Version 1.5.4\r
+//\r
+// Copyright 2004-2015 Setasign - Jan Slabon\r
+//\r
+// Licensed under the Apache License, Version 2.0 (the "License");\r
+// you may not use this file except in compliance with the License.\r
+// You may obtain a copy of the License at\r
+//\r
+// http://www.apache.org/licenses/LICENSE-2.0\r
+//\r
+// Unless required by applicable law or agreed to in writing, software\r
+// distributed under the License is distributed on an "AS IS" BASIS,\r
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+// See the License for the specific language governing permissions and\r
+// limitations under the License.\r
+//\r
+\r
+/**\r
+ * Class FilterASCIIHexDecode\r
+ */\r
+class FilterASCIIHexDecode\r
+{\r
+ /**\r
+ * Converts an ASCII hexadecimal encoded string into it's binary representation.\r
+ *\r
+ * @param string $data The input string\r
+ * @return string\r
+ */\r
+ public function decode($data)\r
+ {\r
+ $data = preg_replace('/[^0-9A-Fa-f]/', '', rtrim($data, '>'));\r
+ if ((strlen($data) % 2) == 1) {\r
+ $data .= '0';\r
+ }\r
+\r
+ return pack('H*', $data);\r
+ }\r
+\r
+ /**\r
+ * Converts a string into ASCII hexadecimal representation.\r
+ *\r
+ * @param string $data The input string\r
+ * @param boolean $leaveEOD\r
+ * @return string\r
+ */\r
+ public function encode($data, $leaveEOD = false)\r
+ {\r
+ return current(unpack('H*', $data)) . ($leaveEOD ? '' : '>');\r
+ }\r
+}
\ No newline at end of file
<?php
//
-// FPDI - Version 1.4.4
+// FPDI - Version 1.5.4
//
-// Copyright 2004-2013 Setasign - Jan Slabon
+// Copyright 2004-2015 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.
// 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);
+/**
+ * Class FilterLZW
+ */
+class FilterLZW
+{
+ protected $_sTable = array();
+ protected $_data = null;
+ protected $_dataLength = 0;
+ protected $_tIdx;
+ protected $_bitsToGet = 9;
+ protected $_bytePointer;
+ protected $_bitPointer;
+ protected $_nextData = 0;
+ protected $_nextBits = 0;
+ protected $_andTable = array(511, 1023, 2047, 4095);
+
+ /**
+ * Decodes LZW compressed data.
+ *
+ * @param string $data The compressed data.
+ * @throws Exception
+ * @return string
+ */
+ public function decode($data)
+ {
+ if ($data[0] == 0x00 && $data[1] == 0x01) {
+ throw new Exception('LZW flavour not supported.');
}
-
- /**
- * 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];
+
+ $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;
+
+ $unCompData = '';
+
+ while (($code = $this->_getNextCode()) != 257) {
+ if ($code == 256) {
+ $this->_initsTable();
+ $code = $this->_getNextCode();
+
+ if ($code == 257) {
+ break;
+ }
+
+ if (!isset($this->_sTable[$code])) {
+ throw new Exception('Error while decompression LZW compressed data.');
+ }
+
+ $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 {
-
- 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;
- }
+ $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;
- }
+
+ return $unCompData;
+ }
+
+
+ /**
+ * Initialize the string table.
+ */
+ protected 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.
+ */
+ protected 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;
+ }
+
+ /**
+ * Returns the next 9, 10, 11 or 12 bits
+ *
+ * @return int
+ */
+ protected function _getNextCode()
+ {
+ if ($this->_bytePointer == $this->_dataLength) {
+ return 257;
}
-
- function encode($in) {
- $this->error("LZW encoding not implemented.");
+
+ $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;
+ }
+
+ /**
+ * NOT IMPLEMENTED
+ *
+ * @param string $in
+ * @return string
+ * @throws LogicException
+ */
+ public function encode($in)
+ {
+ throw new LogicException("LZW encoding not implemented.");
}
-}
+}
\ No newline at end of file
-<?php\r
-//\r
-// FPDF_TPL - Version 1.2.3\r
-//\r
-// Copyright 2004-2013 Setasign - Jan Slabon\r
-//\r
-// Licensed under the Apache License, Version 2.0 (the "License");\r
-// you may not use this file except in compliance with the License.\r
-// You may obtain a copy of the License at\r
-//\r
-// http://www.apache.org/licenses/LICENSE-2.0\r
-//\r
-// Unless required by applicable law or agreed to in writing, software\r
-// distributed under the License is distributed on an "AS IS" BASIS,\r
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-// See the License for the specific language governing permissions and\r
-// limitations under the License.\r
-//\r
-\r
-class FPDF_TPL extends FPDF {\r
- /**\r
- * Array of Tpl-Data\r
- * @var array\r
- */\r
- var $tpls = array();\r
-\r
- /**\r
- * Current Template-ID\r
- * @var int\r
- */\r
- var $tpl = 0;\r
- \r
- /**\r
- * "In Template"-Flag\r
- * @var boolean\r
- */\r
- var $_intpl = false;\r
- \r
- /**\r
- * Nameprefix of Templates used in Resources-Dictonary\r
- * @var string A String defining the Prefix used as Template-Object-Names. Have to beginn with an /\r
- */\r
- var $tplprefix = "/TPL";\r
-\r
- /**\r
- * Resources used By Templates and Pages\r
- * @var array\r
- */\r
- var $_res = array();\r
- \r
- /**\r
- * Last used Template data\r
- *\r
- * @var array\r
- */\r
- var $lastUsedTemplateData = array();\r
- \r
- /**\r
- * Start a Template\r
- *\r
- * This method starts a template. You can give own coordinates to build an own sized\r
- * Template. Pay attention, that the margins are adapted to the new templatesize.\r
- * If you want to write outside the template, for example to build a clipped Template,\r
- * you have to set the Margins and "Cursor"-Position manual after beginTemplate-Call.\r
- *\r
- * If no parameter is given, the template uses the current page-size.\r
- * The Method returns an ID of the current Template. This ID is used later for using this template.\r
- * Warning: A created Template is used in PDF at all events. Still if you don't use it after creation!\r
- *\r
- * @param int $x The x-coordinate given in user-unit\r
- * @param int $y The y-coordinate given in user-unit\r
- * @param int $w The width given in user-unit\r
- * @param int $h The height given in user-unit\r
- * @return int The ID of new created Template\r
- */\r
- function beginTemplate($x = null, $y = null, $w = null, $h = null) {\r
- if (is_subclass_of($this, 'TCPDF')) {\r
- $this->Error('This method is only usable with FPDF. Use TCPDF methods startTemplate() instead.');\r
- return;\r
- }\r
- \r
- if ($this->page <= 0)\r
- $this->error("You have to add a page to fpdf first!");\r
-\r
- if ($x == null)\r
- $x = 0;\r
- if ($y == null)\r
- $y = 0;\r
- if ($w == null)\r
- $w = $this->w;\r
- if ($h == null)\r
- $h = $this->h;\r
-\r
- // Save settings\r
- $this->tpl++;\r
- $tpl =& $this->tpls[$this->tpl];\r
- $tpl = array(\r
- 'o_x' => $this->x,\r
- 'o_y' => $this->y,\r
- 'o_AutoPageBreak' => $this->AutoPageBreak,\r
- 'o_bMargin' => $this->bMargin,\r
- 'o_tMargin' => $this->tMargin,\r
- 'o_lMargin' => $this->lMargin,\r
- 'o_rMargin' => $this->rMargin,\r
- 'o_h' => $this->h,\r
- 'o_w' => $this->w,\r
- 'o_FontFamily' => $this->FontFamily,\r
- 'o_FontStyle' => $this->FontStyle,\r
- 'o_FontSizePt' => $this->FontSizePt,\r
- 'o_FontSize' => $this->FontSize,\r
- 'buffer' => '',\r
- 'x' => $x,\r
- 'y' => $y,\r
- 'w' => $w,\r
- 'h' => $h\r
- );\r
-\r
- $this->SetAutoPageBreak(false);\r
- \r
- // Define own high and width to calculate possitions correct\r
- $this->h = $h;\r
- $this->w = $w;\r
-\r
- $this->_intpl = true;\r
- $this->SetXY($x + $this->lMargin, $y + $this->tMargin);\r
- $this->SetRightMargin($this->w - $w + $this->rMargin);\r
-\r
- if ($this->CurrentFont) {\r
- $fontkey = $this->FontFamily . $this->FontStyle;\r
- $this->_res['tpl'][$this->tpl]['fonts'][$fontkey] =& $this->fonts[$fontkey];\r
- \r
- $this->_out(sprintf('BT /F%d %.2f Tf ET', $this->CurrentFont['i'], $this->FontSizePt));\r
- }\r
- \r
- return $this->tpl;\r
- }\r
- \r
- /**\r
- * End Template\r
- *\r
- * This method ends a template and reset initiated variables on beginTemplate.\r
- *\r
- * @return mixed If a template is opened, the ID is returned. If not a false is returned.\r
- */\r
- function endTemplate() {\r
- if (is_subclass_of($this, 'TCPDF')) {\r
- $args = func_get_args();\r
- return call_user_func_array(array($this, 'TCPDF::endTemplate'), $args);\r
- }\r
- \r
- if ($this->_intpl) {\r
- $this->_intpl = false; \r
- $tpl =& $this->tpls[$this->tpl];\r
- $this->SetXY($tpl['o_x'], $tpl['o_y']);\r
- $this->tMargin = $tpl['o_tMargin'];\r
- $this->lMargin = $tpl['o_lMargin'];\r
- $this->rMargin = $tpl['o_rMargin'];\r
- $this->h = $tpl['o_h'];\r
- $this->w = $tpl['o_w'];\r
- $this->SetAutoPageBreak($tpl['o_AutoPageBreak'], $tpl['o_bMargin']);\r
- \r
- $this->FontFamily = $tpl['o_FontFamily'];\r
- $this->FontStyle = $tpl['o_FontStyle'];\r
- $this->FontSizePt = $tpl['o_FontSizePt'];\r
- $this->FontSize = $tpl['o_FontSize'];\r
- \r
- $fontkey = $this->FontFamily . $this->FontStyle;\r
- if ($fontkey)\r
- $this->CurrentFont =& $this->fonts[$fontkey];\r
- \r
- return $this->tpl;\r
- } else {\r
- return false;\r
- }\r
- }\r
- \r
- /**\r
- * Use a Template in current Page or other Template\r
- *\r
- * You can use a template in a page or in another template.\r
- * You can give the used template a new size like you use the Image()-method.\r
- * All parameters are optional. The width or height is calculated automaticaly\r
- * if one is given. If no parameter is given the origin size as defined in\r
- * beginTemplate() is used.\r
- * The calculated or used width and height are returned as an array.\r
- *\r
- * @param int $tplidx A valid template-Id\r
- * @param int $_x The x-position\r
- * @param int $_y The y-position\r
- * @param int $_w The new width of the template\r
- * @param int $_h The new height of the template\r
- * @return array The height and width of the template\r
- */\r
- function useTemplate($tplidx, $_x = null, $_y = null, $_w = 0, $_h = 0) {\r
- if ($this->page <= 0)\r
- $this->error('You have to add a page first!');\r
- \r
- if (!isset($this->tpls[$tplidx]))\r
- $this->error('Template does not exist!');\r
- \r
- if ($this->_intpl) {\r
- $this->_res['tpl'][$this->tpl]['tpls'][$tplidx] =& $this->tpls[$tplidx];\r
- }\r
- \r
- $tpl =& $this->tpls[$tplidx];\r
- $w = $tpl['w'];\r
- $h = $tpl['h'];\r
- \r
- if ($_x == null)\r
- $_x = 0;\r
- if ($_y == null)\r
- $_y = 0;\r
- \r
- $_x += $tpl['x'];\r
- $_y += $tpl['y'];\r
- \r
- $wh = $this->getTemplateSize($tplidx, $_w, $_h);\r
- $_w = $wh['w'];\r
- $_h = $wh['h'];\r
- \r
- $tData = array(\r
- 'x' => $this->x,\r
- 'y' => $this->y,\r
- 'w' => $_w,\r
- 'h' => $_h,\r
- 'scaleX' => ($_w / $w),\r
- 'scaleY' => ($_h / $h),\r
- 'tx' => $_x,\r
- 'ty' => ($this->h - $_y - $_h),\r
- 'lty' => ($this->h - $_y - $_h) - ($this->h - $h) * ($_h / $h)\r
- );\r
- \r
- $this->_out(sprintf('q %.4F 0 0 %.4F %.4F %.4F cm', $tData['scaleX'], $tData['scaleY'], $tData['tx'] * $this->k, $tData['ty'] * $this->k)); // Translate \r
- $this->_out(sprintf('%s%d Do Q', $this->tplprefix, $tplidx));\r
-\r
- $this->lastUsedTemplateData = $tData;\r
- \r
- return array('w' => $_w, 'h' => $_h);\r
- }\r
- \r
- /**\r
- * Get The calculated Size of a Template\r
- *\r
- * If one size is given, this method calculates the other one.\r
- *\r
- * @param int $tplidx A valid template-Id\r
- * @param int $_w The width of the template\r
- * @param int $_h The height of the template\r
- * @return array The height and width of the template\r
- */\r
- function getTemplateSize($tplidx, $_w = 0, $_h = 0) {\r
- if (!isset($this->tpls[$tplidx]))\r
- return false;\r
-\r
- $tpl =& $this->tpls[$tplidx];\r
- $w = $tpl['w'];\r
- $h = $tpl['h'];\r
- \r
- if ($_w == 0 and $_h == 0) {\r
- $_w = $w;\r
- $_h = $h;\r
- }\r
-\r
- if($_w == 0)\r
- $_w = $_h * $w / $h;\r
- if($_h == 0)\r
- $_h = $_w * $h / $w;\r
- \r
- return array("w" => $_w, "h" => $_h);\r
- }\r
- \r
- /**\r
- * See FPDF/TCPDF-Documentation ;-)\r
- */\r
- public function SetFont($family, $style='', $size=null, $fontfile='', $subset='default', $out=true) {\r
- if (is_subclass_of($this, 'TCPDF')) {\r
- $args = func_get_args();\r
- return call_user_func_array(array($this, 'TCPDF::SetFont'), $args);\r
- }\r
- \r
- parent::SetFont($family, $style, $size);\r
- \r
- $fontkey = $this->FontFamily . $this->FontStyle;\r
- \r
- if ($this->_intpl) {\r
- $this->_res['tpl'][$this->tpl]['fonts'][$fontkey] =& $this->fonts[$fontkey];\r
- } else {\r
- $this->_res['page'][$this->page]['fonts'][$fontkey] =& $this->fonts[$fontkey];\r
- }\r
- }\r
- \r
- /**\r
- * See FPDF/TCPDF-Documentation ;-)\r
- */\r
- function Image(\r
- $file, $x = '', $y = '', $w = 0, $h = 0, $type = '', $link = '', $align = '', $resize = false,\r
- $dpi = 300, $palign = '', $ismask = false, $imgmask = false, $border = 0, $fitbox = false,\r
- $hidden = false, $fitonpage = false, $alt = false, $altimgs = array()\r
- ) {\r
- if (is_subclass_of($this, 'TCPDF')) {\r
- $args = func_get_args();\r
- return call_user_func_array(array($this, 'TCPDF::Image'), $args);\r
- }\r
- \r
- $ret = parent::Image($file, $x, $y, $w, $h, $type, $link);\r
- if ($this->_intpl) {\r
- $this->_res['tpl'][$this->tpl]['images'][$file] =& $this->images[$file];\r
- } else {\r
- $this->_res['page'][$this->page]['images'][$file] =& $this->images[$file];\r
- }\r
- \r
- return $ret;\r
- }\r
- \r
- /**\r
- * See FPDF-Documentation ;-)\r
- *\r
- * AddPage is not available when you're "in" a template.\r
- */\r
- function AddPage($orientation = '', $format = '', $keepmargins = false, $tocpage = false) {\r
- if (is_subclass_of($this, 'TCPDF')) {\r
- $args = func_get_args();\r
- return call_user_func_array(array($this, 'TCPDF::AddPage'), $args);\r
- }\r
- \r
- if ($this->_intpl)\r
- $this->Error('Adding pages in templates isn\'t possible!');\r
- \r
- parent::AddPage($orientation, $format);\r
- }\r
-\r
- /**\r
- * Preserve adding Links in Templates ...won't work\r
- */\r
- function Link($x, $y, $w, $h, $link, $spaces = 0) {\r
- if (is_subclass_of($this, 'TCPDF')) {\r
- $args = func_get_args();\r
- return call_user_func_array(array($this, 'TCPDF::Link'), $args);\r
- }\r
- \r
- if ($this->_intpl)\r
- $this->Error('Using links in templates aren\'t possible!');\r
- \r
- parent::Link($x, $y, $w, $h, $link);\r
- }\r
- \r
- function AddLink() {\r
- if (is_subclass_of($this, 'TCPDF')) {\r
- $args = func_get_args();\r
- return call_user_func_array(array($this, 'TCPDF::AddLink'), $args);\r
- }\r
- \r
- if ($this->_intpl)\r
- $this->Error('Adding links in templates aren\'t possible!');\r
- return parent::AddLink();\r
- }\r
- \r
- function SetLink($link, $y = 0, $page = -1) {\r
- if (is_subclass_of($this, 'TCPDF')) {\r
- $args = func_get_args();\r
- return call_user_func_array(array($this, 'TCPDF::SetLink'), $args);\r
- }\r
- \r
- if ($this->_intpl)\r
- $this->Error('Setting links in templates aren\'t possible!');\r
- parent::SetLink($link, $y, $page);\r
- }\r
- \r
- /**\r
- * Private Method that writes the form xobjects\r
- */\r
- function _putformxobjects() {\r
- $filter=($this->compress) ? '/Filter /FlateDecode ' : '';\r
- reset($this->tpls);\r
- foreach($this->tpls AS $tplidx => $tpl) {\r
-\r
- $p=($this->compress) ? gzcompress($tpl['buffer']) : $tpl['buffer'];\r
- $this->_newobj();\r
- $this->tpls[$tplidx]['n'] = $this->n;\r
- $this->_out('<<'.$filter.'/Type /XObject');\r
- $this->_out('/Subtype /Form');\r
- $this->_out('/FormType 1');\r
- $this->_out(sprintf('/BBox [%.2F %.2F %.2F %.2F]',\r
- // llx\r
- $tpl['x'] * $this->k,\r
- // lly\r
- -$tpl['y'] * $this->k,\r
- // urx\r
- ($tpl['w'] + $tpl['x']) * $this->k,\r
- // ury\r
- ($tpl['h'] - $tpl['y']) * $this->k\r
- ));\r
- \r
- if ($tpl['x'] != 0 || $tpl['y'] != 0) {\r
- $this->_out(sprintf('/Matrix [1 0 0 1 %.5F %.5F]',\r
- -$tpl['x'] * $this->k * 2, $tpl['y'] * $this->k * 2\r
- ));\r
- }\r
- \r
- $this->_out('/Resources ');\r
-\r
- $this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');\r
- if (isset($this->_res['tpl'][$tplidx]['fonts']) && count($this->_res['tpl'][$tplidx]['fonts'])) {\r
- $this->_out('/Font <<');\r
- foreach($this->_res['tpl'][$tplidx]['fonts'] as $font)\r
- $this->_out('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R');\r
- $this->_out('>>');\r
- }\r
- if(isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images']) || \r
- isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls']))\r
- {\r
- $this->_out('/XObject <<');\r
- if (isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images'])) {\r
- foreach($this->_res['tpl'][$tplidx]['images'] as $image)\r
- $this->_out('/I' . $image['i'] . ' ' . $image['n'] . ' 0 R');\r
- }\r
- if (isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls'])) {\r
- foreach($this->_res['tpl'][$tplidx]['tpls'] as $i => $tpl)\r
- $this->_out($this->tplprefix . $i . ' ' . $tpl['n'] . ' 0 R');\r
- }\r
- $this->_out('>>');\r
- }\r
- $this->_out('>>');\r
- \r
- $this->_out('/Length ' . strlen($p) . ' >>');\r
- $this->_putstream($p);\r
- $this->_out('endobj');\r
- }\r
- }\r
- \r
- /**\r
- * Overwritten to add _putformxobjects() after _putimages()\r
- *\r
- */\r
- function _putimages() {\r
- parent::_putimages();\r
- $this->_putformxobjects();\r
- }\r
- \r
- function _putxobjectdict() {\r
- parent::_putxobjectdict();\r
- \r
- if (count($this->tpls)) {\r
- foreach($this->tpls as $tplidx => $tpl) {\r
- $this->_out(sprintf('%s%d %d 0 R', $this->tplprefix, $tplidx, $tpl['n']));\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Private Method\r
- */\r
- function _out($s) {\r
- if ($this->state == 2 && $this->_intpl) {\r
- $this->tpls[$this->tpl]['buffer'] .= $s . "\n";\r
- } else {\r
- parent::_out($s);\r
- }\r
- }\r
-}\r
+<?php
+//
+// FPDI - Version 1.5.4
+//
+// Copyright 2004-2015 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('fpdi_bridge')) {
+ require_once('fpdi_bridge.php');
+}
+
+/**
+ * Class FPDF_TPL
+ */
+class FPDF_TPL extends fpdi_bridge
+{
+ /**
+ * Array of template data
+ *
+ * @var array
+ */
+ protected $_tpls = array();
+
+ /**
+ * Current Template-Id
+ *
+ * @var int
+ */
+ public $tpl = 0;
+
+ /**
+ * "In Template"-Flag
+ *
+ * @var boolean
+ */
+ protected $_inTpl = false;
+
+ /**
+ * Name prefix of templates used in Resources dictionary
+ *
+ * @var string A String defining the Prefix used as Template-Object-Names. Have to begin with an /
+ */
+ public $tplPrefix = "/TPL";
+
+ /**
+ * Resources used by templates and pages
+ *
+ * @var array
+ */
+ protected $_res = array();
+
+ /**
+ * Last used template data
+ *
+ * @var array
+ */
+ public $lastUsedTemplateData = array();
+
+ /**
+ * Start a template.
+ *
+ * This method starts a template. You can give own coordinates to build an own sized
+ * template. Pay attention, that the margins are adapted to the new template size.
+ * If you want to write outside the template, for example to build a clipped template,
+ * you have to set the margins and "cursor"-position manual after beginTemplate()-call.
+ *
+ * If no parameter is given, the template uses the current page-size.
+ * The method returns an id of the current template. This id is used later for using this template.
+ * Warning: A created template is saved in the resulting PDF at all events. Also if you don't use it after creation!
+ *
+ * @param int $x The x-coordinate given in user-unit
+ * @param int $y The y-coordinate given in user-unit
+ * @param int $w The width given in user-unit
+ * @param int $h The height given in user-unit
+ * @return int The id of new created template
+ * @throws LogicException
+ */
+ public function beginTemplate($x = null, $y = null, $w = null, $h = null)
+ {
+ if (is_subclass_of($this, 'TCPDF')) {
+ throw new LogicException('This method is only usable with FPDF. Use TCPDF methods startTemplate() instead.');
+ }
+
+ if ($this->page <= 0) {
+ throw new LogicException("You have to add at least a page first!");
+ }
+
+ if ($x == null)
+ $x = 0;
+ if ($y == null)
+ $y = 0;
+ if ($w == null)
+ $w = $this->w;
+ if ($h == null)
+ $h = $this->h;
+
+ // Save settings
+ $this->tpl++;
+ $tpl =& $this->_tpls[$this->tpl];
+ $tpl = array(
+ 'o_x' => $this->x,
+ 'o_y' => $this->y,
+ 'o_AutoPageBreak' => $this->AutoPageBreak,
+ 'o_bMargin' => $this->bMargin,
+ 'o_tMargin' => $this->tMargin,
+ 'o_lMargin' => $this->lMargin,
+ 'o_rMargin' => $this->rMargin,
+ 'o_h' => $this->h,
+ 'o_w' => $this->w,
+ 'o_FontFamily' => $this->FontFamily,
+ 'o_FontStyle' => $this->FontStyle,
+ 'o_FontSizePt' => $this->FontSizePt,
+ 'o_FontSize' => $this->FontSize,
+ 'buffer' => '',
+ 'x' => $x,
+ 'y' => $y,
+ 'w' => $w,
+ 'h' => $h
+ );
+
+ $this->SetAutoPageBreak(false);
+
+ // Define own high and width to calculate correct positions
+ $this->h = $h;
+ $this->w = $w;
+
+ $this->_inTpl = true;
+ $this->SetXY($x + $this->lMargin, $y + $this->tMargin);
+ $this->SetRightMargin($this->w - $w + $this->rMargin);
+
+ if ($this->CurrentFont) {
+ $fontKey = $this->FontFamily . $this->FontStyle;
+ if ($fontKey) {
+ $this->_res['tpl'][$this->tpl]['fonts'][$fontKey] =& $this->fonts[$fontKey];
+ $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt));
+ }
+ }
+
+ return $this->tpl;
+ }
+
+ /**
+ * End template.
+ *
+ * This method ends a template and reset initiated variables collected in {@link beginTemplate()}.
+ *
+ * @return int|boolean If a template is opened, the id is returned. If not a false is returned.
+ */
+ public function endTemplate()
+ {
+ if (is_subclass_of($this, 'TCPDF')) {
+ $args = func_get_args();
+ return call_user_func_array(array($this, 'TCPDF::endTemplate'), $args);
+ }
+
+ if ($this->_inTpl) {
+ $this->_inTpl = false;
+ $tpl = $this->_tpls[$this->tpl];
+ $this->SetXY($tpl['o_x'], $tpl['o_y']);
+ $this->tMargin = $tpl['o_tMargin'];
+ $this->lMargin = $tpl['o_lMargin'];
+ $this->rMargin = $tpl['o_rMargin'];
+ $this->h = $tpl['o_h'];
+ $this->w = $tpl['o_w'];
+ $this->SetAutoPageBreak($tpl['o_AutoPageBreak'], $tpl['o_bMargin']);
+
+ $this->FontFamily = $tpl['o_FontFamily'];
+ $this->FontStyle = $tpl['o_FontStyle'];
+ $this->FontSizePt = $tpl['o_FontSizePt'];
+ $this->FontSize = $tpl['o_FontSize'];
+
+ $fontKey = $this->FontFamily . $this->FontStyle;
+ if ($fontKey)
+ $this->CurrentFont =& $this->fonts[$fontKey];
+
+ return $this->tpl;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Use a template in current page or other template.
+ *
+ * You can use a template in a page or in another template.
+ * You can give the used template a new size.
+ * All parameters are optional. The width or height is calculated automatically
+ * if one is given. If no parameter is given the origin size as defined in
+ * {@link beginTemplate()} method is used.
+ *
+ * The calculated or used width and height are returned as an array.
+ *
+ * @param int $tplIdx A valid template-id
+ * @param int $x The x-position
+ * @param int $y The y-position
+ * @param int $w The new width of the template
+ * @param int $h The new height of the template
+ * @return array The height and width of the template (array('w' => ..., 'h' => ...))
+ * @throws LogicException|InvalidArgumentException
+ */
+ public function useTemplate($tplIdx, $x = null, $y = null, $w = 0, $h = 0)
+ {
+ if ($this->page <= 0) {
+ throw new LogicException('You have to add at least a page first!');
+ }
+
+ if (!isset($this->_tpls[$tplIdx])) {
+ throw new InvalidArgumentException('Template does not exist!');
+ }
+
+ if ($this->_inTpl) {
+ $this->_res['tpl'][$this->tpl]['tpls'][$tplIdx] =& $this->_tpls[$tplIdx];
+ }
+
+ $tpl = $this->_tpls[$tplIdx];
+ $_w = $tpl['w'];
+ $_h = $tpl['h'];
+
+ if ($x == null) {
+ $x = 0;
+ }
+
+ if ($y == null) {
+ $y = 0;
+ }
+
+ $x += $tpl['x'];
+ $y += $tpl['y'];
+
+ $wh = $this->getTemplateSize($tplIdx, $w, $h);
+ $w = $wh['w'];
+ $h = $wh['h'];
+
+ $tplData = array(
+ 'x' => $this->x,
+ 'y' => $this->y,
+ 'w' => $w,
+ 'h' => $h,
+ 'scaleX' => ($w / $_w),
+ 'scaleY' => ($h / $_h),
+ 'tx' => $x,
+ 'ty' => ($this->h - $y - $h),
+ 'lty' => ($this->h - $y - $h) - ($this->h - $_h) * ($h / $_h)
+ );
+
+ $this->_out(sprintf('q %.4F 0 0 %.4F %.4F %.4F cm',
+ $tplData['scaleX'], $tplData['scaleY'], $tplData['tx'] * $this->k, $tplData['ty'] * $this->k)
+ ); // Translate
+ $this->_out(sprintf('%s%d Do Q', $this->tplPrefix, $tplIdx));
+
+ $this->lastUsedTemplateData = $tplData;
+
+ return array('w' => $w, 'h' => $h);
+ }
+
+ /**
+ * Get the calculated size of a template.
+ *
+ * If one size is given, this method calculates the other one.
+ *
+ * @param int $tplIdx A valid template-id
+ * @param int $w The width of the template
+ * @param int $h The height of the template
+ * @return array The height and width of the template (array('w' => ..., 'h' => ...))
+ */
+ public function getTemplateSize($tplIdx, $w = 0, $h = 0)
+ {
+ if (!isset($this->_tpls[$tplIdx]))
+ return false;
+
+ $tpl = $this->_tpls[$tplIdx];
+ $_w = $tpl['w'];
+ $_h = $tpl['h'];
+
+ if ($w == 0 && $h == 0) {
+ $w = $_w;
+ $h = $_h;
+ }
+
+ if ($w == 0)
+ $w = $h * $_w / $_h;
+ if($h == 0)
+ $h = $w * $_h / $_w;
+
+ return array("w" => $w, "h" => $h);
+ }
+
+ /**
+ * Sets the font used to print character strings.
+ *
+ * See FPDF/TCPDF documentation.
+ *
+ * @see http://fpdf.org/en/doc/setfont.htm
+ * @see http://www.tcpdf.org/doc/code/classTCPDF.html#afd56e360c43553830d543323e81bc045
+ */
+ public function SetFont($family, $style = '', $size = null, $fontfile = '', $subset = 'default', $out = true)
+ {
+ if (is_subclass_of($this, 'TCPDF')) {
+ $args = func_get_args();
+ return call_user_func_array(array($this, 'TCPDF::SetFont'), $args);
+ }
+
+ parent::SetFont($family, $style, $size);
+
+ $fontkey = $this->FontFamily . $this->FontStyle;
+
+ if ($this->_inTpl) {
+ $this->_res['tpl'][$this->tpl]['fonts'][$fontkey] =& $this->fonts[$fontkey];
+ } else {
+ $this->_res['page'][$this->page]['fonts'][$fontkey] =& $this->fonts[$fontkey];
+ }
+ }
+
+ /**
+ * Puts an image.
+ *
+ * See FPDF/TCPDF documentation.
+ *
+ * @see http://fpdf.org/en/doc/image.htm
+ * @see http://www.tcpdf.org/doc/code/classTCPDF.html#a714c2bee7d6b39d4d6d304540c761352
+ */
+ public function Image(
+ $file, $x = '', $y = '', $w = 0, $h = 0, $type = '', $link = '', $align = '', $resize = false,
+ $dpi = 300, $palign = '', $ismask = false, $imgmask = false, $border = 0, $fitbox = false,
+ $hidden = false, $fitonpage = false, $alt = false, $altimgs = array()
+ )
+ {
+ if (is_subclass_of($this, 'TCPDF')) {
+ $args = func_get_args();
+ return call_user_func_array(array($this, 'TCPDF::Image'), $args);
+ }
+
+ $ret = parent::Image($file, $x, $y, $w, $h, $type, $link);
+ if ($this->_inTpl) {
+ $this->_res['tpl'][$this->tpl]['images'][$file] =& $this->images[$file];
+ } else {
+ $this->_res['page'][$this->page]['images'][$file] =& $this->images[$file];
+ }
+
+ return $ret;
+ }
+
+ /**
+ * Adds a new page to the document.
+ *
+ * See FPDF/TCPDF documentation.
+ *
+ * This method cannot be used if you'd started a template.
+ *
+ * @see http://fpdf.org/en/doc/addpage.htm
+ * @see http://www.tcpdf.org/doc/code/classTCPDF.html#a5171e20b366b74523709d84c349c1ced
+ */
+ public function AddPage($orientation = '', $format = '', $keepmargins = false, $tocpage = false)
+ {
+ if (is_subclass_of($this, 'TCPDF')) {
+ $args = func_get_args();
+ return call_user_func_array(array($this, 'TCPDF::AddPage'), $args);
+ }
+
+ if ($this->_inTpl) {
+ throw new LogicException('Adding pages in templates is not possible!');
+ }
+
+ parent::AddPage($orientation, $format);
+ }
+
+ /**
+ * Puts a link on a rectangular area of the page.
+ *
+ * Overwritten because adding links in a template will not work.
+ *
+ * @see http://fpdf.org/en/doc/link.htm
+ * @see http://www.tcpdf.org/doc/code/classTCPDF.html#ab87bf1826384fbfe30eb499d42f1d994
+ */
+ public function Link($x, $y, $w, $h, $link, $spaces = 0)
+ {
+ if (is_subclass_of($this, 'TCPDF')) {
+ $args = func_get_args();
+ return call_user_func_array(array($this, 'TCPDF::Link'), $args);
+ }
+
+ if ($this->_inTpl) {
+ throw new LogicException('Using links in templates is not posible!');
+ }
+
+ parent::Link($x, $y, $w, $h, $link);
+ }
+
+ /**
+ * Creates a new internal link and returns its identifier.
+ *
+ * Overwritten because adding links in a template will not work.
+ *
+ * @see http://fpdf.org/en/doc/addlink.htm
+ * @see http://www.tcpdf.org/doc/code/classTCPDF.html#a749522038ed7786c3e1701435dcb891e
+ */
+ public function AddLink()
+ {
+ if (is_subclass_of($this, 'TCPDF')) {
+ $args = func_get_args();
+ return call_user_func_array(array($this, 'TCPDF::AddLink'), $args);
+ }
+
+ if ($this->_inTpl) {
+ throw new LogicException('Adding links in templates is not possible!');
+ }
+
+ return parent::AddLink();
+ }
+
+ /**
+ * Defines the page and position a link points to.
+ *
+ * Overwritten because adding links in a template will not work.
+ *
+ * @see http://fpdf.org/en/doc/setlink.htm
+ * @see http://www.tcpdf.org/doc/code/classTCPDF.html#ace5be60e7857953ea5e2b89cb90df0ae
+ */
+ public function SetLink($link, $y = 0, $page = -1)
+ {
+ if (is_subclass_of($this, 'TCPDF')) {
+ $args = func_get_args();
+ return call_user_func_array(array($this, 'TCPDF::SetLink'), $args);
+ }
+
+ if ($this->_inTpl) {
+ throw new LogicException('Setting links in templates is not possible!');
+ }
+
+ parent::SetLink($link, $y, $page);
+ }
+
+ /**
+ * Writes the form XObjects to the PDF document.
+ */
+ protected function _putformxobjects()
+ {
+ $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
+ reset($this->_tpls);
+
+ foreach($this->_tpls AS $tplIdx => $tpl) {
+ $this->_newobj();
+ $this->_tpls[$tplIdx]['n'] = $this->n;
+ $this->_out('<<'.$filter.'/Type /XObject');
+ $this->_out('/Subtype /Form');
+ $this->_out('/FormType 1');
+ $this->_out(sprintf('/BBox [%.2F %.2F %.2F %.2F]',
+ // llx
+ $tpl['x'] * $this->k,
+ // lly
+ -$tpl['y'] * $this->k,
+ // urx
+ ($tpl['w'] + $tpl['x']) * $this->k,
+ // ury
+ ($tpl['h'] - $tpl['y']) * $this->k
+ ));
+
+ if ($tpl['x'] != 0 || $tpl['y'] != 0) {
+ $this->_out(sprintf('/Matrix [1 0 0 1 %.5F %.5F]',
+ -$tpl['x'] * $this->k * 2, $tpl['y'] * $this->k * 2
+ ));
+ }
+
+ $this->_out('/Resources ');
+ $this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
+
+ if (isset($this->_res['tpl'][$tplIdx])) {
+ $res = $this->_res['tpl'][$tplIdx];
+ if (isset($res['fonts']) && count($res['fonts'])) {
+ $this->_out('/Font <<');
+
+ foreach($res['fonts'] as $font) {
+ $this->_out('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R');
+ }
+
+ $this->_out('>>');
+ }
+
+ if(isset($res['images']) || isset($res['tpls'])) {
+ $this->_out('/XObject <<');
+
+ if (isset($res['images'])) {
+ foreach($res['images'] as $image)
+ $this->_out('/I' . $image['i'] . ' ' . $image['n'] . ' 0 R');
+ }
+
+ if (isset($res['tpls'])) {
+ foreach($res['tpls'] as $i => $_tpl)
+ $this->_out($this->tplPrefix . $i . ' ' . $_tpl['n'] . ' 0 R');
+ }
+
+ $this->_out('>>');
+ }
+ }
+
+ $this->_out('>>');
+
+ $buffer = ($this->compress) ? gzcompress($tpl['buffer']) : $tpl['buffer'];
+ $this->_out('/Length ' . strlen($buffer) . ' >>');
+ $this->_putstream($buffer);
+ $this->_out('endobj');
+ }
+ }
+
+ /**
+ * Output images.
+ *
+ * Overwritten to add {@link _putformxobjects()} after _putimages().
+ */
+ public function _putimages()
+ {
+ parent::_putimages();
+ $this->_putformxobjects();
+ }
+
+ /**
+ * Writes the references of XObject resources to the document.
+ *
+ * Overwritten to add the the templates to the XObject resource dictionary.
+ */
+ public function _putxobjectdict()
+ {
+ parent::_putxobjectdict();
+
+ foreach($this->_tpls as $tplIdx => $tpl) {
+ $this->_out(sprintf('%s%d %d 0 R', $this->tplPrefix, $tplIdx, $tpl['n']));
+ }
+ }
+
+ /**
+ * Writes bytes to the resulting document.
+ *
+ * Overwritten to delegate the data to the template buffer.
+ *
+ * @param string $s
+ */
+ public function _out($s)
+ {
+ if ($this->state == 2 && $this->_inTpl) {
+ $this->_tpls[$this->tpl]['buffer'] .= $s . "\n";
+ } else {
+ parent::_out($s);
+ }
+ }
+}
<?php\r
//\r
-// FPDI - Version 1.4.4\r
+// FPDI - Version 1.5.4\r
//\r
-// Copyright 2004-2013 Setasign - Jan Slabon\r
+// Copyright 2004-2015 Setasign - Jan Slabon\r
//\r
// Licensed under the Apache License, Version 2.0 (the "License");\r
// you may not use this file except in compliance with the License.\r
// limitations under the License.\r
//\r
\r
-define('FPDI_VERSION', '1.4.4');\r
-\r
-// Check for TCPDF and remap TCPDF to FPDF\r
-if (class_exists('TCPDF', false)) {\r
- require_once('fpdi2tcpdf_bridge.php');\r
+if (!class_exists('FPDF_TPL')) {\r
+ require_once('fpdf_tpl.php');\r
}\r
\r
-require_once('fpdf_tpl.php');\r
-require_once('fpdi_pdf_parser.php');\r
-\r
+/**\r
+ * Class FPDI\r
+ */\r
+class FPDI extends FPDF_TPL\r
+{\r
+ /**\r
+ * FPDI version\r
+ *\r
+ * @string\r
+ */\r
+ const VERSION = '1.5.3';\r
\r
-class FPDI extends FPDF_TPL {\r
/**\r
* Actual filename\r
+ *\r
* @var string\r
*/\r
- var $current_filename;\r
+ public $currentFilename;\r
\r
/**\r
* Parser-Objects\r
- * @var array\r
+ *\r
+ * @var fpdi_pdf_parser[]\r
*/\r
- var $parsers;\r
+ public $parsers = array();\r
\r
/**\r
* Current parser\r
- * @var object\r
+ *\r
+ * @var fpdi_pdf_parser\r
*/\r
- var $current_parser;\r
- \r
+ public $currentParser;\r
+\r
+ /**\r
+ * The name of the last imported page box\r
+ *\r
+ * @var string\r
+ */\r
+ public $lastUsedPageBox;\r
+\r
/**\r
- * object stack\r
+ * Object stack\r
+ *\r
* @var array\r
*/\r
- var $_obj_stack;\r
+ protected $_objStack;\r
\r
/**\r
- * done object stack\r
+ * Done object stack\r
+ *\r
* @var array\r
*/\r
- var $_don_obj_stack;\r
+ protected $_doneObjStack;\r
\r
/**\r
* Current Object Id.\r
+ *\r
* @var integer\r
*/\r
- var $_current_obj_id;\r
- \r
- /**\r
- * The name of the last imported page box\r
- * @var string\r
- */\r
- var $lastUsedPageBox;\r
+ protected $_currentObjId;\r
\r
/**\r
* Cache for imported pages/template ids\r
+ *\r
* @var array\r
*/\r
- var $_importedPages = array();\r
+ protected $_importedPages = array();\r
\r
/**\r
- * Set a source-file\r
+ * Set a source-file.\r
*\r
- * @param string $filename a valid filename\r
- * @return int number of available pages\r
+ * Depending on the PDF version of the used document the PDF version of the resulting document will\r
+ * be adjusted to the higher version.\r
+ *\r
+ * @param string $filename A valid path to the PDF document from which pages should be imported from\r
+ * @return int The number of pages in the document\r
*/\r
- function setSourceFile($filename) {\r
- $this->current_filename = $filename;\r
+ public function setSourceFile($filename)\r
+ {\r
+ $_filename = realpath($filename);\r
+ if (false !== $_filename)\r
+ $filename = $_filename;\r
+\r
+ $this->currentFilename = $filename;\r
\r
- if (!isset($this->parsers[$filename]))\r
+ if (!isset($this->parsers[$filename])) {\r
$this->parsers[$filename] = $this->_getPdfParser($filename);\r
- $this->current_parser =& $this->parsers[$filename];\r
+ $this->setPdfVersion(\r
+ max($this->getPdfVersion(), $this->parsers[$filename]->getPdfVersion())\r
+ );\r
+ }\r
+\r
+ $this->currentParser = $this->parsers[$filename];\r
\r
return $this->parsers[$filename]->getPageCount();\r
}\r
* @param string $filename\r
* @return fpdi_pdf_parser\r
*/\r
- function _getPdfParser($filename) {\r
- return new fpdi_pdf_parser($filename, $this);\r
+ protected function _getPdfParser($filename)\r
+ {\r
+ if (!class_exists('fpdi_pdf_parser')) {\r
+ require_once('fpdi_pdf_parser.php');\r
+ }\r
+ return new fpdi_pdf_parser($filename);\r
}\r
\r
/**\r
- * Get the current PDF version\r
+ * Get the current PDF version.\r
*\r
* @return string\r
*/\r
- function getPDFVersion() {\r
+ public function getPdfVersion()\r
+ {\r
return $this->PDFVersion;\r
}\r
\r
- /**\r
- * Set the PDF version\r
+ /**\r
+ * Set the PDF version.\r
*\r
- * @return string\r
+ * @param string $version\r
*/\r
- function setPDFVersion($version = '1.3') {\r
- $this->PDFVersion = $version;\r
- }\r
+ public function setPdfVersion($version = '1.3')\r
+ {\r
+ $this->PDFVersion = sprintf('%.1F', $version);\r
+ }\r
\r
/**\r
- * Import a page\r
+ * Import a page.\r
*\r
- * @param int $pageno pagenumber\r
- * @return int Index of imported page - to use with fpdf_tpl::useTemplate()\r
+ * The second parameter defines the bounding box that should be used to transform the page into a\r
+ * form XObject.\r
+ *\r
+ * Following values are available: MediaBox, CropBox, BleedBox, TrimBox, ArtBox.\r
+ * If a box is not especially defined its default box will be used:\r
+ *\r
+ * <ul>\r
+ * <li>CropBox: Default -> MediaBox</li>\r
+ * <li>BleedBox: Default -> CropBox</li>\r
+ * <li>TrimBox: Default -> CropBox</li>\r
+ * <li>ArtBox: Default -> CropBox</li>\r
+ * </ul>\r
+ *\r
+ * It is possible to get the used page box by the {@link getLastUsedPageBox()} method.\r
+ *\r
+ * @param int $pageNo The page number\r
+ * @param string $boxName The boundary box to use when transforming the page into a form XObject\r
+ * @param boolean $groupXObject Define the form XObject as a group XObject to support transparency (if used)\r
+ * @return int An id of the imported page/template to use with e.g. fpdf_tpl::useTemplate()\r
+ * @throws LogicException|InvalidArgumentException\r
+ * @see getLastUsedPageBox()\r
*/\r
- function importPage($pageno, $boxName = '/CropBox') {\r
- if ($this->_intpl) {\r
- return $this->error('Please import the desired pages before creating a new template.');\r
+ public function importPage($pageNo, $boxName = 'CropBox', $groupXObject = true)\r
+ {\r
+ if ($this->_inTpl) {\r
+ throw new LogicException('Please import the desired pages before creating a new template.');\r
}\r
\r
- $fn = $this->current_filename;\r
- \r
+ $fn = $this->currentFilename;\r
+ $boxName = '/' . ltrim($boxName, '/');\r
+\r
// check if page already imported\r
- $pageKey = $fn . '-' . ((int)$pageno) . $boxName;\r
- if (isset($this->_importedPages[$pageKey]))\r
+ $pageKey = $fn . '-' . ((int)$pageNo) . $boxName;\r
+ if (isset($this->_importedPages[$pageKey])) {\r
return $this->_importedPages[$pageKey];\r
+ }\r
\r
- $parser =& $this->parsers[$fn];\r
- $parser->setPageno($pageno);\r
+ $parser = $this->parsers[$fn];\r
+ $parser->setPageNo($pageNo);\r
\r
- if (!in_array($boxName, $parser->availableBoxes))\r
- return $this->Error(sprintf('Unknown box: %s', $boxName));\r
+ if (!in_array($boxName, $parser->availableBoxes)) {\r
+ throw new InvalidArgumentException(sprintf('Unknown box: %s', $boxName));\r
+ }\r
\r
- $pageboxes = $parser->getPageBoxes($pageno, $this->k);\r
+ $pageBoxes = $parser->getPageBoxes($pageNo, $this->k);\r
\r
/**\r
* MediaBox\r
* TrimBox: Default -> CropBox\r
* ArtBox: Default -> CropBox\r
*/\r
- if (!isset($pageboxes[$boxName]) && ($boxName == '/BleedBox' || $boxName == '/TrimBox' || $boxName == '/ArtBox'))\r
+ if (!isset($pageBoxes[$boxName]) && ($boxName == '/BleedBox' || $boxName == '/TrimBox' || $boxName == '/ArtBox'))\r
$boxName = '/CropBox';\r
- if (!isset($pageboxes[$boxName]) && $boxName == '/CropBox')\r
+ if (!isset($pageBoxes[$boxName]) && $boxName == '/CropBox')\r
$boxName = '/MediaBox';\r
\r
- if (!isset($pageboxes[$boxName]))\r
+ if (!isset($pageBoxes[$boxName]))\r
return false;\r
\r
$this->lastUsedPageBox = $boxName;\r
\r
- $box = $pageboxes[$boxName];\r
+ $box = $pageBoxes[$boxName];\r
\r
$this->tpl++;\r
- $this->tpls[$this->tpl] = array();\r
- $tpl =& $this->tpls[$this->tpl];\r
- $tpl['parser'] =& $parser;\r
+ $this->_tpls[$this->tpl] = array();\r
+ $tpl =& $this->_tpls[$this->tpl];\r
+ $tpl['parser'] = $parser;\r
$tpl['resources'] = $parser->getPageResources();\r
$tpl['buffer'] = $parser->getContent();\r
$tpl['box'] = $box;\r
- \r
+ $tpl['groupXObject'] = $groupXObject;\r
+ if ($groupXObject) {\r
+ $this->setPdfVersion(max($this->getPdfVersion(), 1.4));\r
+ }\r
+\r
// To build an array that can be used by PDF_TPL::useTemplate()\r
- $this->tpls[$this->tpl] = array_merge($this->tpls[$this->tpl], $box);\r
+ $this->_tpls[$this->tpl] = array_merge($this->_tpls[$this->tpl], $box);\r
\r
- // An imported page will start at 0,0 everytime. Translation will be set in _putformxobjects()\r
+ // An imported page will start at 0,0 all the time. Translation will be set in _putformxobjects()\r
$tpl['x'] = 0;\r
$tpl['y'] = 0;\r
\r
// handle rotated pages\r
- $rotation = $parser->getPageRotation($pageno);\r
+ $rotation = $parser->getPageRotation($pageNo);\r
$tpl['_rotationAngle'] = 0;\r
if (isset($rotation[1]) && ($angle = $rotation[1] % 360) != 0) {\r
$steps = $angle / 90;\r
}\r
\r
/**\r
- * Returns the last used page box\r
+ * Returns the last used page boundary box.\r
*\r
- * @return string\r
+ * @return string The used boundary box: MediaBox, CropBox, BleedBox, TrimBox or ArtBox\r
*/\r
- function getLastUsedPageBox() {\r
+ public function getLastUsedPageBox()\r
+ {\r
return $this->lastUsedPageBox;\r
}\r
- \r
- \r
- function useTemplate($tplidx, $_x = null, $_y = null, $_w = 0, $_h = 0, $adjustPageSize = false) {\r
- if ($adjustPageSize == true && is_null($_x) && is_null($_y)) {\r
- $size = $this->getTemplateSize($tplidx, $_w, $_h);\r
+\r
+ /**\r
+ * Use a template or imported page in current page or other template.\r
+ *\r
+ * You can use a template in a page or in another template.\r
+ * You can give the used template a new size. All parameters are optional.\r
+ * The width or height is calculated automatically if one is given. If no\r
+ * parameter is given the origin size as defined in beginTemplate() or of\r
+ * the imported page is used.\r
+ *\r
+ * The calculated or used width and height are returned as an array.\r
+ *\r
+ * @param int $tplIdx A valid template-id\r
+ * @param int $x The x-position\r
+ * @param int $y The y-position\r
+ * @param int $w The new width of the template\r
+ * @param int $h The new height of the template\r
+ * @param boolean $adjustPageSize If set to true the current page will be resized to fit the dimensions\r
+ * of the template\r
+ *\r
+ * @return array The height and width of the template (array('w' => ..., 'h' => ...))\r
+ * @throws LogicException|InvalidArgumentException\r
+ */\r
+ public function useTemplate($tplIdx, $x = null, $y = null, $w = 0, $h = 0, $adjustPageSize = false)\r
+ {\r
+ if ($adjustPageSize == true && is_null($x) && is_null($y)) {\r
+ $size = $this->getTemplateSize($tplIdx, $w, $h);\r
$orientation = $size['w'] > $size['h'] ? 'L' : 'P';\r
$size = array($size['w'], $size['h']);\r
\r
} else {\r
$size = $this->_getpagesize($size);\r
\r
- if($orientation!=$this->CurOrientation || $size[0]!=$this->CurPageSize[0] || $size[1]!=$this->CurPageSize[1])\r
- {\r
+ if($orientation != $this->CurOrientation ||\r
+ $size[0] != $this->CurPageSize[0] ||\r
+ $size[1] != $this->CurPageSize[1]\r
+ ) {\r
// New size or orientation\r
- if($orientation=='P')\r
- {\r
+ if ($orientation=='P') {\r
$this->w = $size[0];\r
$this->h = $size[1];\r
- }\r
- else\r
- {\r
+ } else {\r
$this->w = $size[1];\r
$this->h = $size[0];\r
}\r
- $this->wPt = $this->w*$this->k;\r
- $this->hPt = $this->h*$this->k;\r
- $this->PageBreakTrigger = $this->h-$this->bMargin;\r
+ $this->wPt = $this->w * $this->k;\r
+ $this->hPt = $this->h * $this->k;\r
+ $this->PageBreakTrigger = $this->h - $this->bMargin;\r
$this->CurOrientation = $orientation;\r
$this->CurPageSize = $size;\r
$this->PageSizes[$this->page] = array($this->wPt, $this->hPt);\r
}\r
\r
$this->_out('q 0 J 1 w 0 j 0 G 0 g'); // reset standard values\r
- $s = parent::useTemplate($tplidx, $_x, $_y, $_w, $_h);\r
+ $size = parent::useTemplate($tplIdx, $x, $y, $w, $h);\r
$this->_out('Q');\r
\r
- return $s;\r
+ return $size;\r
}\r
\r
/**\r
- * Private method, that rebuilds all needed objects of source files\r
+ * Copy all imported objects to the resulting document.\r
*/\r
- function _putimportedobjects() {\r
- if (is_array($this->parsers) && count($this->parsers) > 0) {\r
- foreach($this->parsers AS $filename => $p) {\r
- $this->current_parser =& $this->parsers[$filename];\r
- if (isset($this->_obj_stack[$filename]) && is_array($this->_obj_stack[$filename])) {\r
- while(($n = key($this->_obj_stack[$filename])) !== null) {\r
- $nObj = $this->current_parser->pdf_resolve_object($this->current_parser->c, $this->_obj_stack[$filename][$n][1]);\r
- \r
- $this->_newobj($this->_obj_stack[$filename][$n][0]);\r
- \r
- if ($nObj[0] == PDF_TYPE_STREAM) {\r
- $this->pdf_write_value($nObj);\r
- } else {\r
- $this->pdf_write_value($nObj[1]);\r
- }\r
- \r
- $this->_out('endobj');\r
- $this->_obj_stack[$filename][$n] = null; // free memory\r
- unset($this->_obj_stack[$filename][$n]);\r
- reset($this->_obj_stack[$filename]);\r
- }\r
+ protected function _putimportedobjects()\r
+ {\r
+ foreach($this->parsers AS $filename => $p) {\r
+ $this->currentParser = $p;\r
+ if (!isset($this->_objStack[$filename]) || !is_array($this->_objStack[$filename])) {\r
+ continue;\r
+ }\r
+ while(($n = key($this->_objStack[$filename])) !== null) {\r
+ try {\r
+ $nObj = $this->currentParser->resolveObject($this->_objStack[$filename][$n][1]);\r
+ } catch (Exception $e) {\r
+ $nObj = array(pdf_parser::TYPE_OBJECT, pdf_parser::TYPE_NULL);\r
}\r
+\r
+ $this->_newobj($this->_objStack[$filename][$n][0]);\r
+\r
+ if ($nObj[0] == pdf_parser::TYPE_STREAM) {\r
+ $this->_writeValue($nObj);\r
+ } else {\r
+ $this->_writeValue($nObj[1]);\r
+ }\r
+\r
+ $this->_out("\nendobj");\r
+ $this->_objStack[$filename][$n] = null; // free memory\r
+ unset($this->_objStack[$filename][$n]);\r
+ reset($this->_objStack[$filename]);\r
}\r
}\r
}\r
- \r
- \r
+\r
/**\r
- * Private Method that writes the form xobjects\r
+ * Writes the form XObjects to the PDF document.\r
*/\r
- function _putformxobjects() {\r
- $filter=($this->compress) ? '/Filter /FlateDecode ' : '';\r
- reset($this->tpls);\r
- foreach($this->tpls AS $tplidx => $tpl) {\r
- $p=($this->compress) ? gzcompress($tpl['buffer']) : $tpl['buffer'];\r
- $this->_newobj();\r
- $cN = $this->n; // TCPDF/Protection: rem current "n"\r
+ protected function _putformxobjects()\r
+ {\r
+ $filter = ($this->compress) ? '/Filter /FlateDecode ' : '';\r
+ reset($this->_tpls);\r
+ foreach($this->_tpls AS $tplIdx => $tpl) {\r
+ $this->_newobj();\r
+ $currentN = $this->n; // TCPDF/Protection: rem current "n"\r
\r
- $this->tpls[$tplidx]['n'] = $this->n;\r
+ $this->_tpls[$tplIdx]['n'] = $this->n;\r
$this->_out('<<' . $filter . '/Type /XObject');\r
$this->_out('/Subtype /Form');\r
$this->_out('/FormType 1');\r
\r
if ($tpl['_rotationAngle'] <> 0) {\r
$angle = $tpl['_rotationAngle'] * M_PI/180;\r
- $c=cos($angle);\r
- $s=sin($angle);\r
+ $c = cos($angle);\r
+ $s = sin($angle);\r
\r
switch($tpl['_rotationAngle']) {\r
case -90:\r
break;\r
}\r
}\r
- } elseif ($tpl['x'] != 0 || $tpl['y'] != 0) {\r
+ } else if ($tpl['x'] != 0 || $tpl['y'] != 0) {\r
$tx = -$tpl['x'] * 2;\r
$ty = $tpl['y'] * 2;\r
}\r
$this->_out('/Resources ');\r
\r
if (isset($tpl['resources'])) {\r
- $this->current_parser =& $tpl['parser'];\r
- $this->pdf_write_value($tpl['resources']); // "n" will be changed\r
+ $this->currentParser = $tpl['parser'];\r
+ $this->_writeValue($tpl['resources']); // "n" will be changed\r
} else {\r
+\r
$this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');\r
- if (isset($this->_res['tpl'][$tplidx]['fonts']) && count($this->_res['tpl'][$tplidx]['fonts'])) {\r
- $this->_out('/Font <<');\r
- foreach($this->_res['tpl'][$tplidx]['fonts'] as $font)\r
- $this->_out('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R');\r
- $this->_out('>>');\r
- }\r
- if(isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images']) || \r
- isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls']))\r
- {\r
- $this->_out('/XObject <<');\r
- if (isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images'])) {\r
- foreach($this->_res['tpl'][$tplidx]['images'] as $image)\r
- $this->_out('/I' . $image['i'] . ' ' . $image['n'] . ' 0 R');\r
+ if (isset($this->_res['tpl'][$tplIdx])) {\r
+ $res = $this->_res['tpl'][$tplIdx];\r
+\r
+ if (isset($res['fonts']) && count($res['fonts'])) {\r
+ $this->_out('/Font <<');\r
+ foreach ($res['fonts'] as $font)\r
+ $this->_out('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R');\r
+ $this->_out('>>');\r
}\r
- if (isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls'])) {\r
- foreach($this->_res['tpl'][$tplidx]['tpls'] as $i => $tpl)\r
- $this->_out($this->tplprefix . $i . ' ' . $tpl['n'] . ' 0 R');\r
+ if (isset($res['images']) && count($res['images']) ||\r
+ isset($res['tpls']) && count($res['tpls']))\r
+ {\r
+ $this->_out('/XObject <<');\r
+ if (isset($res['images'])) {\r
+ foreach ($res['images'] as $image)\r
+ $this->_out('/I' . $image['i'] . ' ' . $image['n'] . ' 0 R');\r
+ }\r
+ if (isset($res['tpls'])) {\r
+ foreach ($res['tpls'] as $i => $_tpl)\r
+ $this->_out($this->tplPrefix . $i . ' ' . $_tpl['n'] . ' 0 R');\r
+ }\r
+ $this->_out('>>');\r
}\r
$this->_out('>>');\r
- }\r
- $this->_out('>>');\r
+ }\r
}\r
- \r
- $this->_out('/Group <</Type/Group/S/Transparency>>');\r
- \r
- $nN = $this->n; // TCPDF: rem new "n"\r
- $this->n = $cN; // TCPDF: reset to current "n"\r
+\r
+ if (isset($tpl['groupXObject']) && $tpl['groupXObject']) {\r
+ $this->_out('/Group <</Type/Group/S/Transparency>>');\r
+ }\r
+\r
+ $newN = $this->n; // TCPDF: rem new "n"\r
+ $this->n = $currentN; // TCPDF: reset to current "n"\r
+\r
+ $buffer = ($this->compress) ? gzcompress($tpl['buffer']) : $tpl['buffer'];\r
+\r
if (is_subclass_of($this, 'TCPDF')) {\r
- $p = $this->_getrawstream($p);\r
- $this->_out('/Length ' . strlen($p) . ' >>');\r
- $this->_out("stream\n" . $p . "\nendstream");\r
+ $buffer = $this->_getrawstream($buffer);\r
+ $this->_out('/Length ' . strlen($buffer) . ' >>');\r
+ $this->_out("stream\n" . $buffer . "\nendstream");\r
} else {\r
- $this->_out('/Length ' . strlen($p) . ' >>');\r
- $this->_putstream($p);\r
+ $this->_out('/Length ' . strlen($buffer) . ' >>');\r
+ $this->_putstream($buffer);\r
}\r
$this->_out('endobj');\r
- $this->n = $nN; // TCPDF: reset to new "n"\r
+ $this->n = $newN; // TCPDF: reset to new "n"\r
}\r
\r
$this->_putimportedobjects();\r
}\r
\r
/**\r
+ * Creates and optionally write the object definition to the document.\r
+ *\r
* Rewritten to handle existing own defined objects\r
+ *\r
+ * @param bool $objId\r
+ * @param bool $onlyNewObj\r
+ * @return bool|int\r
*/\r
- function _newobj($obj_id = false, $onlynewobj = false) {\r
- if (!$obj_id) {\r
- $obj_id = ++$this->n;\r
+ public function _newobj($objId = false, $onlyNewObj = false)\r
+ {\r
+ if (!$objId) {\r
+ $objId = ++$this->n;\r
}\r
\r
//Begin a new object\r
- if (!$onlynewobj) {\r
- $this->offsets[$obj_id] = is_subclass_of($this, 'TCPDF') ? $this->bufferlen : strlen($this->buffer);\r
- $this->_out($obj_id . ' 0 obj');\r
- $this->_current_obj_id = $obj_id; // for later use with encryption\r
+ if (!$onlyNewObj) {\r
+ $this->offsets[$objId] = is_subclass_of($this, 'TCPDF') ? $this->bufferlen : strlen($this->buffer);\r
+ $this->_out($objId . ' 0 obj');\r
+ $this->_currentObjId = $objId; // for later use with encryption\r
}\r
\r
- return $obj_id;\r
+ return $objId;\r
}\r
\r
/**\r
- * Writes a value\r
+ * Writes a PDF value to the resulting document.\r
+ *\r
* Needed to rebuild the source document\r
*\r
* @param mixed $value A PDF-Value. Structure of values see cases in this method\r
*/\r
- function pdf_write_value(&$value)\r
+ protected function _writeValue(&$value)\r
{\r
if (is_subclass_of($this, 'TCPDF')) {\r
- parent::pdf_write_value($value);\r
+ parent::_prepareValue($value);\r
}\r
\r
switch ($value[0]) {\r
\r
- case PDF_TYPE_TOKEN:\r
+ case pdf_parser::TYPE_TOKEN:\r
$this->_straightOut($value[1] . ' ');\r
break;\r
- case PDF_TYPE_NUMERIC:\r
- case PDF_TYPE_REAL:\r
+ case pdf_parser::TYPE_NUMERIC:\r
+ case pdf_parser::TYPE_REAL:\r
if (is_float($value[1]) && $value[1] != 0) {\r
$this->_straightOut(rtrim(rtrim(sprintf('%F', $value[1]), '0'), '.') . ' ');\r
} else {\r
}\r
break;\r
\r
- case PDF_TYPE_ARRAY:\r
+ case pdf_parser::TYPE_ARRAY:\r
\r
// An array. Output the proper\r
// structure and move on.\r
\r
$this->_straightOut('[');\r
for ($i = 0; $i < count($value[1]); $i++) {\r
- $this->pdf_write_value($value[1][$i]);\r
+ $this->_writeValue($value[1][$i]);\r
}\r
\r
$this->_out(']');\r
break;\r
\r
- case PDF_TYPE_DICTIONARY:\r
+ case pdf_parser::TYPE_DICTIONARY:\r
\r
// A dictionary.\r
$this->_straightOut('<<');\r
\r
while (list($k, $v) = each($value[1])) {\r
$this->_straightOut($k . ' ');\r
- $this->pdf_write_value($v);\r
+ $this->_writeValue($v);\r
}\r
\r
$this->_straightOut('>>');\r
break;\r
\r
- case PDF_TYPE_OBJREF:\r
+ case pdf_parser::TYPE_OBJREF:\r
\r
// An indirect object reference\r
// Fill the object stack if needed\r
- $cpfn =& $this->current_parser->filename;\r
- \r
- if (!isset($this->_don_obj_stack[$cpfn][$value[1]])) {\r
+ $cpfn =& $this->currentParser->filename;\r
+ if (!isset($this->_doneObjStack[$cpfn][$value[1]])) {\r
$this->_newobj(false, true);\r
- $this->_obj_stack[$cpfn][$value[1]] = array($this->n, $value);\r
- $this->_don_obj_stack[$cpfn][$value[1]] = array($this->n, $value); // Value is maybee obsolete!!!\r
+ $this->_objStack[$cpfn][$value[1]] = array($this->n, $value);\r
+ $this->_doneObjStack[$cpfn][$value[1]] = array($this->n, $value);\r
}\r
- $objid = $this->_don_obj_stack[$cpfn][$value[1]][0];\r
+ $objId = $this->_doneObjStack[$cpfn][$value[1]][0];\r
\r
- $this->_out($objid . ' 0 R');\r
+ $this->_out($objId . ' 0 R');\r
break;\r
\r
- case PDF_TYPE_STRING:\r
+ case pdf_parser::TYPE_STRING:\r
\r
// A string.\r
$this->_straightOut('(' . $value[1] . ')');\r
\r
break;\r
\r
- case PDF_TYPE_STREAM:\r
+ case pdf_parser::TYPE_STREAM:\r
\r
// A stream. First, output the\r
// stream dictionary, then the\r
// stream data itself.\r
- $this->pdf_write_value($value[1]);\r
+ $this->_writeValue($value[1]);\r
$this->_out('stream');\r
$this->_out($value[2][1]);\r
- $this->_out('endstream');\r
+ $this->_straightOut("endstream");\r
break;\r
\r
- case PDF_TYPE_HEX:\r
+ case pdf_parser::TYPE_HEX:\r
$this->_straightOut('<' . $value[1] . '>');\r
break;\r
\r
- case PDF_TYPE_BOOLEAN:\r
+ case pdf_parser::TYPE_BOOLEAN:\r
$this->_straightOut($value[1] ? 'true ' : 'false ');\r
break;\r
\r
- case PDF_TYPE_NULL:\r
+ case pdf_parser::TYPE_NULL:\r
// The null object.\r
\r
$this->_straightOut('null ');\r
\r
\r
/**\r
- * Modified so not each call will add a newline to the output.\r
+ * Modified _out() method so not each call will add a newline to the output.\r
*/\r
- function _straightOut($s) {\r
+ protected function _straightOut($s)\r
+ {\r
if (!is_subclass_of($this, 'TCPDF')) {\r
- if($this->state==2)\r
+ if ($this->state == 2) {\r
$this->pages[$this->page] .= $s;\r
- else\r
+ } else {\r
$this->buffer .= $s;\r
+ }\r
+\r
} else {\r
if ($this->state == 2) {\r
if ($this->inxobj) {\r
// we are inside an XObject template\r
$this->xobjects[$this->xobjid]['outdata'] .= $s;\r
- } elseif ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {\r
+ } else if ((!$this->InFooter) AND isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {\r
// puts data before page footer\r
$pagebuff = $this->getPageBuffer($this->page);\r
$page = substr($pagebuff, 0, -$this->footerlen[$this->page]);\r
$footer = substr($pagebuff, -$this->footerlen[$this->page]);\r
- $this->setPageBuffer($this->page, $page.$s.$footer);\r
+ $this->setPageBuffer($this->page, $page . $s . $footer);\r
// update footer position\r
$this->footerpos[$this->page] += strlen($s);\r
} else {\r
// set page data\r
$this->setPageBuffer($this->page, $s, true);\r
}\r
- } elseif ($this->state > 0) {\r
+ } else if ($this->state > 0) {\r
// set general data\r
$this->setBuffer($s);\r
}\r
}\r
\r
/**\r
- * rewritten to close opened parsers\r
+ * Ends the document\r
*\r
+ * Overwritten to close opened parsers\r
*/\r
- function _enddoc() {\r
+ public function _enddoc()\r
+ {\r
parent::_enddoc();\r
$this->_closeParsers();\r
}\r
\r
/**\r
- * close all files opened by parsers\r
+ * Close all files opened by parsers.\r
+ *\r
+ * @return boolean\r
*/\r
- function _closeParsers() {\r
- if ($this->state > 2 && count($this->parsers) > 0) {\r
+ protected function _closeParsers()\r
+ {\r
+ if ($this->state > 2) {\r
$this->cleanUp();\r
return true;\r
}\r
+\r
return false;\r
}\r
\r
/**\r
- * Removes cylced references and closes the file handles of the parser objects\r
+ * Removes cycled references and closes the file handles of the parser objects.\r
*/\r
- function cleanUp() {\r
- foreach ($this->parsers as $k => $_){\r
- $this->parsers[$k]->cleanUp();\r
- $this->parsers[$k] = null;\r
- unset($this->parsers[$k]);\r
+ public function cleanUp()\r
+ {\r
+ while (($parser = array_pop($this->parsers)) !== null) {\r
+ /**\r
+ * @var fpdi_pdf_parser $parser\r
+ */\r
+ $parser->closeFile();\r
}\r
}\r
}
\ No newline at end of file
+++ /dev/null
-<?php
-//
-// FPDI - Version 1.4.4
-//
-// Copyright 2004-2013 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.
-//
-
-global $CFG;
-require_once($CFG->libdir.'/pdflib.php');
-
-/**
- * This class is used as a bridge between TCPDF and FPDI
- * and will create the possibility to use both FPDF and TCPDF
- * via one FPDI version.
- *
- * We'll simply remap TCPDF to FPDF again.
- *
- * It'll be loaded and extended by FPDF_TPL.
- * Modified to extend the moodle TCPDF wrapper instead.
- */
-class FPDF extends pdf {
-
- function _putstream($s, $n=0) {
- $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']);
- }
- }
-
- return $out;
- }
-
- /**
- * Encryption of imported data by FPDI
- *
- * @param array $value
- */
- function pdf_write_value(&$value) {
- switch ($value[0]) {
- case PDF_TYPE_STRING:
- if ($this->encrypted) {
- $value[1] = $this->_unescape($value[1]);
- $value[1] = $this->_encrypt_data($this->_current_obj_id, $value[1]);
- $value[1] = TCPDF_STATIC::_escape($value[1]);
- }
- break;
-
- case PDF_TYPE_STREAM:
- if ($this->encrypted) {
- $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->_encrypt_data($this->_current_obj_id, $value[1]);
-
- // remake hexstring of encrypted string
- $value[1] = $this->str2hex($value[1]);
- }
- break;
- }
- }
-
- /**
- * Unescapes a PDF string
- *
- * @param string $s
- * @return string
- */
- function _unescape($s) {
- $out = '';
- for ($count = 0, $n = strlen($s); $count < $n; $count++) {
- if ($s[$count] != '\\' || $count == $n-1) {
- $out .= $s[$count];
- } else {
- switch ($s[++$count]) {
- case ')':
- case '(':
- case '\\':
- $out .= $s[$count];
- break;
- case 'f':
- $out .= chr(0x0C);
- break;
- case 'b':
- $out .= chr(0x08);
- break;
- case 't':
- $out .= chr(0x09);
- break;
- case 'r':
- $out .= chr(0x0D);
- break;
- case 'n':
- $out .= chr(0x0A);
- break;
- case "\r":
- if ($count != $n-1 && $s[$count+1] == "\n")
- $count++;
- break;
- case "\n":
- break;
- default:
- // Octal-Values
- if (ord($s[$count]) >= ord('0') &&
- ord($s[$count]) <= ord('9')) {
- $oct = ''. $s[$count];
-
- if (ord($s[$count+1]) >= ord('0') &&
- ord($s[$count+1]) <= ord('9')) {
- $oct .= $s[++$count];
-
- if (ord($s[$count+1]) >= ord('0') &&
- ord($s[$count+1]) <= ord('9')) {
- $oct .= $s[++$count];
- }
- }
-
- $out .= chr(octdec($oct));
- } else {
- $out .= $s[$count];
- }
- }
- }
- }
- return $out;
- }
-
- /**
- * Hexadecimal to string
- *
- * @param string $hex
- * @return string
- */
- function hex2str($hex) {
- return pack('H*', str_replace(array("\r", "\n", ' '), '', $hex));
- }
-
- /**
- * String to hexadecimal
- *
- * @param string $str
- * @return string
- */
- function str2hex($str) {
- return current(unpack('H*', $str));
- }
-}
--- /dev/null
+<?php
+//
+// FPDI - Version 1.5.4
+//
+// Copyright 2004-2015 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.
+//
+
+/**
+ * This file is used as a bridge between TCPDF or FPDF
+ * It will dynamically create the class extending the available
+ * class FPDF or TCPDF.
+ *
+ * This way it is possible to use FPDI for both FPDF and TCPDF with one FPDI version.
+ */
+
+if (!class_exists('TCPDF', false)) {
+ /**
+ * Class fpdi_bridge
+ */
+ class fpdi_bridge extends FPDF
+ {
+ // empty body
+ }
+
+} else {
+
+ /**
+ * Class fpdi_bridge
+ */
+ class fpdi_bridge extends TCPDF
+ {
+ /**
+ * Array of Tpl-Data
+ *
+ * @var array
+ */
+ protected $_tpls = array();
+
+ /**
+ * Name-prefix of Templates used in Resources-Dictionary
+ *
+ * @var string A String defining the Prefix used as Template-Object-Names. Have to begin with an /
+ */
+ public $tplPrefix = "/TPL";
+
+ /**
+ * Current Object Id.
+ *
+ * @var integer
+ */
+ protected $_currentObjId;
+
+ /**
+ * Return XObjects Dictionary.
+ *
+ * Overwritten to add additional XObjects to the resources dictionary of TCPDF
+ *
+ * @return string
+ */
+ protected function _getxobjectdict()
+ {
+ $out = parent::_getxobjectdict();
+ foreach ($this->_tpls as $tplIdx => $tpl) {
+ $out .= sprintf('%s%d %d 0 R', $this->tplPrefix, $tplIdx, $tpl['n']);
+ }
+
+ return $out;
+ }
+
+ /**
+ * Writes a PDF value to the resulting document.
+ *
+ * Prepares the value for encryption of imported data by FPDI
+ *
+ * @param array $value
+ */
+ protected function _prepareValue(&$value)
+ {
+ switch ($value[0]) {
+ case pdf_parser::TYPE_STRING:
+ if ($this->encrypted) {
+ $value[1] = $this->_unescape($value[1]);
+ $value[1] = $this->_encrypt_data($this->_currentObjId, $value[1]);
+ $value[1] = TCPDF_STATIC::_escape($value[1]);
+ }
+ break;
+
+ case pdf_parser::TYPE_STREAM:
+ if ($this->encrypted) {
+ $value[2][1] = $this->_encrypt_data($this->_currentObjId, $value[2][1]);
+ $value[1][1]['/Length'] = array(
+ pdf_parser::TYPE_NUMERIC,
+ strlen($value[2][1])
+ );
+ }
+ break;
+
+ case pdf_parser::TYPE_HEX:
+ if ($this->encrypted) {
+ $value[1] = $this->hex2str($value[1]);
+ $value[1] = $this->_encrypt_data($this->_currentObjId, $value[1]);
+
+ // remake hexstring of encrypted string
+ $value[1] = $this->str2hex($value[1]);
+ }
+ break;
+ }
+ }
+
+ /**
+ * Un-escapes a PDF string
+ *
+ * @param string $s
+ * @return string
+ */
+ protected function _unescape($s)
+ {
+ $out = '';
+ for ($count = 0, $n = strlen($s); $count < $n; $count++) {
+ if ($s[$count] != '\\' || $count == $n-1) {
+ $out .= $s[$count];
+ } else {
+ switch ($s[++$count]) {
+ case ')':
+ case '(':
+ case '\\':
+ $out .= $s[$count];
+ break;
+ case 'f':
+ $out .= chr(0x0C);
+ break;
+ case 'b':
+ $out .= chr(0x08);
+ break;
+ case 't':
+ $out .= chr(0x09);
+ break;
+ case 'r':
+ $out .= chr(0x0D);
+ break;
+ case 'n':
+ $out .= chr(0x0A);
+ break;
+ case "\r":
+ if ($count != $n-1 && $s[$count+1] == "\n")
+ $count++;
+ break;
+ case "\n":
+ break;
+ default:
+ // Octal-Values
+ if (ord($s[$count]) >= ord('0') &&
+ ord($s[$count]) <= ord('9')) {
+ $oct = ''. $s[$count];
+
+ if (ord($s[$count+1]) >= ord('0') &&
+ ord($s[$count+1]) <= ord('9')) {
+ $oct .= $s[++$count];
+
+ if (ord($s[$count+1]) >= ord('0') &&
+ ord($s[$count+1]) <= ord('9')) {
+ $oct .= $s[++$count];
+ }
+ }
+
+ $out .= chr(octdec($oct));
+ } else {
+ $out .= $s[$count];
+ }
+ }
+ }
+ }
+ return $out;
+ }
+
+ /**
+ * Hexadecimal to string
+ *
+ * @param string $data
+ * @return string
+ */
+ public function hex2str($data)
+ {
+ $data = preg_replace('/[^0-9A-Fa-f]/', '', rtrim($data, '>'));
+ if ((strlen($data) % 2) == 1) {
+ $data .= '0';
+ }
+
+ return pack('H*', $data);
+ }
+
+ /**
+ * String to hexadecimal
+ *
+ * @param string $str
+ * @return string
+ */
+ public function str2hex($str)
+ {
+ return current(unpack('H*', $str));
+ }
+ }
+}
\ No newline at end of file
<?php\r
//\r
-// FPDI - Version 1.4.4\r
+// FPDI - Version 1.5.4\r
//\r
-// Copyright 2004-2013 Setasign - Jan Slabon\r
+// Copyright 2004-2015 Setasign - Jan Slabon\r
//\r
// Licensed under the Apache License, Version 2.0 (the "License");\r
// you may not use this file except in compliance with the License.\r
// limitations under the License.\r
//\r
\r
-require_once('pdf_parser.php');\r
-\r
-class fpdi_pdf_parser extends pdf_parser {\r
+if (!class_exists('pdf_parser')) {\r
+ require_once('pdf_parser.php');\r
+}\r
\r
+/**\r
+ * Class fpdi_pdf_parser\r
+ */\r
+class fpdi_pdf_parser extends pdf_parser\r
+{\r
/**\r
* Pages\r
- * Index beginns at 0\r
+ *\r
+ * Index begins at 0\r
*\r
* @var array\r
*/\r
- var $pages;\r
+ protected $_pages;\r
\r
/**\r
* Page count\r
+ *\r
* @var integer\r
*/\r
- var $page_count;\r
+ protected $_pageCount;\r
\r
/**\r
- * actual page number\r
+ * Current page number\r
+ *\r
* @var integer\r
*/\r
- var $pageno;\r
+ public $pageNo;\r
\r
/**\r
- * PDF Version of imported Document\r
+ * PDF version of imported document\r
+ *\r
* @var string\r
*/\r
- var $pdfVersion;\r
- \r
- /**\r
- * FPDI Reference\r
- * @var object\r
- */\r
- var $fpdi;\r
+ public $_pdfVersion;\r
\r
/**\r
* Available BoxTypes\r
*\r
* @var array\r
*/\r
- var $availableBoxes = array('/MediaBox', '/CropBox', '/BleedBox', '/TrimBox', '/ArtBox');\r
+ public $availableBoxes = array('/MediaBox', '/CropBox', '/BleedBox', '/TrimBox', '/ArtBox');\r
\r
/**\r
- * Constructor\r
+ * The constructor.\r
*\r
- * @param string $filename Source-Filename\r
- * @param object $fpdi Object of type fpdi\r
+ * @param string $filename The source filename\r
*/\r
- function fpdi_pdf_parser($filename, &$fpdi) {\r
- $this->fpdi =& $fpdi;\r
- \r
- parent::pdf_parser($filename);\r
+ public function __construct($filename)\r
+ {\r
+ parent::__construct($filename);\r
\r
// resolve Pages-Dictonary\r
- $pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']);\r
+ $pages = $this->resolveObject($this->_root[1][1]['/Pages']);\r
\r
// Read pages\r
- $this->read_pages($this->c, $pages, $this->pages);\r
+ $this->_readPages($pages, $this->_pages);\r
\r
// count pages;\r
- $this->page_count = count($this->pages);\r
- }\r
- \r
- /**\r
- * Removes reference to fpdi object and closes the file handle\r
- */\r
- function cleanUp() {\r
- $this->fpdi = null;\r
- $this->closeFile();\r
+ $this->_pageCount = count($this->_pages);\r
}\r
\r
/**\r
- * Overwrite parent::error()\r
- *\r
- * @param string $msg Error-Message\r
- */\r
- function error($msg) {\r
- $this->fpdi->error($msg); \r
- }\r
- \r
- /**\r
- * Get pagecount from sourcefile\r
+ * Get page count from source file.\r
*\r
* @return int\r
*/\r
- function getPageCount() {\r
- return $this->page_count;\r
+ public function getPageCount()\r
+ {\r
+ return $this->_pageCount;\r
}\r
\r
-\r
/**\r
- * Set pageno\r
+ * Set the page number.\r
*\r
- * @param int $pageno Pagenumber to use\r
+ * @param int $pageNo Page number to use\r
+ * @throws InvalidArgumentException\r
*/\r
- function setPageno($pageno) {\r
- $pageno = ((int) $pageno) - 1;\r
+ public function setPageNo($pageNo)\r
+ {\r
+ $pageNo = ((int) $pageNo) - 1;\r
\r
- if ($pageno < 0 || $pageno >= $this->getPageCount()) {\r
- $this->fpdi->error('Pagenumber is wrong!');\r
+ if ($pageNo < 0 || $pageNo >= $this->getPageCount()) {\r
+ throw new InvalidArgumentException('Invalid page number!');\r
}\r
\r
- $this->pageno = $pageno;\r
+ $this->pageNo = $pageNo;\r
}\r
\r
/**\r
* Get page-resources from current page\r
*\r
- * @return array\r
+ * @return array|boolean\r
*/\r
- function getPageResources() {\r
- return $this->_getPageResources($this->pages[$this->pageno]);\r
+ public function getPageResources()\r
+ {\r
+ return $this->_getPageResources($this->_pages[$this->pageNo]);\r
}\r
\r
/**\r
- * Get page-resources from /Page\r
+ * Get page-resources from a /Page dictionary.\r
*\r
* @param array $obj Array of pdf-data\r
+ * @return array|boolean\r
*/\r
- function _getPageResources ($obj) { // $obj = /Page\r
- $obj = $this->pdf_resolve_object($this->c, $obj);\r
+ protected function _getPageResources($obj)\r
+ {\r
+ $obj = $this->resolveObject($obj);\r
\r
// If the current object has a resources\r
// dictionary associated with it, we use\r
// it. Otherwise, we move back to its\r
// parent object.\r
- if (isset ($obj[1][1]['/Resources'])) {\r
- $res = $this->pdf_resolve_object($this->c, $obj[1][1]['/Resources']);\r
- if ($res[0] == PDF_TYPE_OBJECT)\r
+ if (isset($obj[1][1]['/Resources'])) {\r
+ $res = $this->resolveObject($obj[1][1]['/Resources']);\r
+ if ($res[0] == pdf_parser::TYPE_OBJECT)\r
return $res[1];\r
return $res;\r
- } else {\r
- if (!isset ($obj[1][1]['/Parent'])) {\r
- return false;\r
- } else {\r
- $res = $this->_getPageResources($obj[1][1]['/Parent']);\r
- if ($res[0] == PDF_TYPE_OBJECT)\r
- return $res[1];\r
- return $res;\r
- }\r
}\r
- }\r
\r
+ if (!isset($obj[1][1]['/Parent'])) {\r
+ return false;\r
+ }\r
+\r
+ $res = $this->_getPageResources($obj[1][1]['/Parent']);\r
+ if ($res[0] == pdf_parser::TYPE_OBJECT)\r
+ return $res[1];\r
+ return $res;\r
+ }\r
\r
/**\r
- * Get content of current page\r
+ * Get content of current page.\r
*\r
- * If more /Contents is an array, the streams are concated\r
+ * If /Contents is an array, the streams are concatenated\r
*\r
* @return string\r
*/\r
- function getContent() {\r
+ public function getContent()\r
+ {\r
$buffer = '';\r
\r
- if (isset($this->pages[$this->pageno][1][1]['/Contents'])) {\r
- $contents = $this->_getPageContent($this->pages[$this->pageno][1][1]['/Contents']);\r
- foreach($contents AS $tmp_content) {\r
- $buffer .= $this->_rebuildContentStream($tmp_content) . ' ';\r
+ if (isset($this->_pages[$this->pageNo][1][1]['/Contents'])) {\r
+ $contents = $this->_getPageContent($this->_pages[$this->pageNo][1][1]['/Contents']);\r
+ foreach ($contents AS $tmpContent) {\r
+ $buffer .= $this->_unFilterStream($tmpContent) . ' ';\r
}\r
}\r
\r
return $buffer;\r
}\r
- \r
- \r
+\r
/**\r
- * Resolve all content-objects\r
+ * Resolve all content objects.\r
*\r
- * @param array $content_ref\r
+ * @param array $contentRef\r
* @return array\r
*/\r
- function _getPageContent($content_ref) {\r
+ protected function _getPageContent($contentRef)\r
+ {\r
$contents = array();\r
\r
- if ($content_ref[0] == PDF_TYPE_OBJREF) {\r
- $content = $this->pdf_resolve_object($this->c, $content_ref);\r
- if ($content[1][0] == PDF_TYPE_ARRAY) {\r
+ if ($contentRef[0] == pdf_parser::TYPE_OBJREF) {\r
+ $content = $this->resolveObject($contentRef);\r
+ if ($content[1][0] == pdf_parser::TYPE_ARRAY) {\r
$contents = $this->_getPageContent($content[1]);\r
} else {\r
$contents[] = $content;\r
}\r
- } elseif ($content_ref[0] == PDF_TYPE_ARRAY) {\r
- foreach ($content_ref[1] AS $tmp_content_ref) {\r
- $contents = array_merge($contents,$this->_getPageContent($tmp_content_ref));\r
+ } else if ($contentRef[0] == pdf_parser::TYPE_ARRAY) {\r
+ foreach ($contentRef[1] AS $tmp_content_ref) {\r
+ $contents = array_merge($contents, $this->_getPageContent($tmp_content_ref));\r
}\r
}\r
\r
return $contents;\r
}\r
\r
-\r
/**\r
- * Rebuild content-streams\r
+ * Get a boundary box from a page\r
*\r
- * @param array $obj\r
- * @return string\r
- */\r
- function _rebuildContentStream($obj) {\r
- $filters = array();\r
- \r
- if (isset($obj[1][1]['/Filter'])) {\r
- $_filter = $obj[1][1]['/Filter'];\r
-\r
- if ($_filter[0] == PDF_TYPE_OBJREF) {\r
- $tmpFilter = $this->pdf_resolve_object($this->c, $_filter);\r
- $_filter = $tmpFilter[1];\r
- }\r
- \r
- if ($_filter[0] == PDF_TYPE_TOKEN) {\r
- $filters[] = $_filter;\r
- } elseif ($_filter[0] == PDF_TYPE_ARRAY) {\r
- $filters = $_filter[1];\r
- }\r
- }\r
-\r
- $stream = $obj[2][1];\r
-\r
- foreach ($filters AS $_filter) {\r
- switch ($_filter[1]) {\r
- case '/FlateDecode':\r
- case '/Fl':\r
- // $stream .= "\x0F\x0D"; // in an errorious stream this suffix could work\r
- // $stream .= "\x0A";\r
- // $stream .= "\x0D";\r
- if (function_exists('gzuncompress')) {\r
- $oStream = $stream;\r
- $stream = (strlen($stream) > 0) ? @gzuncompress($stream) : '';\r
- } else {\r
- $this->error(sprintf('To handle %s filter, please compile php with zlib support.',$_filter[1]));\r
- }\r
- \r
- if ($stream === false) {\r
- $oStream = substr($oStream, 2);\r
- $stream = @gzinflate($oStream);\r
- if ($stream == false) {\r
- $this->error('Error while decompressing stream.');\r
- }\r
- }\r
- break;\r
- case '/LZWDecode':\r
- include_once('filters/FilterLZW_FPDI.php');\r
- $decoder = new FilterLZW_FPDI($this->fpdi);\r
- $stream = $decoder->decode($stream);\r
- break;\r
- case '/ASCII85Decode':\r
- include_once('filters/FilterASCII85_FPDI.php');\r
- $decoder = new FilterASCII85_FPDI($this->fpdi);\r
- $stream = $decoder->decode($stream);\r
- break;\r
- case null:\r
- $stream = $stream;\r
- break;\r
- default:\r
- $this->error(sprintf('Unsupported Filter: %s',$_filter[1]));\r
- }\r
- }\r
- \r
- return $stream;\r
- }\r
- \r
- \r
- /**\r
- * Get a Box from a page\r
- * Arrayformat is same as used by fpdf_tpl\r
+ * Array format is same as used by FPDF_TPL.\r
*\r
- * @param array $page a /Page\r
- * @param string $box_index Type of Box @see $availableBoxes\r
+ * @param array $page a /Page dictionary\r
+ * @param string $boxIndex Type of box {see {@link $availableBoxes})\r
* @param float Scale factor from user space units to points\r
- * @return array\r
+ *\r
+ * @return array|boolean\r
*/\r
- function getPageBox($page, $box_index, $k) {\r
- $page = $this->pdf_resolve_object($this->c, $page);\r
+ protected function _getPageBox($page, $boxIndex, $k)\r
+ {\r
+ $page = $this->resolveObject($page);\r
$box = null;\r
- if (isset($page[1][1][$box_index]))\r
- $box =& $page[1][1][$box_index];\r
+ if (isset($page[1][1][$boxIndex])) {\r
+ $box = $page[1][1][$boxIndex];\r
+ }\r
\r
- if (!is_null($box) && $box[0] == PDF_TYPE_OBJREF) {\r
- $tmp_box = $this->pdf_resolve_object($this->c, $box);\r
+ if (!is_null($box) && $box[0] == pdf_parser::TYPE_OBJREF) {\r
+ $tmp_box = $this->resolveObject($box);\r
$box = $tmp_box[1];\r
}\r
\r
- if (!is_null($box) && $box[0] == PDF_TYPE_ARRAY) {\r
- $b =& $box[1];\r
- return array('x' => $b[0][1] / $k,\r
- 'y' => $b[1][1] / $k,\r
- 'w' => abs($b[0][1] - $b[2][1]) / $k,\r
- 'h' => abs($b[1][1] - $b[3][1]) / $k,\r
- 'llx' => min($b[0][1], $b[2][1]) / $k,\r
- 'lly' => min($b[1][1], $b[3][1]) / $k,\r
- 'urx' => max($b[0][1], $b[2][1]) / $k,\r
- 'ury' => max($b[1][1], $b[3][1]) / $k,\r
- );\r
- } elseif (!isset ($page[1][1]['/Parent'])) {\r
+ if (!is_null($box) && $box[0] == pdf_parser::TYPE_ARRAY) {\r
+ $b = $box[1];\r
+ return array(\r
+ 'x' => $b[0][1] / $k,\r
+ 'y' => $b[1][1] / $k,\r
+ 'w' => abs($b[0][1] - $b[2][1]) / $k,\r
+ 'h' => abs($b[1][1] - $b[3][1]) / $k,\r
+ 'llx' => min($b[0][1], $b[2][1]) / $k,\r
+ 'lly' => min($b[1][1], $b[3][1]) / $k,\r
+ 'urx' => max($b[0][1], $b[2][1]) / $k,\r
+ 'ury' => max($b[1][1], $b[3][1]) / $k,\r
+ );\r
+ } else if (!isset($page[1][1]['/Parent'])) {\r
return false;\r
} else {\r
- return $this->getPageBox($this->pdf_resolve_object($this->c, $page[1][1]['/Parent']), $box_index, $k);\r
+ return $this->_getPageBox($this->resolveObject($page[1][1]['/Parent']), $boxIndex, $k);\r
}\r
}\r
\r
/**\r
- * Get all page boxes by page no\r
+ * Get all page boundary boxes by page number\r
* \r
- * @param int The page number\r
- * @param float Scale factor from user space units to points\r
+ * @param int $pageNo The page number\r
+ * @param float $k Scale factor from user space units to points\r
* @return array\r
+ * @throws InvalidArgumentException\r
*/\r
- function getPageBoxes($pageno, $k) {\r
- return $this->_getPageBoxes($this->pages[$pageno - 1], $k);\r
+ public function getPageBoxes($pageNo, $k)\r
+ {\r
+ if (!isset($this->_pages[$pageNo - 1])) {\r
+ throw new InvalidArgumentException('Page ' . $pageNo . ' does not exists.');\r
+ }\r
+\r
+ return $this->_getPageBoxes($this->_pages[$pageNo - 1], $k);\r
}\r
\r
/**\r
- * Get all boxes from /Page\r
+ * Get all boxes from /Page dictionary\r
*\r
- * @param array a /Page\r
+ * @param array $page A /Page dictionary\r
+ * @param float $k Scale factor from user space units to points\r
* @return array\r
*/\r
- function _getPageBoxes($page, $k) {\r
+ protected function _getPageBoxes($page, $k)\r
+ {\r
$boxes = array();\r
\r
foreach($this->availableBoxes AS $box) {\r
- if ($_box = $this->getPageBox($page, $box, $k)) {\r
+ if ($_box = $this->_getPageBox($page, $box, $k)) {\r
$boxes[$box] = $_box;\r
}\r
}\r
}\r
\r
/**\r
- * Get the page rotation by pageno\r
+ * Get the page rotation by page number\r
*\r
- * @param integer $pageno\r
+ * @param integer $pageNo\r
+ * @throws InvalidArgumentException\r
* @return array\r
*/\r
- function getPageRotation($pageno) {\r
- return $this->_getPageRotation($this->pages[$pageno - 1]);\r
+ public function getPageRotation($pageNo)\r
+ {\r
+ if (!isset($this->_pages[$pageNo - 1])) {\r
+ throw new InvalidArgumentException('Page ' . $pageNo . ' does not exists.');\r
+ }\r
+\r
+ return $this->_getPageRotation($this->_pages[$pageNo - 1]);\r
}\r
- \r
- function _getPageRotation($obj) { // $obj = /Page\r
- $obj = $this->pdf_resolve_object($this->c, $obj);\r
- if (isset ($obj[1][1]['/Rotate'])) {\r
- $res = $this->pdf_resolve_object($this->c, $obj[1][1]['/Rotate']);\r
- if ($res[0] == PDF_TYPE_OBJECT)\r
+\r
+ /**\r
+ * Get the rotation value of a page\r
+ *\r
+ * @param array $obj A /Page dictionary\r
+ * @return array|bool\r
+ */\r
+ protected function _getPageRotation($obj)\r
+ {\r
+ $obj = $this->resolveObject($obj);\r
+ if (isset($obj[1][1]['/Rotate'])) {\r
+ $res = $this->resolveObject($obj[1][1]['/Rotate']);\r
+ if ($res[0] == pdf_parser::TYPE_OBJECT)\r
return $res[1];\r
return $res;\r
- } else {\r
- if (!isset ($obj[1][1]['/Parent'])) {\r
- return false;\r
- } else {\r
- $res = $this->_getPageRotation($obj[1][1]['/Parent']);\r
- if ($res[0] == PDF_TYPE_OBJECT)\r
- return $res[1];\r
- return $res;\r
- }\r
}\r
+\r
+ if (!isset($obj[1][1]['/Parent'])) {\r
+ return false;\r
+ }\r
+\r
+ $res = $this->_getPageRotation($obj[1][1]['/Parent']);\r
+ if ($res[0] == pdf_parser::TYPE_OBJECT)\r
+ return $res[1];\r
+\r
+ return $res;\r
}\r
- \r
+\r
/**\r
- * Read all /Page(es)\r
+ * Read all pages\r
*\r
- * @param object pdf_context\r
- * @param array /Pages\r
- * @param array the result-array\r
+ * @param array $pages /Pages dictionary\r
+ * @param array $result The result array\r
+ * @throws Exception\r
*/\r
- function read_pages(&$c, &$pages, &$result) {\r
+ protected function _readPages(&$pages, &$result)\r
+ {\r
// Get the kids dictionary\r
- $_kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']);\r
- \r
- if (!is_array($_kids))\r
- $this->error('Cannot find /Kids in current /Page-Dictionary');\r
- \r
- if ($_kids[1][0] == PDF_TYPE_ARRAY) {\r
- $kids = $_kids[1][1];\r
- } else {\r
- $kids = $_kids[1];\r
+ $_kids = $this->resolveObject($pages[1][1]['/Kids']);\r
+\r
+ if (!is_array($_kids)) {\r
+ throw new Exception('Cannot find /Kids in current /Page-Dictionary');\r
}\r
- \r
+\r
+ if ($_kids[0] === self::TYPE_OBJECT) {\r
+ $_kids = $_kids[1];\r
+ }\r
+\r
+ $kids = $_kids[1];\r
+\r
foreach ($kids as $v) {\r
- $pg = $this->pdf_resolve_object ($c, $v);\r
+ $pg = $this->resolveObject($v);\r
if ($pg[1][1]['/Type'][1] === '/Pages') {\r
// If one of the kids is an embedded\r
// /Pages array, resolve it as well.\r
- if ($pg !== $pages) {\r
- $this->read_pages($c, $pg, $result);\r
- }\r
+ $this->_readPages($pg, $result);\r
} else {\r
$result[] = $pg;\r
}\r
}\r
}\r
-\r
- \r
- \r
- /**\r
- * Get PDF-Version\r
- *\r
- * And reset the PDF Version used in FPDI if needed\r
- */\r
- function getPDFVersion() {\r
- parent::getPDFVersion();\r
- $this->fpdi->setPDFVersion(max($this->fpdi->getPDFVersion(), $this->pdfVersion));\r
- }\r
-}\r
+}
\ No newline at end of file
<?php\r
//\r
-// FPDI - Version 1.4.4\r
+// FPDI - Version 1.5.4\r
//\r
-// Copyright 2004-2013 Setasign - Jan Slabon\r
+// Copyright 2004-2015 Setasign - Jan Slabon\r
//\r
// Licensed under the Apache License, Version 2.0 (the "License");\r
// you may not use this file except in compliance with the License.\r
// limitations under the License.\r
//\r
\r
-if (!class_exists('pdf_context', false)) {\r
- \r
- class pdf_context {\r
- \r
- /**\r
- * Modi\r
- *\r
- * @var integer 0 = file | 1 = string\r
- */\r
- var $_mode = 0;\r
- \r
- var $file;\r
- var $buffer;\r
- var $offset;\r
- var $length;\r
- \r
- var $stack;\r
- \r
- // Constructor\r
- \r
- function pdf_context(&$f) {\r
- $this->file =& $f;\r
- if (is_string($this->file))\r
- $this->_mode = 1;\r
- $this->reset();\r
- }\r
- \r
- // Optionally move the file\r
- // pointer to a new location\r
- // and reset the buffered data\r
- \r
- function reset($pos = null, $l = 100) {\r
- if ($this->_mode == 0) {\r
- if (!is_null ($pos)) {\r
- fseek ($this->file, $pos);\r
- }\r
- \r
- $this->buffer = $l > 0 ? fread($this->file, $l) : '';\r
- $this->length = strlen($this->buffer);\r
- if ($this->length < $l)\r
- $this->increase_length($l - $this->length);\r
- } else {\r
- $this->buffer = $this->file;\r
- $this->length = strlen($this->buffer);\r
- }\r
- $this->offset = 0;\r
- $this->stack = array();\r
- }\r
- \r
- // Make sure that there is at least one\r
- // character beyond the current offset in\r
- // the buffer to prevent the tokenizer\r
- // from attempting to access data that does\r
- // not exist\r
- \r
- function ensure_content() {\r
- if ($this->offset >= $this->length - 1) {\r
- return $this->increase_length();\r
- } else {\r
- return true;\r
- }\r
- }\r
- \r
- // Forcefully read more data into the buffer\r
- \r
- function increase_length($l = 100) {\r
- if ($this->_mode == 0 && feof($this->file)) {\r
- return false;\r
- } elseif ($this->_mode == 0) {\r
- $totalLength = $this->length + $l;\r
- do {\r
- $toRead = $totalLength - $this->length;\r
- if ($toRead < 1)\r
- break;\r
- \r
- $this->buffer .= fread($this->file, $toRead);\r
- } while ((($this->length = strlen($this->buffer)) != $totalLength) && !feof($this->file));\r
- \r
- return true;\r
- } else {\r
- return false;\r
- }\r
- }\r
+/**\r
+ * Class pdf_context\r
+ */\r
+class pdf_context\r
+{\r
+ /**\r
+ * Mode\r
+ *\r
+ * @var integer 0 = file | 1 = string\r
+ */\r
+ protected $_mode = 0;\r
+\r
+ /**\r
+ * @var resource|string\r
+ */\r
+ public $file;\r
+\r
+ /**\r
+ * @var string\r
+ */\r
+ public $buffer;\r
+\r
+ /**\r
+ * @var integer\r
+ */\r
+ public $offset;\r
+\r
+ /**\r
+ * @var integer\r
+ */\r
+ public $length;\r
+\r
+ /**\r
+ * @var array\r
+ */\r
+ public $stack;\r
+\r
+ /**\r
+ * The constructor\r
+ *\r
+ * @param resource $f\r
+ */\r
+ public function __construct(&$f)\r
+ {\r
+ $this->file =& $f;\r
+ if (is_string($this->file))\r
+ $this->_mode = 1;\r
+\r
+ $this->reset();\r
+ }\r
+\r
+ /**\r
+ * Get the position in the file stream\r
+ *\r
+ * @return int\r
+ */\r
+ public function getPos()\r
+ {\r
+ if ($this->_mode == 0) {\r
+ return ftell($this->file);\r
+ } else {\r
+ return 0;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Reset the position in the file stream.\r
+ *\r
+ * Optionally move the file pointer to a new location and reset the buffered data.\r
+ *\r
+ * @param null $pos\r
+ * @param int $l\r
+ */\r
+ public function reset($pos = null, $l = 100)\r
+ {\r
+ if ($this->_mode == 0) {\r
+ if (!is_null($pos)) {\r
+ fseek ($this->file, $pos);\r
+ }\r
+\r
+ $this->buffer = $l > 0 ? fread($this->file, $l) : '';\r
+ $this->length = strlen($this->buffer);\r
+ if ($this->length < $l)\r
+ $this->increaseLength($l - $this->length);\r
+ } else {\r
+ $this->buffer = $this->file;\r
+ $this->length = strlen($this->buffer);\r
+ }\r
+ $this->offset = 0;\r
+ $this->stack = array();\r
+ }\r
+\r
+ /**\r
+ * Make sure that there is at least one character beyond the current offset in the buffer.\r
+ *\r
+ * To prevent the tokenizer from attempting to access data that does not exist.\r
+ *\r
+ * @return bool\r
+ */\r
+ public function ensureContent()\r
+ {\r
+ if ($this->offset >= $this->length - 1) {\r
+ return $this->increaseLength();\r
+ } else {\r
+ return true;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Forcefully read more data into the buffer\r
+ *\r
+ * @param int $l\r
+ * @return bool\r
+ */\r
+ public function increaseLength($l = 100)\r
+ {\r
+ if ($this->_mode == 0 && feof($this->file)) {\r
+ return false;\r
+ } else if ($this->_mode == 0) {\r
+ $totalLength = $this->length + $l;\r
+ do {\r
+ $toRead = $totalLength - $this->length;\r
+ if ($toRead < 1)\r
+ break;\r
+\r
+ $this->buffer .= fread($this->file, $toRead);\r
+ } while ((($this->length = strlen($this->buffer)) != $totalLength) && !feof($this->file));\r
+\r
+ return true;\r
+ } else {\r
+ return false;\r
+ }\r
}\r
}
\ No newline at end of file
<?php\r
//\r
-// FPDI - Version 1.4.4\r
+// FPDI - Version 1.5.4\r
//\r
-// Copyright 2004-2013 Setasign - Jan Slabon\r
+// Copyright 2004-2015 Setasign - Jan Slabon\r
//\r
// Licensed under the Apache License, Version 2.0 (the "License");\r
// you may not use this file except in compliance with the License.\r
// limitations under the License.\r
//\r
\r
-if (!defined ('PDF_TYPE_NULL'))\r
- define ('PDF_TYPE_NULL', 0);\r
-if (!defined ('PDF_TYPE_NUMERIC'))\r
- define ('PDF_TYPE_NUMERIC', 1);\r
-if (!defined ('PDF_TYPE_TOKEN'))\r
- define ('PDF_TYPE_TOKEN', 2);\r
-if (!defined ('PDF_TYPE_HEX'))\r
- define ('PDF_TYPE_HEX', 3);\r
-if (!defined ('PDF_TYPE_STRING'))\r
- define ('PDF_TYPE_STRING', 4);\r
-if (!defined ('PDF_TYPE_DICTIONARY'))\r
- define ('PDF_TYPE_DICTIONARY', 5);\r
-if (!defined ('PDF_TYPE_ARRAY'))\r
- define ('PDF_TYPE_ARRAY', 6);\r
-if (!defined ('PDF_TYPE_OBJDEC'))\r
- define ('PDF_TYPE_OBJDEC', 7);\r
-if (!defined ('PDF_TYPE_OBJREF'))\r
- define ('PDF_TYPE_OBJREF', 8);\r
-if (!defined ('PDF_TYPE_OBJECT'))\r
- define ('PDF_TYPE_OBJECT', 9);\r
-if (!defined ('PDF_TYPE_STREAM'))\r
- define ('PDF_TYPE_STREAM', 10);\r
-if (!defined ('PDF_TYPE_BOOLEAN'))\r
- define ('PDF_TYPE_BOOLEAN', 11);\r
-if (!defined ('PDF_TYPE_REAL'))\r
- define ('PDF_TYPE_REAL', 12);\r
- \r
-require_once('pdf_context.php');\r
-\r
-if (!class_exists('pdf_parser', false)) {\r
- \r
- class pdf_parser {\r
- \r
- /**\r
- * Filename\r
- * @var string\r
- */\r
- var $filename;\r
- \r
- /**\r
- * File resource\r
- * @var resource\r
- */\r
- var $f;\r
- \r
- /**\r
- * PDF Context\r
- * @var object pdf_context-Instance\r
- */\r
- var $c;\r
- \r
- /**\r
- * xref-Data\r
- * @var array\r
- */\r
- var $xref;\r
- \r
- /**\r
- * root-Object\r
- * @var array\r
- */\r
- var $root;\r
- \r
- /**\r
- * PDF version of the loaded document\r
- * @var string\r
- */\r
- var $pdfVersion;\r
- \r
- /**\r
- * For reading encrypted documents and xref/objectstreams are in use\r
- *\r
- * @var boolean\r
- */\r
- var $readPlain = true;\r
- \r
- /**\r
- * Constructor\r
- *\r
- * @param string $filename Source-Filename\r
- */\r
- function pdf_parser($filename) {\r
- $this->filename = $filename;\r
- \r
- $this->f = @fopen($this->filename, 'rb');\r
- \r
- if (!$this->f)\r
- $this->error(sprintf('Cannot open %s !', $filename));\r
- \r
- $this->getPDFVersion();\r
- \r
- $this->c = new pdf_context($this->f);\r
- \r
- // Read xref-Data\r
- $this->xref = array();\r
- $this->pdf_read_xref($this->xref, $this->pdf_find_xref());\r
- \r
- // Check for Encryption\r
- $this->getEncryption();\r
- \r
- // Read root\r
- $this->pdf_read_root();\r
+/**\r
+ * Class pdf_parser\r
+ */\r
+class pdf_parser\r
+{\r
+ /**\r
+ * Type constant\r
+ *\r
+ * @var integer\r
+ */\r
+ const TYPE_NULL = 0;\r
+\r
+ /**\r
+ * Type constant\r
+ *\r
+ * @var integer\r
+ */\r
+ const TYPE_NUMERIC = 1;\r
+\r
+ /**\r
+ * Type constant\r
+ *\r
+ * @var integer\r
+ */\r
+ const TYPE_TOKEN = 2;\r
+\r
+ /**\r
+ * Type constant\r
+ *\r
+ * @var integer\r
+ */\r
+ const TYPE_HEX = 3;\r
+\r
+ /**\r
+ * Type constant\r
+ *\r
+ * @var integer\r
+ */\r
+ const TYPE_STRING = 4;\r
+\r
+ /**\r
+ * Type constant\r
+ *\r
+ * @var integer\r
+ */\r
+ const TYPE_DICTIONARY = 5;\r
+\r
+ /**\r
+ * Type constant\r
+ *\r
+ * @var integer\r
+ */\r
+ const TYPE_ARRAY = 6;\r
+\r
+ /**\r
+ * Type constant\r
+ *\r
+ * @var integer\r
+ */\r
+ const TYPE_OBJDEC = 7;\r
+\r
+ /**\r
+ * Type constant\r
+ *\r
+ * @var integer\r
+ */\r
+ const TYPE_OBJREF = 8;\r
+\r
+ /**\r
+ * Type constant\r
+ *\r
+ * @var integer\r
+ */\r
+ const TYPE_OBJECT = 9;\r
+\r
+ /**\r
+ * Type constant\r
+ *\r
+ * @var integer\r
+ */\r
+ const TYPE_STREAM = 10;\r
+\r
+ /**\r
+ * Type constant\r
+ *\r
+ * @var integer\r
+ */\r
+ const TYPE_BOOLEAN = 11;\r
+\r
+ /**\r
+ * Type constant\r
+ *\r
+ * @var integer\r
+ */\r
+ const TYPE_REAL = 12;\r
+\r
+ /**\r
+ * Define the amount of byte in which the initial keyword of a PDF document should be searched.\r
+ *\r
+ * @var int\r
+ */\r
+ static public $searchForStartxrefLength = 5500;\r
+\r
+ /**\r
+ * Filename\r
+ *\r
+ * @var string\r
+ */\r
+ public $filename;\r
+\r
+ /**\r
+ * File resource\r
+ *\r
+ * @var resource\r
+ */\r
+ protected $_f;\r
+\r
+ /**\r
+ * PDF Context\r
+ *\r
+ * @var pdf_context\r
+ */\r
+ protected $_c;\r
+\r
+ /**\r
+ * xref-Data\r
+ *\r
+ * @var array\r
+ */\r
+ protected $_xref;\r
+\r
+ /**\r
+ * Data of the Root object\r
+ *\r
+ * @var array\r
+ */\r
+ protected $_root;\r
+\r
+ /**\r
+ * PDF version of the loaded document\r
+ *\r
+ * @var string\r
+ */\r
+ protected $_pdfVersion;\r
+\r
+ /**\r
+ * For reading encrypted documents and xref/object streams are in use\r
+ *\r
+ * @var boolean\r
+ */\r
+ protected $_readPlain = true;\r
+\r
+ /**\r
+ * The current read object\r
+ *\r
+ * @var array\r
+ */\r
+ protected $_currentObj;\r
+\r
+ /**\r
+ * Constructor\r
+ *\r
+ * @param string $filename Source filename\r
+ * @throws InvalidArgumentException\r
+ */\r
+ public function __construct($filename)\r
+ {\r
+ $this->filename = $filename;\r
+\r
+ $this->_f = @fopen($this->filename, 'rb');\r
+\r
+ if (!$this->_f) {\r
+ throw new InvalidArgumentException(sprintf('Cannot open %s !', $filename));\r
}\r
- \r
- /**\r
- * Close the opened file\r
- */\r
- function closeFile() {\r
- if (isset($this->f) && is_resource($this->f)) {\r
- fclose($this->f); \r
- unset($this->f);\r
- } \r
+\r
+ $this->getPdfVersion();\r
+\r
+ if (!class_exists('pdf_context')) {\r
+ require_once('pdf_context.php');\r
}\r
- \r
- /**\r
- * Print Error and die\r
- *\r
- * @param string $msg Error-Message\r
- */\r
- function error($msg) {\r
- die('<b>PDF-Parser Error:</b> ' . $msg); \r
+ $this->_c = new pdf_context($this->_f);\r
+\r
+ // Read xref-Data\r
+ $this->_xref = array();\r
+ $this->_readXref($this->_xref, $this->_findXref());\r
+\r
+ // Check for Encryption\r
+ $this->getEncryption();\r
+\r
+ // Read root\r
+ $this->_readRoot();\r
+ }\r
+\r
+ /**\r
+ * Destructor\r
+ */\r
+ public function __destruct()\r
+ {\r
+ $this->closeFile();\r
+ }\r
+\r
+ /**\r
+ * Close the opened file\r
+ */\r
+ public function closeFile()\r
+ {\r
+ if (isset($this->_f) && is_resource($this->_f)) {\r
+ fclose($this->_f);\r
+ unset($this->_f);\r
}\r
- \r
- /**\r
- * Check Trailer for Encryption\r
- */\r
- function getEncryption() {\r
- if (isset($this->xref['trailer'][1]['/Encrypt'])) {\r
- $this->error('File is encrypted!');\r
- }\r
+ }\r
+\r
+ /**\r
+ * Check Trailer for Encryption\r
+ *\r
+ * @throws Exception\r
+ */\r
+ public function getEncryption()\r
+ {\r
+ if (isset($this->_xref['trailer'][1]['/Encrypt'])) {\r
+ throw new Exception('File is encrypted!');\r
}\r
- \r
- /**\r
- * Find/Return /Root\r
- *\r
- * @return array\r
- */\r
- function pdf_find_root() {\r
- if ($this->xref['trailer'][1]['/Root'][0] != PDF_TYPE_OBJREF) {\r
- $this->error('Wrong Type of Root-Element! Must be an indirect reference');\r
- }\r
- \r
- return $this->xref['trailer'][1]['/Root'];\r
+ }\r
+\r
+ /**\r
+ * Get PDF-Version\r
+ *\r
+ * @return string\r
+ */\r
+ public function getPdfVersion()\r
+ {\r
+ if ($this->_pdfVersion === null) {\r
+ fseek($this->_f, 0);\r
+ preg_match('/\d\.\d/', fread($this->_f, 16), $m);\r
+ if (isset($m[0]))\r
+ $this->_pdfVersion = $m[0];\r
}\r
- \r
- /**\r
- * Read the /Root\r
- */\r
- function pdf_read_root() {\r
- // read root\r
- $this->root = $this->pdf_resolve_object($this->c, $this->pdf_find_root());\r
+\r
+ return $this->_pdfVersion;\r
+ }\r
+\r
+ /**\r
+ * Read the /Root dictionary\r
+ */\r
+ protected function _readRoot()\r
+ {\r
+ if ($this->_xref['trailer'][1]['/Root'][0] != self::TYPE_OBJREF) {\r
+ throw new Exception('Wrong Type of Root-Element! Must be an indirect reference');\r
}\r
- \r
- /**\r
- * Get PDF-Version\r
- *\r
- * And reset the PDF Version used in FPDI if needed\r
- */\r
- function getPDFVersion() {\r
- fseek($this->f, 0);\r
- preg_match('/\d\.\d/',fread($this->f, 16), $m);\r
- if (isset($m[0]))\r
- $this->pdfVersion = $m[0];\r
- return $this->pdfVersion;\r
+\r
+ $this->_root = $this->resolveObject($this->_xref['trailer'][1]['/Root']);\r
+ }\r
+\r
+ /**\r
+ * Find the xref table\r
+ *\r
+ * @return integer\r
+ * @throws Exception\r
+ */\r
+ protected function _findXref()\r
+ {\r
+ $toRead = self::$searchForStartxrefLength;\r
+\r
+ $stat = fseek($this->_f, -$toRead, SEEK_END);\r
+ if ($stat === -1) {\r
+ fseek($this->_f, 0);\r
}\r
- \r
- /**\r
- * Find the xref-Table\r
- */\r
- function pdf_find_xref() {\r
- $toRead = 1500;\r
- \r
- $stat = fseek ($this->f, -$toRead, SEEK_END);\r
- if ($stat === -1) {\r
- fseek ($this->f, 0);\r
+\r
+ $data = fread($this->_f, $toRead);\r
+\r
+ $keywordPos = strpos(strrev($data), strrev('startxref'));\r
+ if (false === $keywordPos) {\r
+ $keywordPos = strpos(strrev($data), strrev('startref'));\r
+ }\r
+\r
+ if (false === $keywordPos) {\r
+ throw new Exception('Unable to find "startxref" keyword.');\r
+ }\r
+\r
+ $pos = strlen($data) - $keywordPos;\r
+ $data = substr($data, $pos);\r
+\r
+ if (!preg_match('/\s*(\d+).*$/s', $data, $matches)) {\r
+ throw new Exception('Unable to find pointer to xref table.');\r
+ }\r
+\r
+ return (int) $matches[1];\r
+ }\r
+\r
+ /**\r
+ * Read the xref table\r
+ *\r
+ * @param array $result Array of xref table entries\r
+ * @param integer $offset of xref table\r
+ * @return boolean\r
+ * @throws Exception\r
+ */\r
+ protected function _readXref(&$result, $offset)\r
+ {\r
+ $tempPos = $offset - min(20, $offset);\r
+ fseek($this->_f, $tempPos); // set some bytes backwards to fetch corrupted docs\r
+\r
+ $data = fread($this->_f, 100);\r
+\r
+ $xrefPos = strrpos($data, 'xref');\r
+\r
+ if ($xrefPos === false) {\r
+ $this->_c->reset($offset);\r
+ $xrefStreamObjDec = $this->_readValue($this->_c);\r
+\r
+ if (is_array($xrefStreamObjDec) && isset($xrefStreamObjDec[0]) && $xrefStreamObjDec[0] == self::TYPE_OBJDEC) {\r
+ throw new Exception(\r
+ sprintf(\r
+ 'This document (%s) probably uses a compression technique which is not supported by the ' .\r
+ 'free parser shipped with FPDI. (See https://www.setasign.com/fpdi-pdf-parser for more details)',\r
+ $this->filename\r
+ )\r
+ );\r
+ } else {\r
+ throw new Exception('Unable to find xref table.');\r
}\r
- $data = fread($this->f, $toRead);\r
- \r
- $pos = strlen($data) - strpos(strrev($data), strrev('startxref')); \r
- $data = substr($data, $pos);\r
- \r
- if (!preg_match('/\s*(\d+).*$/s', $data, $matches)) {\r
- $this->error('Unable to find pointer to xref table');\r
- }\r
- \r
- return (int) $matches[1];\r
}\r
- \r
- /**\r
- * Read xref-table\r
- *\r
- * @param array $result Array of xref-table\r
- * @param integer $offset of xref-table\r
- */\r
- function pdf_read_xref(&$result, $offset) {\r
- $o_pos = $offset-min(20, $offset);\r
- fseek($this->f, $o_pos); // set some bytes backwards to fetch errorious docs\r
- \r
- $data = fread($this->f, 100);\r
- \r
- $xrefPos = strrpos($data, 'xref');\r
- \r
- if ($xrefPos === false) {\r
- fseek($this->f, $offset);\r
- $c = new pdf_context($this->f);\r
- $xrefStreamObjDec = $this->pdf_read_value($c);\r
- \r
- if (is_array($xrefStreamObjDec) && isset($xrefStreamObjDec[0]) && $xrefStreamObjDec[0] == PDF_TYPE_OBJDEC) {\r
- $this->error(sprintf('This document (%s) probably uses a compression technique which is not supported by the free parser shipped with FPDI.', $this->filename));\r
- } else { \r
- $this->error('Unable to find xref table.');\r
+\r
+ if (!isset($result['xrefLocation'])) {\r
+ $result['xrefLocation'] = $tempPos + $xrefPos;\r
+ $result['maxObject'] = 0;\r
+ }\r
+\r
+ $cycles = -1;\r
+ $bytesPerCycle = 100;\r
+\r
+ fseek($this->_f, $tempPos = $tempPos + $xrefPos + 4); // set the handle directly after the "xref"-keyword\r
+ $data = fread($this->_f, $bytesPerCycle);\r
+\r
+ while (($trailerPos = strpos($data, 'trailer', max($bytesPerCycle * $cycles++, 0))) === false && !feof($this->_f)) {\r
+ $data .= fread($this->_f, $bytesPerCycle);\r
+ }\r
+\r
+ if ($trailerPos === false) {\r
+ throw new Exception('Trailer keyword not found after xref table');\r
+ }\r
+\r
+ $data = ltrim(substr($data, 0, $trailerPos));\r
+\r
+ // get Line-Ending\r
+ $found = preg_match_all("/(\r\n|\n|\r)/", substr($data, 0, 100), $m); // check the first 100 bytes for line breaks\r
+ if ($found === 0) {\r
+ throw new Exception('Xref table seems to be corrupted.');\r
+ }\r
+ $differentLineEndings = count(array_unique($m[0]));\r
+ if ($differentLineEndings > 1) {\r
+ $lines = preg_split("/(\r\n|\n|\r)/", $data, -1, PREG_SPLIT_NO_EMPTY);\r
+ } else {\r
+ $lines = explode($m[0][0], $data);\r
+ }\r
+\r
+ $data = $differentLineEndings = $m = null;\r
+ unset($data, $differentLineEndings, $m);\r
+\r
+ $linesCount = count($lines);\r
+\r
+ $start = 1;\r
+\r
+ for ($i = 0; $i < $linesCount; $i++) {\r
+ $line = trim($lines[$i]);\r
+ if ($line) {\r
+ $pieces = explode(' ', $line);\r
+ $c = count($pieces);\r
+ switch($c) {\r
+ case 2:\r
+ $start = (int)$pieces[0];\r
+ $end = $start + (int)$pieces[1];\r
+ if ($end > $result['maxObject'])\r
+ $result['maxObject'] = $end;\r
+ break;\r
+ case 3:\r
+ if (!isset($result['xref'][$start]))\r
+ $result['xref'][$start] = array();\r
+\r
+ if (!array_key_exists($gen = (int) $pieces[1], $result['xref'][$start])) {\r
+ $result['xref'][$start][$gen] = $pieces[2] == 'n' ? (int) $pieces[0] : null;\r
+ }\r
+ $start++;\r
+ break;\r
+ default:\r
+ throw new Exception('Unexpected data in xref table');\r
}\r
}\r
- \r
- if (!isset($result['xref_location'])) {\r
- $result['xref_location'] = $o_pos + $xrefPos;\r
- $result['max_object'] = 0;\r
- }\r
- \r
- $cylces = -1;\r
- $bytesPerCycle = 100;\r
- \r
- fseek($this->f, $o_pos = $o_pos + $xrefPos + 4); // set the handle directly after the "xref"-keyword\r
- $data = fread($this->f, $bytesPerCycle);\r
- \r
- while (($trailerPos = strpos($data, 'trailer', max($bytesPerCycle * $cylces++, 0))) === false && !feof($this->f)) {\r
- $data .= fread($this->f, $bytesPerCycle);\r
- }\r
- \r
- if ($trailerPos === false) {\r
- $this->error('Trailer keyword not found after xref table');\r
- }\r
- \r
- $data = substr($data, 0, $trailerPos);\r
- \r
- // get Line-Ending\r
- preg_match_all("/(\r\n|\n|\r)/", substr($data, 0, 100), $m); // check the first 100 bytes for linebreaks\r
- \r
- $differentLineEndings = count(array_unique($m[0]));\r
- if ($differentLineEndings > 1) {\r
- $lines = preg_split("/(\r\n|\n|\r)/", $data, -1, PREG_SPLIT_NO_EMPTY);\r
- } else {\r
- $lines = explode($m[0][1], $data);\r
- }\r
- \r
- $data = $differentLineEndings = $m = null;\r
- unset($data, $differentLineEndings, $m);\r
- \r
- $linesCount = count($lines);\r
- \r
- $start = 1;\r
- \r
- for ($i = 0; $i < $linesCount; $i++) {\r
- $line = trim($lines[$i]);\r
- if ($line) {\r
- $pieces = explode(' ', $line);\r
- $c = count($pieces);\r
- switch($c) {\r
- case 2:\r
- $start = (int)$pieces[0];\r
- $end = $start + (int)$pieces[1];\r
- if ($end > $result['max_object'])\r
- $result['max_object'] = $end;\r
- break;\r
- case 3:\r
- if (!isset($result['xref'][$start]))\r
- $result['xref'][$start] = array();\r
- \r
- if (!array_key_exists($gen = (int) $pieces[1], $result['xref'][$start])) {\r
- $result['xref'][$start][$gen] = $pieces[2] == 'n' ? (int) $pieces[0] : null;\r
- }\r
- $start++;\r
- break;\r
- default:\r
- $this->error('Unexpected data in xref table');\r
+ }\r
+\r
+ $lines = $pieces = $line = $start = $end = $gen = null;\r
+ unset($lines, $pieces, $line, $start, $end, $gen);\r
+\r
+ $this->_c->reset($tempPos + $trailerPos + 7);\r
+ $trailer = $this->_readValue($this->_c);\r
+\r
+ if (!isset($result['trailer'])) {\r
+ $result['trailer'] = $trailer;\r
+ }\r
+\r
+ if (isset($trailer[1]['/Prev'])) {\r
+ $this->_readXref($result, $trailer[1]['/Prev'][1]);\r
+ }\r
+\r
+ $trailer = null;\r
+ unset($trailer);\r
+\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * Reads a PDF value\r
+ *\r
+ * @param pdf_context $c\r
+ * @param string $token A token\r
+ * @return mixed\r
+ * @throws Exception\r
+ */\r
+ protected function _readValue(&$c, $token = null)\r
+ {\r
+ if (is_null($token)) {\r
+ $token = $this->_readToken($c);\r
+ }\r
+\r
+ if ($token === false) {\r
+ return false;\r
+ }\r
+\r
+ switch ($token) {\r
+ case '<':\r
+ // This is a hex string.\r
+ // Read the value, then the terminator\r
+\r
+ $pos = $c->offset;\r
+\r
+ while(1) {\r
+\r
+ $match = strpos($c->buffer, '>', $pos);\r
+\r
+ // If you can't find it, try\r
+ // reading more data from the stream\r
+\r
+ if ($match === false) {\r
+ if (!$c->increaseLength()) {\r
+ return false;\r
+ } else {\r
+ continue;\r
+ }\r
}\r
+\r
+ $result = substr($c->buffer, $c->offset, $match - $c->offset);\r
+ $c->offset = $match + 1;\r
+\r
+ return array (self::TYPE_HEX, $result);\r
}\r
+ break;\r
+\r
+ case '<<':\r
+ // This is a dictionary.\r
+\r
+ $result = array();\r
+\r
+ // Recurse into this function until we reach\r
+ // the end of the dictionary.\r
+ while (($key = $this->_readToken($c)) !== '>>') {\r
+ if ($key === false) {\r
+ return false;\r
+ }\r
+\r
+ if (($value = $this->_readValue($c)) === false) {\r
+ return false;\r
+ }\r
+\r
+ // Catch missing value\r
+ if ($value[0] == self::TYPE_TOKEN && $value[1] == '>>') {\r
+ $result[$key] = array(self::TYPE_NULL);\r
+ break;\r
+ }\r
+\r
+ $result[$key] = $value;\r
+ }\r
+\r
+ return array (self::TYPE_DICTIONARY, $result);\r
+\r
+ case '[':\r
+ // This is an array.\r
+\r
+ $result = array();\r
+\r
+ // Recurse into this function until we reach\r
+ // the end of the array.\r
+ while (($token = $this->_readToken($c)) !== ']') {\r
+ if ($token === false) {\r
+ return false;\r
+ }\r
+\r
+ if (($value = $this->_readValue($c, $token)) === false) {\r
+ return false;\r
+ }\r
+\r
+ $result[] = $value;\r
+ }\r
+\r
+ return array (self::TYPE_ARRAY, $result);\r
+\r
+ case '(':\r
+ // This is a string\r
+ $pos = $c->offset;\r
+\r
+ $openBrackets = 1;\r
+ do {\r
+ for (; $openBrackets != 0 && $pos < $c->length; $pos++) {\r
+ switch (ord($c->buffer[$pos])) {\r
+ case 0x28: // '('\r
+ $openBrackets++;\r
+ break;\r
+ case 0x29: // ')'\r
+ $openBrackets--;\r
+ break;\r
+ case 0x5C: // backslash\r
+ $pos++;\r
+ }\r
+ }\r
+ } while($openBrackets != 0 && $c->increaseLength());\r
+\r
+ $result = substr($c->buffer, $c->offset, $pos - $c->offset - 1);\r
+ $c->offset = $pos;\r
+\r
+ return array (self::TYPE_STRING, $result);\r
+\r
+ case 'stream':\r
+ $tempPos = $c->getPos() - strlen($c->buffer);\r
+ $tempOffset = $c->offset;\r
+\r
+ $c->reset($startPos = $tempPos + $tempOffset);\r
+\r
+ // Find the first "newline"\r
+ while ($c->buffer[0] !== chr(10) && $c->buffer[0] !== chr(13)) {\r
+ $c->reset(++$startPos);\r
+ if ($c->ensureContent() === false) {\r
+ throw new Exception(\r
+ 'Unable to parse stream data. No newline followed the stream keyword.'\r
+ );\r
+ }\r
+ }\r
+\r
+ $e = 0; // ensure line breaks in front of the stream\r
+ if ($c->buffer[0] == chr(10) || $c->buffer[0] == chr(13))\r
+ $e++;\r
+ if ($c->buffer[1] == chr(10) && $c->buffer[0] != chr(10))\r
+ $e++;\r
+\r
+ if ($this->_currentObj[1][1]['/Length'][0] == self::TYPE_OBJREF) {\r
+ $tmpLength = $this->resolveObject($this->_currentObj[1][1]['/Length']);\r
+ $length = $tmpLength[1][1];\r
+ } else {\r
+ $length = $this->_currentObj[1][1]['/Length'][1];\r
+ }\r
+\r
+ if ($length > 0) {\r
+ $c->reset($startPos + $e, $length);\r
+ $v = $c->buffer;\r
+ } else {\r
+ $v = '';\r
+ }\r
+\r
+ $c->reset($startPos + $e + $length);\r
+ $endstream = $this->_readToken($c);\r
+\r
+ if ($endstream != 'endstream') {\r
+ $c->reset($startPos + $e + $length + 9); // 9 = strlen("endstream")\r
+ // We don't throw an error here because the next\r
+ // round trip will start at a new offset\r
+ }\r
+\r
+ return array(self::TYPE_STREAM, $v);\r
+\r
+ default :\r
+ if (is_numeric($token)) {\r
+ // A numeric token. Make sure that\r
+ // it is not part of something else.\r
+ if (($tok2 = $this->_readToken($c)) !== false) {\r
+ if (is_numeric($tok2)) {\r
+\r
+ // Two numeric tokens in a row.\r
+ // In this case, we're probably in\r
+ // front of either an object reference\r
+ // or an object specification.\r
+ // Determine the case and return the data\r
+ if (($tok3 = $this->_readToken($c)) !== false) {\r
+ switch ($tok3) {\r
+ case 'obj':\r
+ return array(self::TYPE_OBJDEC, (int)$token, (int)$tok2);\r
+ case 'R':\r
+ return array(self::TYPE_OBJREF, (int)$token, (int)$tok2);\r
+ }\r
+ // If we get to this point, that numeric value up\r
+ // there was just a numeric value. Push the extra\r
+ // tokens back into the stack and return the value.\r
+ array_push($c->stack, $tok3);\r
+ }\r
+ }\r
+\r
+ array_push($c->stack, $tok2);\r
+ }\r
+\r
+ if ($token === (string)((int)$token))\r
+ return array(self::TYPE_NUMERIC, (int)$token);\r
+ else\r
+ return array(self::TYPE_REAL, (float)$token);\r
+ } else if ($token == 'true' || $token == 'false') {\r
+ return array(self::TYPE_BOOLEAN, $token == 'true');\r
+ } else if ($token == 'null') {\r
+ return array(self::TYPE_NULL);\r
+ } else {\r
+ // Just a token. Return it.\r
+ return array(self::TYPE_TOKEN, $token);\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Resolve an object\r
+ *\r
+ * @param array $objSpec The object-data\r
+ * @return array|boolean\r
+ * @throws Exception\r
+ */\r
+ public function resolveObject($objSpec)\r
+ {\r
+ $c = $this->_c;\r
+\r
+ // Exit if we get invalid data\r
+ if (!is_array($objSpec)) {\r
+ return false;\r
+ }\r
+\r
+ if ($objSpec[0] == self::TYPE_OBJREF) {\r
+\r
+ // This is a reference, resolve it\r
+ if (isset($this->_xref['xref'][$objSpec[1]][$objSpec[2]])) {\r
+\r
+ // Save current file position\r
+ // This is needed if you want to resolve\r
+ // references while you're reading another object\r
+ // (e.g.: if you need to determine the length\r
+ // of a stream)\r
+\r
+ $oldPos = $c->getPos();\r
+\r
+ // Reposition the file pointer and\r
+ // load the object header.\r
+\r
+ $c->reset($this->_xref['xref'][$objSpec[1]][$objSpec[2]]);\r
+\r
+ $header = $this->_readValue($c);\r
+\r
+ if ($header[0] != self::TYPE_OBJDEC || $header[1] != $objSpec[1] || $header[2] != $objSpec[2]) {\r
+ $toSearchFor = $objSpec[1] . ' ' . $objSpec[2] . ' obj';\r
+ if (preg_match('/' . $toSearchFor . '/', $c->buffer)) {\r
+ $c->offset = strpos($c->buffer, $toSearchFor) + strlen($toSearchFor);\r
+ // reset stack\r
+ $c->stack = array();\r
+ } else {\r
+ throw new Exception(\r
+ sprintf("Unable to find object (%s, %s) at expected location.", $objSpec[1], $objSpec[2])\r
+ );\r
+ }\r
+ }\r
+\r
+ // If we're being asked to store all the information\r
+ // about the object, we add the object ID and generation\r
+ // number for later use\r
+ $result = array (\r
+ self::TYPE_OBJECT,\r
+ 'obj' => $objSpec[1],\r
+ 'gen' => $objSpec[2]\r
+ );\r
+\r
+ $this->_currentObj =& $result;\r
+\r
+ // Now simply read the object data until\r
+ // we encounter an end-of-object marker\r
+ while (true) {\r
+ $value = $this->_readValue($c);\r
+ if ($value === false || count($result) > 4) {\r
+ // in this case the parser couldn't find an "endobj" so we break here\r
+ break;\r
+ }\r
+\r
+ if ($value[0] == self::TYPE_TOKEN && $value[1] === 'endobj') {\r
+ break;\r
+ }\r
+\r
+ $result[] = $value;\r
+ }\r
+\r
+ $c->reset($oldPos);\r
+\r
+ if (isset($result[2][0]) && $result[2][0] == self::TYPE_STREAM) {\r
+ $result[0] = self::TYPE_STREAM;\r
+ }\r
+\r
+ } else {\r
+ throw new Exception(\r
+ sprintf("Unable to find object (%s, %s) at expected location.", $objSpec[1], $objSpec[2])\r
+ );\r
}\r
- \r
- $lines = $pieces = $line = $start = $end = $gen = null;\r
- unset($lines, $pieces, $line, $start, $end, $gen);\r
- \r
- fseek($this->f, $o_pos + $trailerPos + 7);\r
- \r
- $c = new pdf_context($this->f);\r
- $trailer = $this->pdf_read_value($c);\r
- \r
- $c = null;\r
- unset($c);\r
- \r
- if (!isset($result['trailer'])) {\r
- $result['trailer'] = $trailer; \r
- }\r
- \r
- if (isset($trailer[1]['/Prev'])) {\r
- $this->pdf_read_xref($result, $trailer[1]['/Prev'][1]);\r
- } \r
- \r
- $trailer = null;\r
- unset($trailer);\r
- \r
- return true;\r
+\r
+ return $result;\r
+ } else {\r
+ return $objSpec;\r
}\r
- \r
- /**\r
- * Reads an Value\r
- *\r
- * @param object $c pdf_context\r
- * @param string $token a Token\r
- * @return mixed\r
- */\r
- function pdf_read_value(&$c, $token = null) {\r
- if (is_null($token)) {\r
- $token = $this->pdf_read_token($c);\r
- }\r
- \r
- if ($token === false) {\r
- return false;\r
- }\r
- \r
- switch ($token) {\r
- case '<':\r
- // This is a hex string.\r
- // Read the value, then the terminator\r
- \r
- $pos = $c->offset;\r
- \r
- while(1) {\r
- \r
- $match = strpos ($c->buffer, '>', $pos);\r
- \r
- // If you can't find it, try\r
- // reading more data from the stream\r
- \r
- if ($match === false) {\r
- if (!$c->increase_length()) {\r
- return false;\r
- } else {\r
- continue;\r
- }\r
- }\r
- \r
- $result = substr ($c->buffer, $c->offset, $match - $c->offset);\r
- $c->offset = $match + 1;\r
- \r
- return array (PDF_TYPE_HEX, $result);\r
+ }\r
+\r
+ /**\r
+ * Reads a token from the context\r
+ *\r
+ * @param pdf_context $c\r
+ * @return mixed\r
+ */\r
+ protected function _readToken($c)\r
+ {\r
+ // If there is a token available\r
+ // on the stack, pop it out and\r
+ // return it.\r
+\r
+ if (count($c->stack)) {\r
+ return array_pop($c->stack);\r
+ }\r
+\r
+ // Strip away any whitespace\r
+\r
+ do {\r
+ if (!$c->ensureContent()) {\r
+ return false;\r
+ }\r
+ $c->offset += strspn($c->buffer, "\x20\x0A\x0C\x0D\x09\x00", $c->offset);\r
+ } while ($c->offset >= $c->length - 1);\r
+\r
+ // Get the first character in the stream\r
+\r
+ $char = $c->buffer[$c->offset++];\r
+\r
+ switch ($char) {\r
+\r
+ case '[':\r
+ case ']':\r
+ case '(':\r
+ case ')':\r
+\r
+ // This is either an array or literal string\r
+ // delimiter, Return it\r
+\r
+ return $char;\r
+\r
+ case '<':\r
+ case '>':\r
+\r
+ // This could either be a hex string or\r
+ // dictionary delimiter. Determine the\r
+ // appropriate case and return the token\r
+\r
+ if ($c->buffer[$c->offset] == $char) {\r
+ if (!$c->ensureContent()) {\r
+ return false;\r
}\r
- \r
- break;\r
- case '<<':\r
- // This is a dictionary.\r
- \r
- $result = array();\r
- \r
- // Recurse into this function until we reach\r
- // the end of the dictionary.\r
- while (($key = $this->pdf_read_token($c)) !== '>>') {\r
- if ($key === false) {\r
- return false;\r
- }\r
- \r
- if (($value = $this->pdf_read_value($c)) === false) {\r
- return false;\r
- }\r
- \r
- // Catch missing value\r
- if ($value[0] == PDF_TYPE_TOKEN && $value[1] == '>>') {\r
- $result[$key] = array(PDF_TYPE_NULL);\r
- break;\r
- }\r
- \r
- $result[$key] = $value;\r
- }\r
- \r
- return array (PDF_TYPE_DICTIONARY, $result);\r
- \r
- case '[':\r
- // This is an array.\r
- \r
- $result = array();\r
- \r
- // Recurse into this function until we reach\r
- // the end of the array.\r
- while (($token = $this->pdf_read_token($c)) !== ']') {\r
- if ($token === false) {\r
- return false;\r
- }\r
- \r
- if (($value = $this->pdf_read_value($c, $token)) === false) {\r
+ $c->offset++;\r
+ return $char . $char;\r
+ } else {\r
+ return $char;\r
+ }\r
+\r
+ case '%':\r
+\r
+ // This is a comment - jump over it!\r
+\r
+ $pos = $c->offset;\r
+ while(1) {\r
+ $match = preg_match("/(\r\n|\r|\n)/", $c->buffer, $m, PREG_OFFSET_CAPTURE, $pos);\r
+ if ($match === 0) {\r
+ if (!$c->increaseLength()) {\r
return false;\r
- }\r
- \r
- $result[] = $value;\r
- }\r
- \r
- return array (PDF_TYPE_ARRAY, $result);\r
- \r
- case '(' :\r
- // This is a string\r
- $pos = $c->offset;\r
- \r
- $openBrackets = 1;\r
- do {\r
- for (; $openBrackets != 0 && $pos < $c->length; $pos++) {\r
- switch (ord($c->buffer[$pos])) {\r
- case 0x28: // '('\r
- $openBrackets++;\r
- break;\r
- case 0x29: // ')'\r
- $openBrackets--;\r
- break;\r
- case 0x5C: // backslash\r
- $pos++;\r
- }\r
+ } else {\r
+ continue;\r
}\r
- } while($openBrackets != 0 && $c->increase_length());\r
- \r
- $result = substr($c->buffer, $c->offset, $pos - $c->offset - 1);\r
- $c->offset = $pos;\r
- \r
- return array (PDF_TYPE_STRING, $result);\r
- \r
- case 'stream':\r
- $o_pos = ftell($c->file)-strlen($c->buffer);\r
- $o_offset = $c->offset;\r
- \r
- $c->reset($startpos = $o_pos + $o_offset);\r
- \r
- $e = 0; // ensure line breaks in front of the stream\r
- if ($c->buffer[0] == chr(10) || $c->buffer[0] == chr(13))\r
- $e++;\r
- if ($c->buffer[1] == chr(10) && $c->buffer[0] != chr(10))\r
- $e++;\r
- \r
- if ($this->actual_obj[1][1]['/Length'][0] == PDF_TYPE_OBJREF) {\r
- $tmp_c = new pdf_context($this->f);\r
- $tmp_length = $this->pdf_resolve_object($tmp_c, $this->actual_obj[1][1]['/Length']);\r
- $length = $tmp_length[1][1];\r
- } else {\r
- $length = $this->actual_obj[1][1]['/Length'][1]; \r
- }\r
- \r
- if ($length > 0) {\r
- $c->reset($startpos + $e,$length);\r
- $v = $c->buffer;\r
- } else {\r
- $v = ''; \r
- }\r
- $c->reset($startpos + $e + $length + 9); // 9 = strlen("endstream")\r
- \r
- return array(PDF_TYPE_STREAM, $v);\r
- \r
- default :\r
- if (is_numeric ($token)) {\r
- // A numeric token. Make sure that\r
- // it is not part of something else.\r
- if (($tok2 = $this->pdf_read_token ($c)) !== false) {\r
- if (is_numeric ($tok2)) {\r
- \r
- // Two numeric tokens in a row.\r
- // In this case, we're probably in\r
- // front of either an object reference\r
- // or an object specification.\r
- // Determine the case and return the data\r
- if (($tok3 = $this->pdf_read_token ($c)) !== false) {\r
- switch ($tok3) {\r
- case 'obj':\r
- return array (PDF_TYPE_OBJDEC, (int) $token, (int) $tok2);\r
- case 'R':\r
- return array (PDF_TYPE_OBJREF, (int) $token, (int) $tok2);\r
- }\r
- // If we get to this point, that numeric value up\r
- // there was just a numeric value. Push the extra\r
- // tokens back into the stack and return the value.\r
- array_push ($c->stack, $tok3);\r
- }\r
- }\r
- \r
- array_push ($c->stack, $tok2);\r
- }\r
- \r
- if ($token === (string)((int)$token))\r
- return array (PDF_TYPE_NUMERIC, (int)$token);\r
- else \r
- return array (PDF_TYPE_REAL, (float)$token);\r
- } elseif ($token == 'true' || $token == 'false') {\r
- return array (PDF_TYPE_BOOLEAN, $token == 'true');\r
- } elseif ($token == 'null') {\r
- return array (PDF_TYPE_NULL);\r
- } else {\r
- // Just a token. Return it.\r
- return array (PDF_TYPE_TOKEN, $token);\r
- }\r
- }\r
- }\r
- \r
- /**\r
- * Resolve an object\r
- *\r
- * @param object $c pdf_context\r
- * @param array $obj_spec The object-data\r
- * @param boolean $encapsulate Must set to true, cause the parsing and fpdi use this method only without this para\r
- */\r
- function pdf_resolve_object(&$c, $obj_spec, $encapsulate = true) {\r
- // Exit if we get invalid data\r
- if (!is_array($obj_spec)) {\r
- $ret = false;\r
- return $ret;\r
- }\r
- \r
- if ($obj_spec[0] == PDF_TYPE_OBJREF) {\r
- \r
- // This is a reference, resolve it\r
- if (isset($this->xref['xref'][$obj_spec[1]][$obj_spec[2]])) {\r
- \r
- // Save current file position\r
- // This is needed if you want to resolve\r
- // references while you're reading another object\r
- // (e.g.: if you need to determine the length\r
- // of a stream)\r
- \r
- $old_pos = ftell($c->file);\r
- \r
- // Reposition the file pointer and\r
- // load the object header.\r
- \r
- $c->reset($this->xref['xref'][$obj_spec[1]][$obj_spec[2]]);\r
- \r
- $header = $this->pdf_read_value($c);\r
- \r
- if ($header[0] != PDF_TYPE_OBJDEC || $header[1] != $obj_spec[1] || $header[2] != $obj_spec[2]) {\r
- $toSearchFor = $obj_spec[1] . ' ' . $obj_spec[2] . ' obj';\r
- if (preg_match('/' . $toSearchFor . '/', $c->buffer)) {\r
- $c->offset = strpos($c->buffer, $toSearchFor) + strlen($toSearchFor);\r
- // reset stack\r
- $c->stack = array();\r
- } else {\r
- $this->error("Unable to find object ({$obj_spec[1]}, {$obj_spec[2]}) at expected location");\r
- }\r
- }\r
- \r
- // If we're being asked to store all the information\r
- // about the object, we add the object ID and generation\r
- // number for later use\r
- $result = array();\r
- $this->actual_obj =& $result;\r
- if ($encapsulate) {\r
- $result = array (\r
- PDF_TYPE_OBJECT,\r
- 'obj' => $obj_spec[1],\r
- 'gen' => $obj_spec[2]\r
- );\r
- } \r
- \r
- // Now simply read the object data until\r
- // we encounter an end-of-object marker\r
- while(1) {\r
- $value = $this->pdf_read_value($c);\r
- if ($value === false || count($result) > 4) {\r
- // in this case the parser coudn't find an endobj so we break here\r
- break;\r
- }\r
- \r
- if ($value[0] == PDF_TYPE_TOKEN && $value[1] === 'endobj') {\r
- break;\r
- }\r
- \r
- $result[] = $value;\r
- }\r
- \r
- $c->reset($old_pos);\r
- \r
- if (isset($result[2][0]) && $result[2][0] == PDF_TYPE_STREAM) {\r
- $result[0] = PDF_TYPE_STREAM;\r
}\r
- \r
- return $result;\r
- }\r
- } else {\r
- return $obj_spec;\r
- }\r
+\r
+ $c->offset = $m[0][1] + strlen($m[0][0]);\r
+\r
+ return $this->_readToken($c);\r
+ }\r
+\r
+ default:\r
+\r
+ // This is "another" type of token (probably\r
+ // a dictionary entry or a numeric value)\r
+ // Find the end and return it.\r
+\r
+ if (!$c->ensureContent()) {\r
+ return false;\r
+ }\r
+\r
+ while(1) {\r
+\r
+ // Determine the length of the token\r
+\r
+ $pos = strcspn($c->buffer, "\x20%[]<>()/\x0A\x0C\x0D\x09\x00", $c->offset);\r
+\r
+ if ($c->offset + $pos <= $c->length - 1) {\r
+ break;\r
+ } else {\r
+ // If the script reaches this point,\r
+ // the token may span beyond the end\r
+ // of the current buffer. Therefore,\r
+ // we increase the size of the buffer\r
+ // and try again--just to be safe.\r
+\r
+ $c->increaseLength();\r
+ }\r
+ }\r
+\r
+ $result = substr($c->buffer, $c->offset - 1, $pos + 1);\r
+\r
+ $c->offset += $pos;\r
+\r
+ return $result;\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Un-filter a stream object\r
+ *\r
+ * @param array $obj\r
+ * @return string\r
+ * @throws Exception\r
+ */\r
+ protected function _unFilterStream($obj)\r
+ {\r
+ $filters = array();\r
+\r
+ if (isset($obj[1][1]['/Filter'])) {\r
+ $filter = $obj[1][1]['/Filter'];\r
+\r
+ if ($filter[0] == pdf_parser::TYPE_OBJREF) {\r
+ $tmpFilter = $this->resolveObject($filter);\r
+ $filter = $tmpFilter[1];\r
+ }\r
+\r
+ if ($filter[0] == pdf_parser::TYPE_TOKEN) {\r
+ $filters[] = $filter;\r
+ } else if ($filter[0] == pdf_parser::TYPE_ARRAY) {\r
+ $filters = $filter[1];\r
+ }\r
}\r
- \r
- \r
- \r
- /**\r
- * Reads a token from the file\r
- *\r
- * @param object $c pdf_context\r
- * @return mixed\r
- */\r
- function pdf_read_token(&$c)\r
- {\r
- // If there is a token available\r
- // on the stack, pop it out and\r
- // return it.\r
- \r
- if (count($c->stack)) {\r
- return array_pop($c->stack);\r
- }\r
- \r
- // Strip away any whitespace\r
- \r
- do {\r
- if (!$c->ensure_content()) {\r
- return false;\r
- }\r
- $c->offset += strspn($c->buffer, "\x20\x0A\x0C\x0D\x09\x00", $c->offset);\r
- } while ($c->offset >= $c->length - 1);\r
- \r
- // Get the first character in the stream\r
- \r
- $char = $c->buffer[$c->offset++];\r
- \r
- switch ($char) {\r
- \r
- case '[':\r
- case ']':\r
- case '(':\r
- case ')':\r
- \r
- // This is either an array or literal string\r
- // delimiter, Return it\r
- \r
- return $char;\r
- \r
- case '<':\r
- case '>':\r
- \r
- // This could either be a hex string or\r
- // dictionary delimiter. Determine the\r
- // appropriate case and return the token\r
- \r
- if ($c->buffer[$c->offset] == $char) {\r
- if (!$c->ensure_content()) {\r
- return false;\r
- }\r
- $c->offset++;\r
- return $char . $char;\r
- } else {\r
- return $char;\r
- }\r
- \r
- case '%':\r
- \r
- // This is a comment - jump over it!\r
- \r
- $pos = $c->offset;\r
- while(1) {\r
- $match = preg_match("/(\r\n|\r|\n)/", $c->buffer, $m, PREG_OFFSET_CAPTURE, $pos);\r
- if ($match === 0) {\r
- if (!$c->increase_length()) {\r
- return false;\r
- } else {\r
- continue;\r
- }\r
- }\r
- \r
- $c->offset = $m[0][1]+strlen($m[0][0]);\r
- \r
- return $this->pdf_read_token($c);\r
+\r
+ $stream = $obj[2][1];\r
+\r
+ foreach ($filters AS $filter) {\r
+ switch ($filter[1]) {\r
+ case '/FlateDecode':\r
+ case '/Fl':\r
+ if (function_exists('gzuncompress')) {\r
+ $oStream = $stream;\r
+ $stream = (strlen($stream) > 0) ? @gzuncompress($stream) : '';\r
+ } else {\r
+ throw new Exception(\r
+ sprintf('To handle %s filter, please compile php with zlib support.', $filter[1])\r
+ );\r
}\r
- \r
- default:\r
- \r
- // This is "another" type of token (probably\r
- // a dictionary entry or a numeric value)\r
- // Find the end and return it.\r
- \r
- if (!$c->ensure_content()) {\r
- return false;\r
- }\r
- \r
- while(1) {\r
- \r
- // Determine the length of the token\r
- \r
- $pos = strcspn($c->buffer, "\x20%[]<>()/\x0A\x0C\x0D\x09\x00", $c->offset);\r
- \r
- if ($c->offset + $pos <= $c->length - 1) {\r
- break;\r
- } else {\r
- // If the script reaches this point,\r
- // the token may span beyond the end\r
- // of the current buffer. Therefore,\r
- // we increase the size of the buffer\r
- // and try again--just to be safe.\r
- \r
- $c->increase_length();\r
- }\r
- }\r
- \r
- $result = substr($c->buffer, $c->offset - 1, $pos + 1);\r
- \r
- $c->offset += $pos;\r
- return $result;\r
- }\r
+\r
+ if ($stream === false) {\r
+ $tries = 0;\r
+ while ($tries < 8 && ($stream === false || strlen($stream) < strlen($oStream))) {\r
+ $oStream = substr($oStream, 1);\r
+ $stream = @gzinflate($oStream);\r
+ $tries++;\r
+ }\r
+\r
+ if ($stream === false) {\r
+ throw new Exception('Error while decompressing stream.');\r
+ }\r
+ }\r
+ break;\r
+ case '/LZWDecode':\r
+ if (!class_exists('FilterLZW')) {\r
+ require_once('filters/FilterLZW.php');\r
+ }\r
+ $decoder = new FilterLZW();\r
+ $stream = $decoder->decode($stream);\r
+ break;\r
+ case '/ASCII85Decode':\r
+ if (!class_exists('FilterASCII85')) {\r
+ require_once('filters/FilterASCII85.php');\r
+ }\r
+ $decoder = new FilterASCII85();\r
+ $stream = $decoder->decode($stream);\r
+ break;\r
+ case '/ASCIIHexDecode':\r
+ if (!class_exists('FilterASCIIHexDecode')) {\r
+ require_once('filters/FilterASCIIHexDecode.php');\r
+ }\r
+ $decoder = new FilterASCIIHexDecode();\r
+ $stream = $decoder->decode($stream);\r
+ break;\r
+ case null:\r
+ break;\r
+ default:\r
+ throw new Exception(sprintf('Unsupported Filter: %s', $filter[1]));\r
+ }\r
}\r
+\r
+ return $stream;\r
}\r
-}\r
+}
\ No newline at end of file