Skip to content
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor/
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "libresign/tcpdi_parser",
"license": "LGPL-3.0-or-later",
"autoload": {
"psr-4": {
"LibreSign\\TcpdiParser\\": "src/"
}
},
"authors": [
{
"name": "Vitor Mattos",
"email": "[email protected]"
},
{
"name": "Paul Nicholls",
"homepage": "https://github.com/pauln"
}
]
}
61 changes: 36 additions & 25 deletions tcpdi_parser.php → src/tcpdi_parser.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php

namespace LibreSign\TcpdiParser;

use TCPDF_FILTERS;

//============================================================+
// File name : tcpdi_parser.php
// Version : 1.1
Expand Down Expand Up @@ -47,9 +52,6 @@
* @version 1.1
*/

// include class for decoding filters
require_once(dirname(__FILE__).'/include/tcpdf_filters.php');

if (!defined ('PDF_TYPE_NULL'))
define ('PDF_TYPE_NULL', 0);
if (!defined ('PDF_TYPE_NUMERIC'))
Expand Down Expand Up @@ -256,10 +258,12 @@ public function getPDFVersion() {
function readPages() {
$params = $this->getObjectVal($this->xref['trailer'][1]['/Root']);
$objref = null;
foreach ($params[1][1] as $k=>$v) {
if ($k == '/Pages') {
$objref = $v;
break;
if ($params && !empty($params[1]) && is_array($params[1][1])) {
foreach ($params[1][1] as $k=>$v) {
if ($k == '/Pages') {
$objref = $v;
break;
}
}
}
if ($objref == null || $objref[0] !== PDF_TYPE_OBJREF) {
Expand Down Expand Up @@ -483,7 +487,7 @@ protected function decodeXrefStream($startxref, $xref=array()) {
$v = $sarr[$key];
if (($key == '/Type') AND ($v[0] == PDF_TYPE_TOKEN AND ($v[1] == 'XRef'))) {
$valid_crs = true;
} elseif (($key == '/Index') AND ($v[0] == PDF_TYPE_ARRAY AND count($v[1] >= 2))) {
} elseif (($key == '/Index') AND ($v[0] == PDF_TYPE_ARRAY AND count($v[1]) >= 2)) {
// first object number in the subsection
$index_first = intval($v[1][0][1]);
// number of entries in the subsection
Expand Down Expand Up @@ -709,20 +713,19 @@ protected function getRawObject($offset=0, $data=null) {
$objtype = ''; // object type to be returned
$objval = ''; // object value to be returned
// skip initial white space chars: \x00 null (NUL), \x09 horizontal tab (HT), \x0A line feed (LF), \x0C form feed (FF), \x0D carriage return (CR), \x20 space (SP)
while (strspn($data{$offset}, "\x00\x09\x0a\x0c\x0d\x20") == 1) {
while (strspn($data[$offset], "\x00\x09\x0a\x0c\x0d\x20")) {
$offset++;
}
// get first char
$char = $data{$offset};
$char = $data[$offset];
// get object type
switch ($char) {
case '%': { // \x25 PERCENT SIGN
// skip comment and search for next token
$next = strcspn($data, "\r\n", $offset);
if ($next > 0) {
$offset += $next;
list($obj, $unused) = $this->getRawObject($offset, $data);
return $obj;
return $this->getRawObject($offset, $data);
}
break;
}
Expand All @@ -744,10 +747,10 @@ protected function getRawObject($offset=0, $data=null) {
if ($char == '(') {
$open_bracket = 1;
while ($open_bracket > 0) {
if (!isset($data{$strpos})) {
if (!isset($data[$strpos])) {
break;
}
$ch = $data{$strpos};
$ch = $data[$strpos];
switch ($ch) {
case '\\': { // REVERSE SOLIDUS (5Ch) (Backslash)
// skip next character
Expand Down Expand Up @@ -792,7 +795,7 @@ protected function getRawObject($offset=0, $data=null) {
}
case '<': // \x3C LESS-THAN SIGN
case '>': { // \x3E GREATER-THAN SIGN
if (isset($data{($offset + 1)}) AND ($data{($offset + 1)} == $char)) {
if (isset($data[($offset + 1)]) AND ($data[($offset + 1)] == $char)) {
// dictionary object
$objtype = PDF_TYPE_DICTIONARY;
if ($char == '<') {
Expand All @@ -809,13 +812,16 @@ protected function getRawObject($offset=0, $data=null) {
if (($char == '<') AND (preg_match('/^([0-9A-Fa-f ]+)[>]/iU', substr($data, $offset), $matches) == 1)) {
$objval = $matches[1];
$offset += strlen($matches[0]);
unset($matches);
} else if (($char == '<') AND ($endpos = strpos($this->pdfdata, '>', $offset)) !== FALSE) {
$objval = substr($data, $offset,$endpos-$offset);
$offset = $endpos + 1;
}
unset($matches);
}
break;
}
default: {
$frag = $data{$offset} . @$data{$offset+1} . @$data{$offset+2} . @$data{$offset+3};
$frag = $data[$offset] . @$data[$offset+1] . @$data[$offset+2] . @$data[$offset+3];
switch ($frag) {
case 'endo':
// indirect object
Expand Down Expand Up @@ -888,20 +894,25 @@ private function getDictValue($offset, &$data) {
$objval = array();

// Extract dict from data.
$i=1;
$i=2;
$dict = '';
$offset += 2;
do {
if ($data{$offset} == '>' && $data{$offset+1} == '>') {
$i--;
if ($data[$offset] == '>' && $data[$offset+1] == '>') {
$i -= 2;
$dict .= '>>';
$offset += 2;
} else if ($data{$offset} == '<' && $data{$offset+1} == '<') {
$i++;
} else if ($data[$offset] == '<' && $data[$offset+1] == '<') {
$i += 2;
$dict .= '<<';
$offset += 2;
} else {
$dict .= $data{$offset};
if ($data[$offset] == '<') {
$i++;
} else if ($data[$offset] == '>') {
$i--;
}
$dict .= $data[$offset];
$offset++;
}
} while ($i>0);
Expand Down Expand Up @@ -1425,7 +1436,7 @@ private function _getPageRotation($obj) { // $obj = /Page
if (!isset ($obj[1][1]['/Parent'])) {
return false;
} else {
$res = $this->_getPageRotation($obj[1][1]['/Parent']);
$res = (array)$this->_getPageRotation($obj[1][1]['/Parent']);
if ($res[0] == PDF_TYPE_OBJECT)
return $res[1];
return $res;
Expand All @@ -1441,7 +1452,7 @@ private function _getPageRotation($obj) { // $obj = /Page
*/
public function Error($msg) {
// exit program and print error
die("<strong>TCPDI_PARSER ERROR [{$this->uniqueid}]: </strong>".$msg);
throw new Exception("<strong>TCPDI_PARSER ERROR [{$this->uniqueid}]: </strong>".$msg);
}

} // END OF TCPDF_PARSER CLASS
Expand Down