source: trunk/data/module/pdf/fpdf_tpl.php @ 18562

Revision 18562, 12.1 KB checked in by kajiwara, 14 years ago (diff)

EC-CUBE Ver2.4.3 分コミット。詳細はこちら( http://www.ec-cube.net/release/detail.php?release_id=210

Line 
1<?php
2//
3//  FPDF_TPL - Version 1.1.1
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
20require_once("fpdf.php");
21
22class FPDF_TPL extends FPDF {
23    /**
24     * Array of Tpl-Data
25     * @var array
26     */
27    var $tpls = array();
28
29    /**
30     * Current Template-ID
31     * @var int
32     */
33    var $tpl = 0;
34   
35    /**
36     * "In Template"-Flag
37     * @var boolean
38     */
39    var $_intpl = false;
40   
41    /**
42     * Nameprefix of Templates used in Resources-Dictonary
43     * @var string A String defining the Prefix used as Template-Object-Names. Have to beginn with an /
44     */
45    var $tplprefix = "/TPL";
46
47    /**
48     * Resources used By Templates and Pages
49     * @var array
50     */
51    var $_res = array();
52   
53    /**
54     * Constructor
55     * See FPDF-Documentation
56     * @param string $orientation
57     * @param string $unit
58     * @param mixed $format
59     */
60    function fpdf_tpl($orientation='P',$unit='mm',$format='A4') {
61        parent::fpdf($orientation,$unit,$format);
62    }
63   
64    /**
65     * Start a Template
66     *
67     * This method starts a template. You can give own coordinates to build an own sized
68     * Template. Pay attention, that the margins are adapted to the new templatesize.
69     * If you want to write outside the template, for example to build a clipped Template,
70     * you have to set the Margins and "Cursor"-Position manual after beginTemplate-Call.
71     *
72     * If no parameter is given, the template uses the current page-size.
73     * The Method returns an ID of the current Template. This ID is used later for using this template.
74     * Warning: A created Template is used in PDF at all events. Still if you don't use it after creation!
75     *
76     * @param int $x The x-coordinate given in user-unit
77     * @param int $y The y-coordinate given in user-unit
78     * @param int $w The width given in user-unit
79     * @param int $h The height given in user-unit
80     * @return int The ID of new created Template
81     */
82    function beginTemplate($x=null,$y=null,$w=null,$h=null) {
83        if ($this->page <= 0)
84            $this->error("You have to add a page to fpdf first!");
85
86        if ($x == null)
87            $x = 0;
88        if ($y == null)
89            $y = 0;
90        if ($w == null)
91            $w = $this->w;
92        if ($h == null)
93            $h = $this->h;
94
95        // Save settings
96        $this->tpl++;
97        $tpl =& $this->tpls[$this->tpl];
98        $tpl = array(
99            'o_x' => $this->x,
100            'o_y' => $this->y,
101            'o_AutoPageBreak' => $this->AutoPageBreak,
102            'o_bMargin' => $this->bMargin,
103            'o_tMargin' => $this->tMargin,
104            'o_lMargin' => $this->lMargin,
105            'o_rMargin' => $this->rMargin,
106            'o_h' => $this->h,
107            'o_w' => $this->w,
108            'buffer' => '',
109            'x' => $x,
110            'y' => $y,
111            'w' => $w,
112            'h' => $h
113        );
114
115        $this->SetAutoPageBreak(false);
116       
117        // Define own high and width to calculate possitions correct
118        $this->h = $h;
119        $this->w = $w;
120
121        $this->_intpl = true;
122        $this->SetXY($x+$this->lMargin,$y+$this->tMargin);
123        $this->SetRightMargin($this->w-$w+$this->rMargin);
124
125        return $this->tpl;
126    }
127   
128    /**
129     * End Template
130     *
131     * This method ends a template and reset initiated variables on beginTemplate.
132     *
133     * @return mixed If a template is opened, the ID is returned. If not a false is returned.
134     */
135    function endTemplate() {
136        if ($this->_intpl) {
137            $this->_intpl = false;
138            $tpl =& $this->tpls[$this->tpl];
139            $this->SetXY($tpl['o_x'], $tpl['o_y']);
140            $this->tMargin = $tpl['o_tMargin'];
141            $this->lMargin = $tpl['o_lMargin'];
142            $this->rMargin = $tpl['o_rMargin'];
143            $this->h = $tpl['o_h'];
144            $this->w = $tpl['o_w'];
145            $this->SetAutoPageBreak($tpl['o_AutoPageBreak'], $tpl['o_bMargin']);
146           
147            return $this->tpl;
148        } else {
149            return false;
150        }
151    }
152   
153    /**
154     * Use a Template in current Page or other Template
155     *
156     * You can use a template in a page or in another template.
157     * You can give the used template a new size like you use the Image()-method.
158     * All parameters are optional. The width or height is calculated automaticaly
159     * if one is given. If no parameter is given the origin size as defined in
160     * beginTemplate() is used.
161     * The calculated or used width and height are returned as an array.
162     *
163     * @param int $tplidx A valid template-Id
164     * @param int $_x The x-position
165     * @param int $_y The y-position
166     * @param int $_w The new width of the template
167     * @param int $_h The new height of the template
168     * @retrun array The height and width of the template
169     */
170    function useTemplate($tplidx, $_x=null, $_y=null, $_w=0, $_h=0) {
171        if ($this->page <= 0)
172            $this->error("You have to add a page to fpdf first!");
173
174        if (!isset($this->tpls[$tplidx]))
175            $this->error("Template does not exist!");
176           
177        if ($this->_intpl) {
178            $this->_res['tpl'][$this->tpl]['tpls'][$tplidx] =& $this->tpls[$tplidx];
179        }
180       
181        $tpl =& $this->tpls[$tplidx];
182        $x = $tpl['x'];
183        $y = $tpl['y'];
184        $w = $tpl['w'];
185        $h = $tpl['h'];
186       
187        if ($_x == null)
188            $_x = $x;
189        if ($_y == null)
190            $_y = $y;
191        $wh = $this->getTemplateSize($tplidx,$_w,$_h);
192        $_w = $wh['w'];
193        $_h = $wh['h'];
194   
195        $this->_out(sprintf("q %.4f 0 0 %.4f %.2f %.2f cm", ($_w/$w), ($_h/$h), $_x*$this->k, ($this->h-($_y+$_h))*$this->k)); // Translate
196        $this->_out($this->tplprefix.$tplidx." Do Q");
197
198        return array("w" => $_w, "h" => $_h);
199    }
200   
201    /**
202     * Get The calculated Size of a Template
203     *
204     * If one size is given, this method calculates the other one.
205     *
206     * @param int $tplidx A valid template-Id
207     * @param int $_w The width of the template
208     * @param int $_h The height of the template
209     * @return array The height and width of the template
210     */
211    function getTemplateSize($tplidx, $_w=0, $_h=0) {
212        if (!$this->tpls[$tplidx])
213            return false;
214
215        $tpl =& $this->tpls[$tplidx];
216        $w = $tpl['w'];
217        $h = $tpl['h'];
218       
219        if ($_w == 0 and $_h == 0) {
220            $_w = $w;
221            $_h = $h;
222        }
223
224        if($_w==0)
225            $_w=$_h*$w/$h;
226        if($_h==0)
227            $_h=$_w*$h/$w;
228           
229        return array("w" => $_w, "h" => $_h);
230    }
231   
232    /**
233     * See FPDF-Documentation ;-)
234     */
235    function SetFont($family,$style='',$size=0) {
236        /**
237         * force the resetting of font changes in a template
238         */
239        if ($this->_intpl)
240            $this->FontFamily = '';
241           
242        parent::SetFont($family, $style, $size);
243       
244        $fontkey = $this->FontFamily.$this->FontStyle;
245       
246        if ($this->_intpl) {
247            $this->_res['tpl'][$this->tpl]['fonts'][$fontkey] =& $this->fonts[$fontkey];
248        } else {
249            $this->_res['page'][$this->page]['fonts'][$fontkey] =& $this->fonts[$fontkey];
250        }
251    }
252   
253    /**
254     * See FPDF-Documentation ;-)
255     */
256    function Image($file,$x,$y,$w=0,$h=0,$type='',$link='') {
257        parent::Image($file,$x,$y,$w,$h,$type,$link);
258        if ($this->_intpl) {
259            $this->_res['tpl'][$this->tpl]['images'][$file] =& $this->images[$file];
260        } else {
261            $this->_res['page'][$this->page]['images'][$file] =& $this->images[$file];
262        }
263    }
264   
265    /**
266     * See FPDF-Documentation ;-)
267     *
268     * AddPage is not available when you're "in" a template.
269     */
270    function AddPage($orientation='') {
271        if ($this->_intpl)
272            $this->Error('Adding pages in templates isn\'t possible!');
273        parent::AddPage($orientation);
274    }
275
276    /**
277     * Preserve adding Links in Templates ...won't work
278     */
279    function Link($x,$y,$w,$h,$link) {
280        if ($this->_intpl)
281            $this->Error('Using links in templates aren\'t possible!');
282        parent::Link($x,$y,$w,$h,$link);
283    }
284   
285    function AddLink() {
286        if ($this->_intpl)
287            $this->Error('Adding links in templates aren\'t possible!');
288        return parent::AddLink();
289    }
290   
291    function SetLink($link,$y=0,$page=-1) {
292        if ($this->_intpl)
293            $this->Error('Setting links in templates aren\'t possible!');
294        parent::SetLink($link,$y,$page);
295    }
296   
297    /**
298     * Private Method that writes the form xobjects
299     */
300    function _putformxobjects() {
301        $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
302        reset($this->tpls);
303        foreach($this->tpls AS $tplidx => $tpl) {
304
305            $p=($this->compress) ? gzcompress($tpl['buffer']) : $tpl['buffer'];
306            $this->_newobj();
307            $this->tpls[$tplidx]['n'] = $this->n;
308            $this->_out('<<'.$filter.'/Type /XObject');
309            $this->_out('/Subtype /Form');
310            $this->_out('/FormType 1');
311            $this->_out(sprintf('/BBox [%.2f %.2f %.2f %.2f]',$tpl['x']*$this->k, ($tpl['h']-$tpl['y'])*$this->k, $tpl['w']*$this->k, ($tpl['h']-$tpl['y']-$tpl['h'])*$this->k));
312            $this->_out('/Resources ');
313
314            $this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
315            if (isset($this->_res['tpl'][$tplidx]['fonts']) && count($this->_res['tpl'][$tplidx]['fonts'])) {
316                $this->_out('/Font <<');
317                foreach($this->_res['tpl'][$tplidx]['fonts'] as $font)
318                    $this->_out('/F'.$font['i'].' '.$font['n'].' 0 R');
319                $this->_out('>>');
320            }
321            if(isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images']) ||
322               isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls']))
323            {
324                $this->_out('/XObject <<');
325                if (isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images'])) {
326                    foreach($this->_res['tpl'][$tplidx]['images'] as $image)
327                        $this->_out('/I'.$image['i'].' '.$image['n'].' 0 R');
328                }
329                if (isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls'])) {
330                    foreach($this->_res['tpl'][$tplidx]['tpls'] as $i => $tpl)
331                        $this->_out($this->tplprefix.$i.' '.$tpl['n'].' 0 R');
332                }
333                $this->_out('>>');
334            }
335            $this->_out('>>');
336           
337            $this->_out('/Length '.strlen($p).' >>');
338            $this->_putstream($p);
339            $this->_out('endobj');
340        }
341    }
342   
343    /**
344     * Private Method
345     */
346    function _putresources() {
347        $this->_putfonts();
348        $this->_putimages();
349        $this->_putformxobjects();
350        //Resource dictionary
351        $this->offsets[2]=strlen($this->buffer);
352        $this->_out('2 0 obj');
353        $this->_out('<<');
354        $this->_putresourcedict();
355        $this->_out('>>');
356        $this->_out('endobj');
357    }
358   
359    function _putxobjectdict() {
360        parent::_putxobjectdict();
361       
362        if (count($this->tpls)) {
363            foreach($this->tpls as $tplidx => $tpl) {
364                $this->_out($this->tplprefix.$tplidx.' '.$tpl['n'].' 0 R');
365            }
366        }
367    }
368
369    /**
370     * Private Method
371     */
372    function _out($s) {
373       //Add a line to the document
374       if ($this->state==2) {
375           if (!$this->_intpl)
376               $this->pages[$this->page].=$s."\n";
377           else
378               $this->tpls[$this->tpl]['buffer'] .= $s."\n";
379       } else {
380           $this->buffer.=$s."\n";
381       }
382    }
383}
384
385?>
Note: See TracBrowser for help on using the repository browser.