source: branches/version-2_5-dev/data/module/fpdf/fpdi_pdf_parser.php @ 20116

Revision 20116, 11.1 KB checked in by nanasess, 13 years ago (diff)
  • svn properties を再設定
  • 再設定用のスクリプト追加
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2//
3//  FPDI - Version 1.4
4//
5//    Copyright 2004-2010 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
20require_once('pdf_parser.php');
21
22class 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                    // $stream .= "\x0F\x0D"; // in an errorious stream this suffix could work
234                    if (function_exists('gzuncompress')) {
235                        $stream = (strlen($stream) > 0) ? @gzuncompress($stream) : '';
236                    } else {
237                        $this->error(sprintf('To handle %s filter, please compile php with zlib support.',$_filter[1]));
238                    }
239                   
240                    if ($stream === false) {
241                        $this->error('Error while decompressing stream.');
242                    }
243                break;
244                case '/LZWDecode':
245                    include_once('filters/FilterLZW_FPDI.php');
246                    $decoder = new FilterLZW_FPDI($this->fpdi);
247                    $stream = $decoder->decode($stream);
248                    break;
249                case '/ASCII85Decode':
250                    include_once('filters/FilterASCII85_FPDI.php');
251                    $decoder = new FilterASCII85_FPDI($this->fpdi);
252                    $stream = $decoder->decode($stream);
253                    break;
254                case null:
255                    $stream = $stream;
256                break;
257                default:
258                    $this->error(sprintf('Unsupported Filter: %s',$_filter[1]));
259            }
260        }
261       
262        return $stream;
263    }
264   
265   
266    /**
267     * Get a Box from a page
268     * Arrayformat is same as used by fpdf_tpl
269     *
270     * @param array $page a /Page
271     * @param string $box_index Type of Box @see $availableBoxes
272     * @param float Scale factor from user space units to points
273     * @return array
274     */
275    function getPageBox($page, $box_index, $k) {
276        $page = $this->pdf_resolve_object($this->c, $page);
277        $box = null;
278        if (isset($page[1][1][$box_index]))
279            $box =& $page[1][1][$box_index];
280       
281        if (!is_null($box) && $box[0] == PDF_TYPE_OBJREF) {
282            $tmp_box = $this->pdf_resolve_object($this->c, $box);
283            $box = $tmp_box[1];
284        }
285           
286        if (!is_null($box) && $box[0] == PDF_TYPE_ARRAY) {
287            $b =& $box[1];
288            return array('x' => $b[0][1]/$k,
289                         'y' => $b[1][1]/$k,
290                         'w' => abs($b[0][1]-$b[2][1])/$k,
291                         'h' => abs($b[1][1]-$b[3][1])/$k,
292                         'llx' => min($b[0][1], $b[2][1])/$k,
293                         'lly' => min($b[1][1], $b[3][1])/$k,
294                         'urx' => max($b[0][1], $b[2][1])/$k,
295                         'ury' => max($b[1][1], $b[3][1])/$k,
296                         );
297        } else if (!isset ($page[1][1]['/Parent'])) {
298            return false;
299        } else {
300            return $this->getPageBox($this->pdf_resolve_object($this->c, $page[1][1]['/Parent']), $box_index, $k);
301        }
302    }
303
304    /**
305     * Get all page boxes by page no
306     *
307     * @param int The page number
308     * @param float Scale factor from user space units to points
309     * @return array
310     */
311     function getPageBoxes($pageno, $k) {
312        return $this->_getPageBoxes($this->pages[$pageno-1], $k);
313    }
314   
315    /**
316     * Get all boxes from /Page
317     *
318     * @param array a /Page
319     * @return array
320     */
321    function _getPageBoxes($page, $k) {
322        $boxes = array();
323
324        foreach($this->availableBoxes AS $box) {
325            if ($_box = $this->getPageBox($page, $box, $k)) {
326                $boxes[$box] = $_box;
327            }
328        }
329
330        return $boxes;
331    }
332
333    /**
334     * Get the page rotation by pageno
335     *
336     * @param integer $pageno
337     * @return array
338     */
339    function getPageRotation($pageno) {
340        return $this->_getPageRotation($this->pages[$pageno-1]);
341    }
342   
343    function _getPageRotation($obj) { // $obj = /Page
344        $obj = $this->pdf_resolve_object($this->c, $obj);
345        if (isset ($obj[1][1]['/Rotate'])) {
346            $res = $this->pdf_resolve_object($this->c, $obj[1][1]['/Rotate']);
347            if ($res[0] == PDF_TYPE_OBJECT)
348                return $res[1];
349            return $res;
350        } else {
351            if (!isset ($obj[1][1]['/Parent'])) {
352                return false;
353            } else {
354                $res = $this->_getPageRotation($obj[1][1]['/Parent']);
355                if ($res[0] == PDF_TYPE_OBJECT)
356                    return $res[1];
357                return $res;
358            }
359        }
360    }
361   
362    /**
363     * Read all /Page(es)
364     *
365     * @param object pdf_context
366     * @param array /Pages
367     * @param array the result-array
368     */
369    function read_pages(&$c, &$pages, &$result) {
370        // Get the kids dictionary
371        $_kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']);
372       
373        if (!is_array($_kids))
374            $this->error('Cannot find /Kids in current /Page-Dictionary');
375           
376        if ($_kids[1][0] == PDF_TYPE_ARRAY) {
377            $kids = $_kids[1][1];
378        } else {
379            $kids = $_kids[1];
380        }
381       
382        foreach ($kids as $v) {
383            $pg = $this->pdf_resolve_object ($c, $v);
384            if ($pg[1][1]['/Type'][1] === '/Pages') {
385                // If one of the kids is an embedded
386                // /Pages array, resolve it as well.
387                $this->read_pages($c, $pg, $result);
388            } else {
389                $result[] = $pg;
390            }
391        }
392    }
393
394   
395   
396    /**
397     * Get PDF-Version
398     *
399     * And reset the PDF Version used in FPDI if needed
400     */
401    function getPDFVersion() {
402        parent::getPDFVersion();
403        $this->fpdi->setPDFVersion(max($this->fpdi->getPDFVersion(), $this->pdfVersion));
404    }
405   
406}
Note: See TracBrowser for help on using the repository browser.