| 1 |
<?php |
| 2 |
// |
| 3 |
// FPDI - Version 1.4.2 |
| 4 |
// |
| 5 |
// Copyright 2004-2011 Setasign - Jan Slabon |
| 6 |
// |
| 7 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
// you may not use this file except in compliance with the License. |
| 9 |
// You may obtain a copy of the License at |
| 10 |
// |
| 11 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
// |
| 13 |
// Unless required by applicable law or agreed to in writing, software |
| 14 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 |
// See the License for the specific language governing permissions and |
| 17 |
// limitations under the License. |
| 18 |
// |
| 19 |
require_once( USCES_PLUGIN_DIR . '/pdf/fpdi/pdf_parser.php'); |
| 20 |
|
| 21 |
|
| 22 |
class fpdi_pdf_parser extends pdf_parser { |
| 23 |
|
| 24 |
/** |
| 25 |
* Pages |
| 26 |
* Index beginns at 0 |
| 27 |
* |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
var $pages; |
| 31 |
|
| 32 |
/** |
| 33 |
* Page count |
| 34 |
* @var integer |
| 35 |
*/ |
| 36 |
var $page_count; |
| 37 |
|
| 38 |
/** |
| 39 |
* actual page number |
| 40 |
* @var integer |
| 41 |
*/ |
| 42 |
var $pageno; |
| 43 |
|
| 44 |
/** |
| 45 |
* PDF Version of imported Document |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
var $pdfVersion; |
| 49 |
|
| 50 |
/** |
| 51 |
* FPDI Reference |
| 52 |
* @var object |
| 53 |
*/ |
| 54 |
var $fpdi; |
| 55 |
|
| 56 |
/** |
| 57 |
* Available BoxTypes |
| 58 |
* |
| 59 |
* @var array |
| 60 |
*/ |
| 61 |
var $availableBoxes = array('/MediaBox', '/CropBox', '/BleedBox', '/TrimBox', '/ArtBox'); |
| 62 |
|
| 63 |
/** |
| 64 |
* Constructor |
| 65 |
* |
| 66 |
* @param string $filename Source-Filename |
| 67 |
* @param object $fpdi Object of type fpdi |
| 68 |
*/ |
| 69 |
function fpdi_pdf_parser($filename, &$fpdi) { |
| 70 |
$this->fpdi =& $fpdi; |
| 71 |
|
| 72 |
parent::pdf_parser($filename); |
| 73 |
|
| 74 |
// resolve Pages-Dictonary |
| 75 |
$pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']); |
| 76 |
|
| 77 |
// Read pages |
| 78 |
$this->read_pages($this->c, $pages, $this->pages); |
| 79 |
|
| 80 |
// count pages; |
| 81 |
$this->page_count = count($this->pages); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Overwrite parent::error() |
| 86 |
* |
| 87 |
* @param string $msg Error-Message |
| 88 |
*/ |
| 89 |
function error($msg) { |
| 90 |
$this->fpdi->error($msg); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get pagecount from sourcefile |
| 95 |
* |
| 96 |
* @return int |
| 97 |
*/ |
| 98 |
function getPageCount() { |
| 99 |
return $this->page_count; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* Set pageno |
| 105 |
* |
| 106 |
* @param int $pageno Pagenumber to use |
| 107 |
*/ |
| 108 |
function setPageno($pageno) { |
| 109 |
$pageno = ((int) $pageno) - 1; |
| 110 |
|
| 111 |
if ($pageno < 0 || $pageno >= $this->getPageCount()) { |
| 112 |
$this->fpdi->error('Pagenumber is wrong!'); |
| 113 |
} |
| 114 |
|
| 115 |
$this->pageno = $pageno; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Get page-resources from current page |
| 120 |
* |
| 121 |
* @return array |
| 122 |
*/ |
| 123 |
function getPageResources() { |
| 124 |
return $this->_getPageResources($this->pages[$this->pageno]); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Get page-resources from /Page |
| 129 |
* |
| 130 |
* @param array $obj Array of pdf-data |
| 131 |
*/ |
| 132 |
function _getPageResources ($obj) { // $obj = /Page |
| 133 |
$obj = $this->pdf_resolve_object($this->c, $obj); |
| 134 |
|
| 135 |
// If the current object has a resources |
| 136 |
// dictionary associated with it, we use |
| 137 |
// it. Otherwise, we move back to its |
| 138 |
// parent object. |
| 139 |
if (isset ($obj[1][1]['/Resources'])) { |
| 140 |
$res = $this->pdf_resolve_object($this->c, $obj[1][1]['/Resources']); |
| 141 |
if ($res[0] == PDF_TYPE_OBJECT) |
| 142 |
return $res[1]; |
| 143 |
return $res; |
| 144 |
} else { |
| 145 |
if (!isset ($obj[1][1]['/Parent'])) { |
| 146 |
return false; |
| 147 |
} else { |
| 148 |
$res = $this->_getPageResources($obj[1][1]['/Parent']); |
| 149 |
if ($res[0] == PDF_TYPE_OBJECT) |
| 150 |
return $res[1]; |
| 151 |
return $res; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
/** |
| 158 |
* Get content of current page |
| 159 |
* |
| 160 |
* If more /Contents is an array, the streams are concated |
| 161 |
* |
| 162 |
* @return string |
| 163 |
*/ |
| 164 |
function getContent() { |
| 165 |
$buffer = ''; |
| 166 |
|
| 167 |
if (isset($this->pages[$this->pageno][1][1]['/Contents'])) { |
| 168 |
$contents = $this->_getPageContent($this->pages[$this->pageno][1][1]['/Contents']); |
| 169 |
foreach($contents AS $tmp_content) { |
| 170 |
$buffer .= $this->_rebuildContentStream($tmp_content) . ' '; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return $buffer; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Resolve all content-objects |
| 180 |
* |
| 181 |
* @param array $content_ref |
| 182 |
* @return array |
| 183 |
*/ |
| 184 |
function _getPageContent($content_ref) { |
| 185 |
$contents = array(); |
| 186 |
|
| 187 |
if ($content_ref[0] == PDF_TYPE_OBJREF) { |
| 188 |
$content = $this->pdf_resolve_object($this->c, $content_ref); |
| 189 |
if ($content[1][0] == PDF_TYPE_ARRAY) { |
| 190 |
$contents = $this->_getPageContent($content[1]); |
| 191 |
} else { |
| 192 |
$contents[] = $content; |
| 193 |
} |
| 194 |
} else if ($content_ref[0] == PDF_TYPE_ARRAY) { |
| 195 |
foreach ($content_ref[1] AS $tmp_content_ref) { |
| 196 |
$contents = array_merge($contents,$this->_getPageContent($tmp_content_ref)); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return $contents; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
/** |
| 205 |
* Rebuild content-streams |
| 206 |
* |
| 207 |
* @param array $obj |
| 208 |
* @return string |
| 209 |
*/ |
| 210 |
function _rebuildContentStream($obj) { |
| 211 |
$filters = array(); |
| 212 |
|
| 213 |
if (isset($obj[1][1]['/Filter'])) { |
| 214 |
$_filter = $obj[1][1]['/Filter']; |
| 215 |
|
| 216 |
if ($_filter[0] == PDF_TYPE_OBJREF) { |
| 217 |
$tmpFilter = $this->pdf_resolve_object($this->c, $_filter); |
| 218 |
$_filter = $tmpFilter[1]; |
| 219 |
} |
| 220 |
|
| 221 |
if ($_filter[0] == PDF_TYPE_TOKEN) { |
| 222 |
$filters[] = $_filter; |
| 223 |
} else if ($_filter[0] == PDF_TYPE_ARRAY) { |
| 224 |
$filters = $_filter[1]; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
$stream = $obj[2][1]; |
| 229 |
|
| 230 |
foreach ($filters AS $_filter) { |
| 231 |
switch ($_filter[1]) { |
| 232 |
case '/FlateDecode': |
| 233 |
case '/Fl': |
| 234 |
// $stream .= "\x0F\x0D"; // in an errorious stream this suffix could work |
| 235 |
// $stream .= "\x0A"; |
| 236 |
// $stream .= "\x0D"; |
| 237 |
if (function_exists('gzuncompress')) { |
| 238 |
$stream = (strlen($stream) > 0) ? @gzuncompress($stream) : ''; |
| 239 |
} else { |
| 240 |
$this->error(sprintf('To handle %s filter, please compile php with zlib support.',$_filter[1])); |
| 241 |
} |
| 242 |
|
| 243 |
if ($stream === false) { |
| 244 |
$this->error('Error while decompressing stream.'); |
| 245 |
} |
| 246 |
break; |
| 247 |
case '/LZWDecode': |
| 248 |
include_once('filters/FilterLZW_FPDI.php'); |
| 249 |
$decoder = new FilterLZW_FPDI($this->fpdi); |
| 250 |
$stream = $decoder->decode($stream); |
| 251 |
break; |
| 252 |
case '/ASCII85Decode': |
| 253 |
include_once('filters/FilterASCII85_FPDI.php'); |
| 254 |
$decoder = new FilterASCII85_FPDI($this->fpdi); |
| 255 |
$stream = $decoder->decode($stream); |
| 256 |
break; |
| 257 |
case null: |
| 258 |
$stream = $stream; |
| 259 |
break; |
| 260 |
default: |
| 261 |
$this->error(sprintf('Unsupported Filter: %s',$_filter[1])); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
return $stream; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
/** |
| 270 |
* Get a Box from a page |
| 271 |
* Arrayformat is same as used by fpdf_tpl |
| 272 |
* |
| 273 |
* @param array $page a /Page |
| 274 |
* @param string $box_index Type of Box @see $availableBoxes |
| 275 |
* @param float Scale factor from user space units to points |
| 276 |
* @return array |
| 277 |
*/ |
| 278 |
function getPageBox($page, $box_index, $k) { |
| 279 |
$page = $this->pdf_resolve_object($this->c, $page); |
| 280 |
$box = null; |
| 281 |
if (isset($page[1][1][$box_index])) |
| 282 |
$box =& $page[1][1][$box_index]; |
| 283 |
|
| 284 |
if (!is_null($box) && $box[0] == PDF_TYPE_OBJREF) { |
| 285 |
$tmp_box = $this->pdf_resolve_object($this->c, $box); |
| 286 |
$box = $tmp_box[1]; |
| 287 |
} |
| 288 |
|
| 289 |
if (!is_null($box) && $box[0] == PDF_TYPE_ARRAY) { |
| 290 |
$b =& $box[1]; |
| 291 |
return array('x' => $b[0][1] / $k, |
| 292 |
'y' => $b[1][1] / $k, |
| 293 |
'w' => abs($b[0][1] - $b[2][1]) / $k, |
| 294 |
'h' => abs($b[1][1] - $b[3][1]) / $k, |
| 295 |
'llx' => min($b[0][1], $b[2][1]) / $k, |
| 296 |
'lly' => min($b[1][1], $b[3][1]) / $k, |
| 297 |
'urx' => max($b[0][1], $b[2][1]) / $k, |
| 298 |
'ury' => max($b[1][1], $b[3][1]) / $k, |
| 299 |
); |
| 300 |
} else if (!isset ($page[1][1]['/Parent'])) { |
| 301 |
return false; |
| 302 |
} else { |
| 303 |
return $this->getPageBox($this->pdf_resolve_object($this->c, $page[1][1]['/Parent']), $box_index, $k); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Get all page boxes by page no |
| 309 |
* |
| 310 |
* @param int The page number |
| 311 |
* @param float Scale factor from user space units to points |
| 312 |
* @return array |
| 313 |
*/ |
| 314 |
function getPageBoxes($pageno, $k) { |
| 315 |
return $this->_getPageBoxes($this->pages[$pageno - 1], $k); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Get all boxes from /Page |
| 320 |
* |
| 321 |
* @param array a /Page |
| 322 |
* @return array |
| 323 |
*/ |
| 324 |
function _getPageBoxes($page, $k) { |
| 325 |
$boxes = array(); |
| 326 |
|
| 327 |
foreach($this->availableBoxes AS $box) { |
| 328 |
if ($_box = $this->getPageBox($page, $box, $k)) { |
| 329 |
$boxes[$box] = $_box; |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
return $boxes; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Get the page rotation by pageno |
| 338 |
* |
| 339 |
* @param integer $pageno |
| 340 |
* @return array |
| 341 |
*/ |
| 342 |
function getPageRotation($pageno) { |
| 343 |
return $this->_getPageRotation($this->pages[$pageno - 1]); |
| 344 |
} |
| 345 |
|
| 346 |
function _getPageRotation($obj) { // $obj = /Page |
| 347 |
$obj = $this->pdf_resolve_object($this->c, $obj); |
| 348 |
if (isset ($obj[1][1]['/Rotate'])) { |
| 349 |
$res = $this->pdf_resolve_object($this->c, $obj[1][1]['/Rotate']); |
| 350 |
if ($res[0] == PDF_TYPE_OBJECT) |
| 351 |
return $res[1]; |
| 352 |
return $res; |
| 353 |
} else { |
| 354 |
if (!isset ($obj[1][1]['/Parent'])) { |
| 355 |
return false; |
| 356 |
} else { |
| 357 |
$res = $this->_getPageRotation($obj[1][1]['/Parent']); |
| 358 |
if ($res[0] == PDF_TYPE_OBJECT) |
| 359 |
return $res[1]; |
| 360 |
return $res; |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Read all /Page(es) |
| 367 |
* |
| 368 |
* @param object pdf_context |
| 369 |
* @param array /Pages |
| 370 |
* @param array the result-array |
| 371 |
*/ |
| 372 |
function read_pages(&$c, &$pages, &$result) { |
| 373 |
// Get the kids dictionary |
| 374 |
$_kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']); |
| 375 |
|
| 376 |
if (!is_array($_kids)) |
| 377 |
$this->error('Cannot find /Kids in current /Page-Dictionary'); |
| 378 |
|
| 379 |
if ($_kids[1][0] == PDF_TYPE_ARRAY) { |
| 380 |
$kids = $_kids[1][1]; |
| 381 |
} else { |
| 382 |
$kids = $_kids[1]; |
| 383 |
} |
| 384 |
|
| 385 |
foreach ($kids as $v) { |
| 386 |
$pg = $this->pdf_resolve_object ($c, $v); |
| 387 |
if ($pg[1][1]['/Type'][1] === '/Pages') { |
| 388 |
// If one of the kids is an embedded |
| 389 |
// /Pages array, resolve it as well. |
| 390 |
$this->read_pages($c, $pg, $result); |
| 391 |
} else { |
| 392 |
$result[] = $pg; |
| 393 |
} |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
/** |
| 400 |
* Get PDF-Version |
| 401 |
* |
| 402 |
* And reset the PDF Version used in FPDI if needed |
| 403 |
*/ |
| 404 |
function getPDFVersion() { |
| 405 |
parent::getPDFVersion(); |
| 406 |
$this->fpdi->setPDFVersion(max($this->fpdi->getPDFVersion(), $this->pdfVersion)); |
| 407 |
} |
| 408 |
} |