]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/mpdf/mpdfi/fpdi_pdf_parser.php
5 // Copyright 2004-2007 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.
21 class fpdi_pdf_parser
extends pdf_parser
{
55 var $availableBoxes = array("/MediaBox","/CropBox","/BleedBox","/TrimBox","/ArtBox");
60 * @param string $filename Source-Filename
61 * @param object $fpdi Object of type fpdi
63 function fpdi_pdf_parser($filename,&$fpdi) {
65 $this->filename
= $filename;
67 parent
::pdf_parser($filename);
68 if ($this->success
== false) { return false; }
70 // resolve Pages-Dictonary
71 $pages = $this->pdf_resolve_object($this->c
, $this->root
[1][1]['/Pages']);
72 if ($this->success
== false) { return false; }
75 $this->read_pages($this->c
, $pages, $this->pages
);
76 if ($this->success
== false) { return false; }
79 $this->page_count
= count($this->pages
);
84 * Get pagecount from sourcefile
88 function getPageCount() {
89 return $this->page_count
;
96 * @param int $pageno Pagenumber to use
98 function setPageno($pageno) {
99 $pageno = ((int) $pageno) - 1;
101 if ($pageno < 0 || $pageno >= $this->getPageCount()) {
102 $this->fpdi
->error("Pagenumber is wrong!");
105 $this->pageno
= $pageno;
109 * Get page-resources from current page
113 function getPageResources() {
114 return $this->_getPageResources($this->pages
[$this->pageno
]);
118 * Get page-resources from /Page
120 * @param array $obj Array of pdf-data
122 function _getPageResources ($obj) { // $obj = /Page
123 $obj = $this->pdf_resolve_object($this->c
, $obj);
125 // If the current object has a resources
126 // dictionary associated with it, we use
127 // it. Otherwise, we move back to its
129 if (isset ($obj[1][1]['/Resources'])) {
130 $res = $this->pdf_resolve_object($this->c
, $obj[1][1]['/Resources']);
131 if ($res[0] == PDF_TYPE_OBJECT
)
135 if (!isset ($obj[1][1]['/Parent'])) {
138 $res = $this->_getPageResources($obj[1][1]['/Parent']);
139 if ($res[0] == PDF_TYPE_OBJECT
)
148 * Get content of current page
150 * If more /Contents is an array, the streams are concated
154 function getContent() {
157 if (isset($this->pages
[$this->pageno
][1][1]['/Contents'])) {
158 $contents = $this->_getPageContent($this->pages
[$this->pageno
][1][1]['/Contents']);
159 foreach($contents AS $tmp_content) {
160 $buffer .= $this->_rebuildContentStream($tmp_content).' ';
169 * Resolve all content-objects
171 * @param array $content_ref
174 function _getPageContent($content_ref) {
177 if ($content_ref[0] == PDF_TYPE_OBJREF
) {
178 $content = $this->pdf_resolve_object($this->c
, $content_ref);
179 if ($content[1][0] == PDF_TYPE_ARRAY
) {
180 $contents = $this->_getPageContent($content[1]);
182 $contents[] = $content;
184 } else if ($content_ref[0] == PDF_TYPE_ARRAY
) {
185 foreach ($content_ref[1] AS $tmp_content_ref) {
186 $contents = array_merge($contents,$this->_getPageContent($tmp_content_ref));
195 * Rebuild content-streams
200 function _rebuildContentStream($obj) {
203 if (isset($obj[1][1]['/Filter'])) {
204 $_filter = $obj[1][1]['/Filter'];
206 if ($_filter[0] == PDF_TYPE_TOKEN
) {
207 $filters[] = $_filter;
208 } else if ($_filter[0] == PDF_TYPE_ARRAY
) {
209 $filters = $_filter[1];
213 $stream = $obj[2][1];
215 foreach ($filters AS $_filter) {
216 switch ($_filter[1]) {
218 if (function_exists('gzuncompress')) {
219 $stream = (strlen($stream) > 0) ? @gzuncompress($stream) : '';
221 $this->fpdi
->error(sprintf("To handle %s filter, please compile php with zlib support.",$_filter[1]));
223 if ($stream === false) {
224 $this->fpdi
->error("Error while decompressing stream.");
229 include_once(_MPDF_PATH
.'mpdfi/filters/FilterLZW.php');
230 // mPDF 5.0 Removed pass by reference =&
231 $decoder = new FilterLZW();
232 $stream = $decoder->decode($stream);
234 case '/ASCII85Decode':
235 include_once(_MPDF_PATH
.'mpdfi/filters/FilterASCII85.php');
236 // mPDF 5.0 Removed pass by reference =&
237 $decoder = new FilterASCII85();
238 $stream = $decoder->decode($stream);
244 $this->fpdi
->error(sprintf("Unsupported Filter: %s",$_filter[1]));
253 * Get a Box from a page
254 * Arrayformat is same as used by fpdf_tpl
256 * @param array $page a /Page
257 * @param string $box_index Type of Box @see $availableBoxes
260 function getPageBox($page, $box_index) {
261 $page = $this->pdf_resolve_object($this->c
,$page);
263 if (isset($page[1][1][$box_index]))
264 $box =& $page[1][1][$box_index];
266 if (!is_null($box) && $box[0] == PDF_TYPE_OBJREF
) {
267 $tmp_box = $this->pdf_resolve_object($this->c
,$box);
271 if (!is_null($box) && $box[0] == PDF_TYPE_ARRAY
) {
273 return array("x" => $b[0][1]/_MPDFK
,
274 "y" => $b[1][1]/_MPDFK
,
275 "w" => abs($b[0][1]-$b[2][1])/_MPDFK
,
276 "h" => abs($b[1][1]-$b[3][1])/_MPDFK
); // mPDF 5.3.90
277 } else if (!isset ($page[1][1]['/Parent'])) {
280 return $this->getPageBox($this->pdf_resolve_object($this->c
, $page[1][1]['/Parent']), $box_index);
284 function getPageBoxes($pageno) {
285 return $this->_getPageBoxes($this->pages
[$pageno-1]);
289 * Get all Boxes from /Page
291 * @param array a /Page
294 function _getPageBoxes($page) {
297 foreach($this->availableBoxes
AS $box) {
298 if ($_box = $this->getPageBox($page,$box)) {
299 $boxes[$box] = $_box;
306 function getPageRotation($pageno) {
307 return $this->_getPageRotation($this->pages
[$pageno-1]);
310 function _getPageRotation ($obj) { // $obj = /Page
311 $obj = $this->pdf_resolve_object($this->c
, $obj);
312 if (isset ($obj[1][1]['/Rotate'])) {
313 $res = $this->pdf_resolve_object($this->c
, $obj[1][1]['/Rotate']);
314 if ($res[0] == PDF_TYPE_OBJECT
)
318 if (!isset ($obj[1][1]['/Parent'])) {
321 $res = $this->_getPageRotation($obj[1][1]['/Parent']);
322 if ($res[0] == PDF_TYPE_OBJECT
)
332 * @param object pdf_context
333 * @param array /Pages
334 * @param array the result-array
336 function read_pages (&$c, &$pages, &$result) {
337 // Get the kids dictionary
338 $kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']);
340 if (!is_array($kids)) {
342 $this->success
= false;
343 $this->errormsg
= sprintf("Cannot find /Kids in current /Page-Dictionary");
346 foreach ($kids[1] as $v) {
347 $pg = $this->pdf_resolve_object ($c, $v);
348 if ($pg[1][1]['/Type'][1] === '/Pages') {
349 // If one of the kids is an embedded
350 // /Pages array, resolve it as well.
351 $this->read_pages ($c, $pg, $result);