source: branches/version-2_12-dev/data/module/fpdi/fpdi.php @ 21867

Revision 21867, 17.5 KB checked in by nakanishi, 12 years ago (diff)

#1831 Copyright Update

Line 
1<?php
2//
3//  FPDI - Version 1.4.1
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
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
20define('FPDI_VERSION', '1.4.1');
21
22// Check for TCPDF and remap TCPDF to FPDF
23if (class_exists('TCPDF', false)) {
24    require_once('fpdi2tcpdf_bridge.php');
25}
26
27require_once('fpdf_tpl.php');
28require_once('fpdi_pdf_parser.php');
29
30
31class FPDI extends FPDF_TPL {
32    /**
33     * Actual filename
34     * @var string
35     */
36    var $current_filename;
37
38    /**
39     * Parser-Objects
40     * @var array
41     */
42    var $parsers;
43   
44    /**
45     * Current parser
46     * @var object
47     */
48    var $current_parser;
49   
50    /**
51     * object stack
52     * @var array
53     */
54    var $_obj_stack;
55   
56    /**
57     * done object stack
58     * @var array
59     */
60    var $_don_obj_stack;
61
62    /**
63     * Current Object Id.
64     * @var integer
65     */
66    var $_current_obj_id;
67   
68    /**
69     * The name of the last imported page box
70     * @var string
71     */
72    var $lastUsedPageBox;
73   
74    /**
75     * Cache for imported pages/template ids
76     * @var array
77     */
78    var $_importedPages = array();
79   
80    /**
81     * Set a source-file
82     *
83     * @param string $filename a valid filename
84     * @return int number of available pages
85     */
86    function setSourceFile($filename) {
87        $this->current_filename = $filename;
88       
89        if (!isset($this->parsers[$filename]))
90            $this->parsers[$filename] = $this->_getPdfParser($filename);
91        $this->current_parser =& $this->parsers[$filename];
92       
93        return $this->parsers[$filename]->getPageCount();
94    }
95   
96    /**
97     * Returns a PDF parser object
98     *
99     * @param string $filename
100     * @return fpdi_pdf_parser
101     */
102    function _getPdfParser($filename) {
103        return new fpdi_pdf_parser($filename, $this);
104    }
105   
106    /**
107     * Get the current PDF version
108     *
109     * @return string
110     */
111    function getPDFVersion() {
112        return $this->PDFVersion;
113    }
114   
115    /**
116     * Set the PDF version
117     *
118     * @return string
119     */
120    function setPDFVersion($version = '1.3') {
121        $this->PDFVersion = $version;
122    }
123   
124    /**
125     * Import a page
126     *
127     * @param int $pageno pagenumber
128     * @return int Index of imported page - to use with fpdf_tpl::useTemplate()
129     */
130    function importPage($pageno, $boxName = '/CropBox') {
131        if ($this->_intpl) {
132            return $this->error('Please import the desired pages before creating a new template.');
133        }
134       
135        $fn = $this->current_filename;
136       
137        // check if page already imported
138        $pageKey = $fn . '-' . ((int)$pageno) . $boxName;
139        if (isset($this->_importedPages[$pageKey]))
140            return $this->_importedPages[$pageKey];
141       
142        $parser =& $this->parsers[$fn];
143        $parser->setPageno($pageno);
144
145        if (!in_array($boxName, $parser->availableBoxes))
146            return $this->Error(sprintf('Unknown box: %s', $boxName));
147        $pageboxes = $parser->getPageBoxes($pageno, $this->k);
148       
149        /**
150         * MediaBox
151         * CropBox: Default -> MediaBox
152         * BleedBox: Default -> CropBox
153         * TrimBox: Default -> CropBox
154         * ArtBox: Default -> CropBox
155         */
156        if (!isset($pageboxes[$boxName]) && ($boxName == '/BleedBox' || $boxName == '/TrimBox' || $boxName == '/ArtBox'))
157            $boxName = '/CropBox';
158        if (!isset($pageboxes[$boxName]) && $boxName == '/CropBox')
159            $boxName = '/MediaBox';
160       
161        if (!isset($pageboxes[$boxName]))
162            return false;
163        $this->lastUsedPageBox = $boxName;
164       
165        $box = $pageboxes[$boxName];
166       
167        $this->tpl++;
168        $this->tpls[$this->tpl] = array();
169        $tpl =& $this->tpls[$this->tpl];
170        $tpl['parser'] =& $parser;
171        $tpl['resources'] = $parser->getPageResources();
172        $tpl['buffer'] = $parser->getContent();
173        $tpl['box'] = $box;
174       
175        // To build an array that can be used by PDF_TPL::useTemplate()
176        $this->tpls[$this->tpl] = array_merge($this->tpls[$this->tpl], $box);
177       
178        // An imported page will start at 0,0 everytime. Translation will be set in _putformxobjects()
179        $tpl['x'] = 0;
180        $tpl['y'] = 0;
181       
182        // handle rotated pages
183        $rotation = $parser->getPageRotation($pageno);
184        $tpl['_rotationAngle'] = 0;
185        if (isset($rotation[1]) && ($angle = $rotation[1] % 360) != 0) {
186            $steps = $angle / 90;
187               
188            $_w = $tpl['w'];
189            $_h = $tpl['h'];
190            $tpl['w'] = $steps % 2 == 0 ? $_w : $_h;
191            $tpl['h'] = $steps % 2 == 0 ? $_h : $_w;
192           
193            if ($angle < 0)
194                $angle += 360;
195           
196            $tpl['_rotationAngle'] = $angle * -1;
197        }
198       
199        $this->_importedPages[$pageKey] = $this->tpl;
200       
201        return $this->tpl;
202    }
203   
204    function getLastUsedPageBox() {
205        return $this->lastUsedPageBox;
206    }
207   
208    function useTemplate($tplidx, $_x = null, $_y = null, $_w = 0, $_h = 0, $adjustPageSize = false) {
209        if ($adjustPageSize == true && is_null($_x) && is_null($_y)) {
210            $size = $this->getTemplateSize($tplidx, $_w, $_h);
211            $format = array($size['w'], $size['h']);
212            if (is_subclass_of($this, 'TCPDF')) {
213                $this->setPageFormat($format, $format[0] > $format[1] ? 'L' : 'P');
214            } else {
215                if ($format[0] != $this->CurPageFormat[0] || $format[1] != $this->CurPageFormat[1]) {
216                    $this->w = $format[0];
217                    $this->h = $format[1];
218                    $this->wPt = $this->w * $this->k;
219                    $this->hPt = $this->h * $this->k;
220                    $this->PageBreakTrigger = $this->h - $this->bMargin;
221                    $this->CurPageFormat = $format;
222                    $this->PageSizes[$this->page] = array($this->wPt, $this->hPt);
223                }
224            }
225        }
226       
227        $this->_out('q 0 J 1 w 0 j 0 G 0 g'); // reset standard values
228        $s = parent::useTemplate($tplidx, $_x, $_y, $_w, $_h);
229        $this->_out('Q');
230       
231        return $s;
232    }
233   
234    /**
235     * Private method, that rebuilds all needed objects of source files
236     */
237    function _putimportedobjects() {
238        if (is_array($this->parsers) && count($this->parsers) > 0) {
239            foreach($this->parsers AS $filename => $p) {
240                $this->current_parser =& $this->parsers[$filename];
241                if (isset($this->_obj_stack[$filename]) && is_array($this->_obj_stack[$filename])) {
242                    while(($n = key($this->_obj_stack[$filename])) !== null) {
243                        $nObj = $this->current_parser->pdf_resolve_object($this->current_parser->c, $this->_obj_stack[$filename][$n][1]);
244                       
245                        $this->_newobj($this->_obj_stack[$filename][$n][0]);
246                       
247                        if ($nObj[0] == PDF_TYPE_STREAM) {
248                            $this->pdf_write_value($nObj);
249                        } else {
250                            $this->pdf_write_value($nObj[1]);
251                        }
252                       
253                        $this->_out('endobj');
254                        $this->_obj_stack[$filename][$n] = null; // free memory
255                        unset($this->_obj_stack[$filename][$n]);
256                        reset($this->_obj_stack[$filename]);
257                    }
258                }
259            }
260        }
261    }
262   
263   
264    /**
265     * Private Method that writes the form xobjects
266     */
267    function _putformxobjects() {
268        $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
269        reset($this->tpls);
270        foreach($this->tpls AS $tplidx => $tpl) {
271            $p=($this->compress) ? gzcompress($tpl['buffer']) : $tpl['buffer'];
272            $this->_newobj();
273            $cN = $this->n; // TCPDF/Protection: rem current "n"
274           
275            $this->tpls[$tplidx]['n'] = $this->n;
276            $this->_out('<<' . $filter . '/Type /XObject');
277            $this->_out('/Subtype /Form');
278            $this->_out('/FormType 1');
279           
280            $this->_out(sprintf('/BBox [%.2F %.2F %.2F %.2F]',
281                (isset($tpl['box']['llx']) ? $tpl['box']['llx'] : $tpl['x']) * $this->k,
282                (isset($tpl['box']['lly']) ? $tpl['box']['lly'] : -$tpl['y']) * $this->k,
283                (isset($tpl['box']['urx']) ? $tpl['box']['urx'] : $tpl['w'] + $tpl['x']) * $this->k,
284                (isset($tpl['box']['ury']) ? $tpl['box']['ury'] : $tpl['h'] - $tpl['y']) * $this->k
285            ));
286           
287            $c = 1;
288            $s = 0;
289            $tx = 0;
290            $ty = 0;
291           
292            if (isset($tpl['box'])) {
293                $tx = -$tpl['box']['llx'];
294                $ty = -$tpl['box']['lly'];
295               
296                if ($tpl['_rotationAngle'] <> 0) {
297                    $angle = $tpl['_rotationAngle'] * M_PI/180;
298                    $c=cos($angle);
299                    $s=sin($angle);
300                   
301                    switch($tpl['_rotationAngle']) {
302                        case -90:
303                           $tx = -$tpl['box']['lly'];
304                           $ty = $tpl['box']['urx'];
305                           break;
306                        case -180:
307                            $tx = $tpl['box']['urx'];
308                            $ty = $tpl['box']['ury'];
309                            break;
310                        case -270:
311                            $tx = $tpl['box']['ury'];
312                            $ty = -$tpl['box']['llx'];
313                            break;
314                    }
315                }
316            } else if ($tpl['x'] != 0 || $tpl['y'] != 0) {
317                $tx = -$tpl['x']*2;
318                $ty = $tpl['y']*2;
319            }
320           
321            $tx *= $this->k;
322            $ty *= $this->k;
323           
324            if ($c != 1 || $s != 0 || $tx != 0 || $ty != 0) {
325                $this->_out(sprintf('/Matrix [%.5F %.5F %.5F %.5F %.5F %.5F]',
326                    $c, $s, -$s, $c, $tx, $ty
327                ));
328            }
329           
330            $this->_out('/Resources ');
331
332            if (isset($tpl['resources'])) {
333                $this->current_parser =& $tpl['parser'];
334                $this->pdf_write_value($tpl['resources']); // "n" will be changed
335            } else {
336                $this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
337                if (isset($this->_res['tpl'][$tplidx]['fonts']) && count($this->_res['tpl'][$tplidx]['fonts'])) {
338                    $this->_out('/Font <<');
339                    foreach($this->_res['tpl'][$tplidx]['fonts'] as $font)
340                        $this->_out('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R');
341                    $this->_out('>>');
342                }
343                if(isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images']) ||
344                   isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls']))
345                {
346                    $this->_out('/XObject <<');
347                    if (isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images'])) {
348                        foreach($this->_res['tpl'][$tplidx]['images'] as $image)
349                            $this->_out('/I' . $image['i'] . ' ' . $image['n'] . ' 0 R');
350                    }
351                    if (isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls'])) {
352                        foreach($this->_res['tpl'][$tplidx]['tpls'] as $i => $tpl)
353                            $this->_out($this->tplprefix . $i . ' ' . $tpl['n'] . ' 0 R');
354                    }
355                    $this->_out('>>');
356                }
357                $this->_out('>>');
358            }
359
360            $nN = $this->n; // TCPDF: rem new "n"
361            $this->n = $cN; // TCPDF: reset to current "n"
362            $this->_out('/Length ' . strlen($p) . ' >>');
363            $this->_putstream($p);
364            $this->_out('endobj');
365            $this->n = $nN; // TCPDF: reset to new "n"
366        }
367       
368        $this->_putimportedobjects();
369    }
370
371    /**
372     * Rewritten to handle existing own defined objects
373     */
374    function _newobj($obj_id = false, $onlynewobj = false) {
375        if (!$obj_id) {
376            $obj_id = ++$this->n;
377        }
378
379        //Begin a new object
380        if (!$onlynewobj) {
381            $this->offsets[$obj_id] = is_subclass_of($this, 'TCPDF') ? $this->bufferlen : strlen($this->buffer);
382            $this->_out($obj_id . ' 0 obj');
383            $this->_current_obj_id = $obj_id; // for later use with encryption
384        }
385       
386        return $obj_id;
387    }
388
389    /**
390     * Writes a value
391     * Needed to rebuild the source document
392     *
393     * @param mixed $value A PDF-Value. Structure of values see cases in this method
394     */
395    function pdf_write_value(&$value)
396    {
397        if (is_subclass_of($this, 'TCPDF')) {
398            parent::pdf_write_value($value);
399        }
400       
401        switch ($value[0]) {
402
403            case PDF_TYPE_TOKEN:
404                $this->_straightOut($value[1] . ' ');
405                break;
406            case PDF_TYPE_NUMERIC:
407            case PDF_TYPE_REAL:
408                if (is_float($value[1]) && $value[1] != 0) {
409                    $this->_straightOut(rtrim(rtrim(sprintf('%F', $value[1]), '0'), '.') . ' ');
410                } else {
411                    $this->_straightOut($value[1] . ' ');
412                }
413                break;
414               
415            case PDF_TYPE_ARRAY:
416
417                // An array. Output the proper
418                // structure and move on.
419
420                $this->_straightOut('[');
421                for ($i = 0; $i < count($value[1]); $i++) {
422                    $this->pdf_write_value($value[1][$i]);
423                }
424
425                $this->_out(']');
426                break;
427
428            case PDF_TYPE_DICTIONARY:
429
430                // A dictionary.
431                $this->_straightOut('<<');
432
433                reset ($value[1]);
434
435                while (list($k, $v) = each($value[1])) {
436                    $this->_straightOut($k . ' ');
437                    $this->pdf_write_value($v);
438                }
439
440                $this->_straightOut('>>');
441                break;
442
443            case PDF_TYPE_OBJREF:
444
445                // An indirect object reference
446                // Fill the object stack if needed
447                $cpfn =& $this->current_parser->filename;
448               
449                if (!isset($this->_don_obj_stack[$cpfn][$value[1]])) {
450                    $this->_newobj(false, true);
451                    $this->_obj_stack[$cpfn][$value[1]] = array($this->n, $value);
452                    $this->_don_obj_stack[$cpfn][$value[1]] = array($this->n, $value); // Value is maybee obsolete!!!
453                }
454                $objid = $this->_don_obj_stack[$cpfn][$value[1]][0];
455
456                $this->_out($objid . ' 0 R');
457                break;
458
459            case PDF_TYPE_STRING:
460
461                // A string.
462                $this->_straightOut('(' . $value[1] . ')');
463
464                break;
465
466            case PDF_TYPE_STREAM:
467
468                // A stream. First, output the
469                // stream dictionary, then the
470                // stream data itself.
471                $this->pdf_write_value($value[1]);
472                $this->_out('stream');
473                $this->_out($value[2][1]);
474                $this->_out('endstream');
475                break;
476               
477            case PDF_TYPE_HEX:
478                $this->_straightOut('<' . $value[1] . '>');
479                break;
480
481            case PDF_TYPE_BOOLEAN:
482                $this->_straightOut($value[1] ? 'true ' : 'false ');
483                break;
484           
485            case PDF_TYPE_NULL:
486                // The null object.
487
488                $this->_straightOut('null ');
489                break;
490        }
491    }
492   
493   
494    /**
495     * Modified so not each call will add a newline to the output.
496     */
497    function _straightOut($s) {
498        if (!is_subclass_of($this, 'TCPDF')) {
499            if($this->state==2)
500                $this->pages[$this->page] .= $s;
501            else
502                $this->buffer .= $s;
503        } else {
504            if ($this->state == 2) {
505                if (isset($this->footerlen[$this->page]) AND ($this->footerlen[$this->page] > 0)) {
506                    // puts data before page footer
507                    $page = substr($this->getPageBuffer($this->page), 0, -$this->footerlen[$this->page]);
508                    $footer = substr($this->getPageBuffer($this->page), -$this->footerlen[$this->page]);
509                    $this->setPageBuffer($this->page, $page . ' ' . $s . "\n" . $footer);
510                } else {
511                    $this->setPageBuffer($this->page, $s, true);
512                }
513            } else {
514                $this->setBuffer($s);
515            }
516        }
517    }
518
519    /**
520     * rewritten to close opened parsers
521     *
522     */
523    function _enddoc() {
524        parent::_enddoc();
525        $this->_closeParsers();
526    }
527   
528    /**
529     * close all files opened by parsers
530     */
531    function _closeParsers() {
532        if ($this->state > 2 && count($this->parsers) > 0) {
533            foreach ($this->parsers as $k => $_){
534                $this->parsers[$k]->closeFile();
535                $this->parsers[$k] = null;
536                unset($this->parsers[$k]);
537            }
538            return true;
539        }
540        return false;
541    }
542}
Note: See TracBrowser for help on using the repository browser.