Added HTML2PDF and FPDI to the project.
[openemr.git] / library / html2pdf / fpdi / fpdi_pdf_parser.php
blobfd2b4414c76ab696a80e651240ae1e6964cbac33
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
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 {
24 /**
25 * Pages
26 * Index beginns at 0
28 * @var array
30 var $pages;
32 /**
33 * Page count
34 * @var integer
36 var $page_count;
38 /**
39 * actual page number
40 * @var integer
42 var $pageno;
44 /**
45 * PDF Version of imported Document
46 * @var string
48 var $pdfVersion;
50 /**
51 * FPDI Reference
52 * @var object
54 var $fpdi;
56 /**
57 * Available BoxTypes
59 * @var array
61 var $availableBoxes = array('/MediaBox', '/CropBox', '/BleedBox', '/TrimBox', '/ArtBox');
63 /**
64 * Constructor
66 * @param string $filename Source-Filename
67 * @param object $fpdi Object of type fpdi
69 function fpdi_pdf_parser($filename, &$fpdi) {
70 $this->fpdi =& $fpdi;
72 parent::pdf_parser($filename);
74 // resolve Pages-Dictonary
75 $pages = $this->pdf_resolve_object($this->c, $this->root[1][1]['/Pages']);
77 // Read pages
78 $this->read_pages($this->c, $pages, $this->pages);
80 // count pages;
81 $this->page_count = count($this->pages);
84 /**
85 * Overwrite parent::error()
87 * @param string $msg Error-Message
89 function error($msg) {
90 $this->fpdi->error($msg);
93 /**
94 * Get pagecount from sourcefile
96 * @return int
98 function getPageCount() {
99 return $this->page_count;
104 * Set pageno
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
121 * @return array
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
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;
158 * Get content of current page
160 * If more /Contents is an array, the streams are concated
162 * @return string
164 function getContent() {
165 $buffer = '';
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) . ' ';
174 return $buffer;
179 * Resolve all content-objects
181 * @param array $content_ref
182 * @return array
184 function _getPageContent($content_ref) {
185 $contents = array();
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;
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));
200 return $contents;
205 * Rebuild content-streams
207 * @param array $obj
208 * @return string
210 function _rebuildContentStream($obj) {
211 $filters = array();
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]) {
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]));
243 if ($stream === false) {
244 $this->error('Error while decompressing stream.');
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]));
265 return $stream;
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
276 * @return array
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];
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];
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,
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);
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
312 * @return array
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
322 * @return array
324 function _getPageBoxes($page, $k) {
325 $boxes = array();
327 foreach($this->availableBoxes AS $box) {
328 if ($_box = $this->getPageBox($page, $box, $k)) {
329 $boxes[$box] = $_box;
333 return $boxes;
337 * Get the page rotation by pageno
339 * @param integer $pageno
340 * @return array
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)
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;
366 * Read all /Page(es)
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];
381 } else {
382 $kids = $_kids[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);
391 } else {
392 $result[] = $pg;
400 * Get PDF-Version
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));