3 // FPDI - Version 1.4.2
5 // Copyright 2004-2011 Setasign - Jan Slabon
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
20 require_once('pdf_parser.php');
22 class fpdi_pdf_parser
extends pdf_parser
{
45 * PDF Version of imported Document
61 var $availableBoxes = array('/MediaBox', '/CropBox', '/BleedBox', '/TrimBox', '/ArtBox');
66 * @param string $filename Source-Filename
67 * @param object $fpdi Object of type fpdi
69 function fpdi_pdf_parser($filename, &$fpdi) {
72 parent
::pdf_parser($filename);
74 // resolve Pages-Dictonary
75 $pages = $this->pdf_resolve_object($this->c
, $this->root
[1][1]['/Pages']);
78 $this->read_pages($this->c
, $pages, $this->pages
);
81 $this->page_count
= count($this->pages
);
85 * Overwrite parent::error()
87 * @param string $msg Error-Message
89 function error($msg) {
90 $this->fpdi
->error($msg);
94 * Get pagecount from sourcefile
98 function getPageCount() {
99 return $this->page_count
;
106 * @param int $pageno Pagenumber to use
108 function setPageno($pageno) {
109 $pageno = ((int) $pageno) - 1;
111 if ($pageno < 0 ||
$pageno >= $this->getPageCount()) {
112 $this->fpdi
->error('Pagenumber is wrong!');
115 $this->pageno
= $pageno;
119 * Get page-resources from current page
123 function getPageResources() {
124 return $this->_getPageResources($this->pages
[$this->pageno
]);
128 * Get page-resources from /Page
130 * @param array $obj Array of pdf-data
132 function _getPageResources ($obj) { // $obj = /Page
133 $obj = $this->pdf_resolve_object($this->c
, $obj);
135 // If the current object has a resources
136 // dictionary associated with it, we use
137 // it. Otherwise, we move back to its
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
)
145 if (!isset ($obj[1][1]['/Parent'])) {
148 $res = $this->_getPageResources($obj[1][1]['/Parent']);
149 if ($res[0] == PDF_TYPE_OBJECT
)
158 * Get content of current page
160 * If more /Contents is an array, the streams are concated
164 function getContent() {
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) . ' ';
179 * Resolve all content-objects
181 * @param array $content_ref
184 function _getPageContent($content_ref) {
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]);
192 $contents[] = $content;
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));
205 * Rebuild content-streams
210 function _rebuildContentStream($obj) {
213 if (isset($obj[1][1]['/Filter'])) {
214 $_filter = $obj[1][1]['/Filter'];
216 if ($_filter[0] == PDF_TYPE_OBJREF
) {
217 $tmpFilter = $this->pdf_resolve_object($this->c
, $_filter);
218 $_filter = $tmpFilter[1];
221 if ($_filter[0] == PDF_TYPE_TOKEN
) {
222 $filters[] = $_filter;
223 } else if ($_filter[0] == PDF_TYPE_ARRAY
) {
224 $filters = $_filter[1];
228 $stream = $obj[2][1];
230 foreach ($filters AS $_filter) {
231 switch ($_filter[1]) {
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) : '';
240 $this->error(sprintf('To handle %s filter, please compile php with zlib support.',$_filter[1]));
243 if ($stream === false) {
244 $this->error('Error while decompressing stream.');
248 include_once('filters/FilterLZW_FPDI.php');
249 $decoder = new FilterLZW_FPDI($this->fpdi
);
250 $stream = $decoder->decode($stream);
252 case '/ASCII85Decode':
253 include_once('filters/FilterASCII85_FPDI.php');
254 $decoder = new FilterASCII85_FPDI($this->fpdi
);
255 $stream = $decoder->decode($stream);
261 $this->error(sprintf('Unsupported Filter: %s',$_filter[1]));
270 * Get a Box from a page
271 * Arrayformat is same as used by fpdf_tpl
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
278 function getPageBox($page, $box_index, $k) {
279 $page = $this->pdf_resolve_object($this->c
, $page);
281 if (isset($page[1][1][$box_index]))
282 $box =& $page[1][1][$box_index];
284 if (!is_null($box) && $box[0] == PDF_TYPE_OBJREF
) {
285 $tmp_box = $this->pdf_resolve_object($this->c
, $box);
289 if (!is_null($box) && $box[0] == PDF_TYPE_ARRAY
) {
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,
300 } else if (!isset ($page[1][1]['/Parent'])) {
303 return $this->getPageBox($this->pdf_resolve_object($this->c
, $page[1][1]['/Parent']), $box_index, $k);
308 * Get all page boxes by page no
310 * @param int The page number
311 * @param float Scale factor from user space units to points
314 function getPageBoxes($pageno, $k) {
315 return $this->_getPageBoxes($this->pages
[$pageno - 1], $k);
319 * Get all boxes from /Page
321 * @param array a /Page
324 function _getPageBoxes($page, $k) {
327 foreach($this->availableBoxes
AS $box) {
328 if ($_box = $this->getPageBox($page, $box, $k)) {
329 $boxes[$box] = $_box;
337 * Get the page rotation by pageno
339 * @param integer $pageno
342 function getPageRotation($pageno) {
343 return $this->_getPageRotation($this->pages
[$pageno - 1]);
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
)
354 if (!isset ($obj[1][1]['/Parent'])) {
357 $res = $this->_getPageRotation($obj[1][1]['/Parent']);
358 if ($res[0] == PDF_TYPE_OBJECT
)
368 * @param object pdf_context
369 * @param array /Pages
370 * @param array the result-array
372 function read_pages(&$c, &$pages, &$result) {
373 // Get the kids dictionary
374 $_kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']);
376 if (!is_array($_kids))
377 $this->error('Cannot find /Kids in current /Page-Dictionary');
379 if ($_kids[1][0] == PDF_TYPE_ARRAY
) {
380 $kids = $_kids[1][1];
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);
402 * And reset the PDF Version used in FPDI if needed
404 function getPDFVersion() {
405 parent
::getPDFVersion();
406 $this->fpdi
->setPDFVersion(max($this->fpdi
->getPDFVersion(), $this->pdfVersion
));