PluginProbe
PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin / 1.7.1
PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin v1.7.1
trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9.0 1.9.1 All 71 releases
pdf-print / mpdf / mpdfi / fpdi_pdf_parser.php

fpdi_pdf_parser.php in PDF & Print by BestWebSoft – WordPress Posts and Pages PDF Generator Plugin 1.7.1, at mpdf/mpdfi/fpdi_pdf_parser.php

363 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 //
3 // FPDI - Version 1.2
4 //
5 // Copyright 2004-2007 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
20
21 class fpdi_pdf_parser extends pdf_parser {
22
23 /**
24 * Pages
25 * Index beginns at 0
26 *
27 * @var array
28 */
29 var $pages;
30
31 /**
32 * Page count
33 * @var integer
34 */
35 var $page_count;
36
37 /**
38 * actual page number
39 * @var integer
40 */
41 var $pageno;
42
43
44 /**
45 * FPDI Reference
46 * @var object
47 */
48 var $fpdi;
49
50 /**
51 * Available BoxTypes
52 *
53 * @var array
54 */
55 var $availableBoxes = array("/MediaBox","/CropBox","/BleedBox","/TrimBox","/ArtBox");
56
57 /**
58 * Constructor
59 *
60 * @param string $filename Source-Filename
61 * @param object $fpdi Object of type fpdi
62 */
63 function fpdi_pdf_parser($filename,&$fpdi) {
64 $this->fpdi =& $fpdi;
65 $this->filename = $filename;
66
67 parent::pdf_parser($filename);
68 if ($this->success == false) { return false; }
69
70 // resolve Pages-Dictonary
71 $pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']);
72 if ($this->success == false) { return false; }
73
74 // Read pages
75 $this->read_pages($this->c, $pages, $this->pages);
76 if ($this->success == false) { return false; }
77
78 // count pages;
79 $this->page_count = count($this->pages);
80 }
81
82
83 /**
84 * Get pagecount from sourcefile
85 *
86 * @return int
87 */
88 function getPageCount() {
89 return $this->page_count;
90 }
91
92
93 /**
94 * Set pageno
95 *
96 * @param int $pageno Pagenumber to use
97 */
98 function setPageno($pageno) {
99 $pageno = ((int) $pageno) - 1;
100
101 if ($pageno < 0 || $pageno >= $this->getPageCount()) {
102 $this->fpdi->error("Pagenumber is wrong!");
103 }
104
105 $this->pageno = $pageno;
106 }
107
108 /**
109 * Get page-resources from current page
110 *
111 * @return array
112 */
113 function getPageResources() {
114 return $this->_getPageResources($this->pages[$this->pageno]);
115 }
116
117 /**
118 * Get page-resources from /Page
119 *
120 * @param array $obj Array of pdf-data
121 */
122 function _getPageResources ($obj) { // $obj = /Page
123 $obj = $this->pdf_resolve_object($this->c, $obj);
124
125 // If the current object has a resources
126 // dictionary associated with it, we use
127 // it. Otherwise, we move back to its
128 // parent object.
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)
132 return $res[1];
133 return $res;
134 } else {
135 if (!isset ($obj[1][1]['/Parent'])) {
136 return false;
137 } else {
138 $res = $this->_getPageResources($obj[1][1]['/Parent']);
139 if ($res[0] == PDF_TYPE_OBJECT)
140 return $res[1];
141 return $res;
142 }
143 }
144 }
145
146
147 /**
148 * Get content of current page
149 *
150 * If more /Contents is an array, the streams are concated
151 *
152 * @return string
153 */
154 function getContent() {
155 $buffer = "";
156
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).' ';
161 }
162 }
163
164 return $buffer;
165 }
166
167
168 /**
169 * Resolve all content-objects
170 *
171 * @param array $content_ref
172 * @return array
173 */
174 function _getPageContent($content_ref) {
175 $contents = array();
176
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]);
181 } else {
182 $contents[] = $content;
183 }
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));
187 }
188 }
189
190 return $contents;
191 }
192
193
194 /**
195 * Rebuild content-streams
196 *
197 * @param array $obj
198 * @return string
199 */
200 function _rebuildContentStream($obj) {
201 $filters = array();
202
203 if (isset($obj[1][1]['/Filter'])) {
204 $_filter = $obj[1][1]['/Filter'];
205
206 if ($_filter[0] == PDF_TYPE_TOKEN) {
207 $filters[] = $_filter;
208 } else if ($_filter[0] == PDF_TYPE_ARRAY) {
209 $filters = $_filter[1];
210 }
211 }
212
213 $stream = $obj[2][1];
214
215 foreach ($filters AS $_filter) {
216 switch ($_filter[1]) {
217 case "/FlateDecode":
218 if (function_exists('gzuncompress')) {
219 $stream = (strlen($stream) > 0) ? @gzuncompress($stream) : '';
220 } else {
221 $this->fpdi->error(sprintf("To handle %s filter, please compile php with zlib support.",$_filter[1]));
222 }
223 if ($stream === false) {
224 $this->fpdi->error("Error while decompressing stream.");
225 }
226 break;
227 // mPDF 4.2.003
228 case '/LZWDecode':
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);
233 break;
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);
239 break;
240 case null:
241 $stream = $stream;
242 break;
243 default:
244 $this->fpdi->error(sprintf("Unsupported Filter: %s",$_filter[1]));
245 }
246 }
247
248 return $stream;
249 }
250
251
252 /**
253 * Get a Box from a page
254 * Arrayformat is same as used by fpdf_tpl
255 *
256 * @param array $page a /Page
257 * @param string $box_index Type of Box @see $availableBoxes
258 * @return array
259 */
260 function getPageBox($page, $box_index) {
261 $page = $this->pdf_resolve_object($this->c,$page);
262 $box = null;
263 if (isset($page[1][1][$box_index]))
264 $box =& $page[1][1][$box_index];
265
266 if (!is_null($box) && $box[0] == PDF_TYPE_OBJREF) {
267 $tmp_box = $this->pdf_resolve_object($this->c,$box);
268 $box = $tmp_box[1];
269 }
270
271 if (!is_null($box) && $box[0] == PDF_TYPE_ARRAY) {
272 $b =& $box[1];
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'])) {
278 return false;
279 } else {
280 return $this->getPageBox($this->pdf_resolve_object($this->c, $page[1][1]['/Parent']), $box_index);
281 }
282 }
283
284 function getPageBoxes($pageno) {
285 return $this->_getPageBoxes($this->pages[$pageno-1]);
286 }
287
288 /**
289 * Get all Boxes from /Page
290 *
291 * @param array a /Page
292 * @return array
293 */
294 function _getPageBoxes($page) {
295 $boxes = array();
296
297 foreach($this->availableBoxes AS $box) {
298 if ($_box = $this->getPageBox($page,$box)) {
299 $boxes[$box] = $_box;
300 }
301 }
302
303 return $boxes;
304 }
305
306 function getPageRotation($pageno) {
307 return $this->_getPageRotation($this->pages[$pageno-1]);
308 }
309
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)
315 return $res[1];
316 return $res;
317 } else {
318 if (!isset ($obj[1][1]['/Parent'])) {
319 return false;
320 } else {
321 $res = $this->_getPageRotation($obj[1][1]['/Parent']);
322 if ($res[0] == PDF_TYPE_OBJECT)
323 return $res[1];
324 return $res;
325 }
326 }
327 }
328
329 /**
330 * Read all /Page(es)
331 *
332 * @param object pdf_context
333 * @param array /Pages
334 * @param array the result-array
335 */
336 function read_pages (&$c, &$pages, &$result) {
337 // Get the kids dictionary
338 $kids = $this->pdf_resolve_object ($c, $pages[1][1]['/Kids']);
339
340 if (!is_array($kids)) {
341 // mPDF 4.0
342 $this->success = false;
343 $this->errormsg = sprintf("Cannot find /Kids in current /Page-Dictionary");
344 return false;
345 }
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);
352 } else {
353 $result[] = $pg;
354 }
355 }
356 }
357
358
359
360
361 }
362
363 ?>