]> git.llucax.com Git - mecon/meconlib.git/blob - lib/MECON/PDF/external/template.class.php
A MECON_PDF se le pasa opcionalmente como parametro el nombre que debe tener el archi...
[mecon/meconlib.git] / lib / MECON / PDF / external / template.class.php
1 <?php
2 /*
3    php pdf generation library - template extension
4    Copyright (C) Potential Technologies 2002 - 2003
5    http://www.potentialtech.com
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21    $Id: template.class.php,v 2.3 2003/07/05 21:33:07 wmoran Exp $
22 */
23
24 class template
25 {
26
27         var $nexttid, $templ;
28     var $pdf; // reference to the parent class
29
30     function template()
31     {
32         $this->tid = 0;
33     }
34
35     function create()
36     {
37         $temp = $this->nexttid;
38         // Stores the next object ID within this template
39         $this->templ[$temp]['next'] = 0;
40         $this->nexttid ++;
41         return $temp;
42     }
43
44     function size($tid, $width, $height)
45     {
46         $this->templ[$tid]["height"] = $height;
47         $this->templ[$tid]["width"]  = $width;
48         return true;
49     }
50
51     function rectangle($tid, $bottom, $left, $top, $right, $attrib = array())
52     {
53         $temp = $this->pdf->_resolve_param($attrib);
54         $temp["type"] = "rectangle";
55         $temp["top"] = $top;
56         $temp["left"] = $left;
57         $temp["bottom"] = $bottom;
58         $temp["right"] = $right;
59         return $this->_add_object($temp, $tid);
60     }
61
62     function circle($tid, $cenx, $ceny, $radius, $attrib = array())
63     {
64                 $temp = $this->pdf->_resolve_param($attrib);
65         $temp["type"] = "circle";
66         $temp["x"] = $cenx;
67         $temp["y"] = $ceny;
68         $temp["radius"] = $radius;
69         return $this->_add_object($temp, $tid);
70     }
71
72     function line($tid, $x, $y, $attrib = array())
73     {
74                 $temp = $this->pdf->_resolve_param($attrib);
75         $temp["type"] = "line";
76         $temp["x"] = $x;
77         $temp["y"] = $y;
78         return $this->_add_object($temp, $tid);
79     }
80
81     function image($tid, $left, $bottom, $width, $height, $image, $attrib = array())
82     {
83         $this->ifield($tid, $left, $bottom, $width, $height, false, $image, $attrib);
84     }
85
86     function ifield($tid, $left, $bottom, $width, $height, $name, $default = false, $attrib = array())
87     {
88                 $temp = $this->pdf->_resolve_param($attrib);
89         $temp['type'] = "ifield";
90         $temp['left'] = $left;
91         $temp['bottom'] = $bottom;
92         $temp['name'] = $name;
93         $temp['default'] = $default;
94         $temp['width'] = $width;
95         $temp['height'] = $height;
96         return $this->_add_object($temp, $tid);
97     }
98
99     function text($tid, $left, $bottom, $text, $attrib = array())
100     {
101         return $this->field($tid, $left, $bottom, false, $text, $attrib);
102     }
103
104     function field($tid, $left, $bottom, $name, $default = '', $attrib = array())
105     {
106         $temp = $this->pdf->_resolve_param($attrib);
107         $temp["type"] = "field";
108         $temp["left"] = $left;
109         $temp["bottom"] = $bottom;
110         $temp["name"] = $name;
111         $temp["default"] = $default;
112         return $this->_add_object($temp, $tid);
113     }
114
115     function paragraph($tid, $bottom, $left, $top, $right, $text, $attrib = array())
116     {
117                 return $this->pfield($tid, $bottom, $left, $top, $right, false, $text, $attrib);
118     }
119
120     function pfield($tid, $bottom, $left, $top, $right, $name, $default = '', $attrib = array())
121     {
122         $temp = $this->pdf->_resolve_param($attrib);
123         $temp['type'] = 'pfield';
124         $temp['left'] = $left;
125         $temp['bottom'] = $bottom;
126         $temp['top'] = $top;
127         $temp['right'] = $right;
128         $temp['name'] = $name;
129         $temp['default'] = $default;
130         return $this->_add_object($temp, $tid);
131     }
132
133     function place($tid, $page, $left, $bottom, $data = array())
134     {
135         $ok = true;
136         foreach( $this->templ[$tid]["objects"] as $o ) {
137             switch ($o['type']) {
138             case 'rectangle' :
139                 $ok = $ok && $this->pdf->draw_rectangle($bottom + $o["top"],
140                                                         $left + $o["left"],
141                                                         $bottom + $o["bottom"],
142                                                         $left + $o["right"],
143                                                         $page,
144                                                         $o);
145                 break;
146
147             case 'circle' :
148                 $ok = $ok && $this->pdf->draw_circle($left + $o['x'],
149                                                                              $bottom + $o['y'],
150                                                      $o['radius'],
151                                                      $page,
152                                                      $o);
153                 break;
154
155             case 'line' :
156                 foreach ($o['x'] as $key => $value) {
157                         $o['x'][$key] += $left;
158                     $o['y'][$key] += $bottom;
159                 }
160                 $ok = $ok && $this->pdf->draw_line($o['x'],
161                                                                        $o['y'],
162                                                    $page,
163                                                    $o);
164                 break;
165
166             case 'field' :
167                 $temp = ($o['name'] === false) || !isset($data[$o['name']]) || !strlen($data[$o['name']]) ? $o['default'] : $data[$o['name']];
168                 $ok = $ok && $this->pdf->draw_text($left + $o['left'],
169                                                    $bottom + $o['bottom'],
170                                                    $temp,
171                                                    $page,
172                                                    $o);
173                 break;
174
175             case 'pfield' :
176                 $temp = ($o['name'] === false) || !isset($data[$o['name']]) || !strlen($data[$o['name']]) ? $o['default'] : $data[$o['name']];
177                 $t = $this->pdf->draw_paragraph($bottom + $o['top'],
178                                                 $left + $o['left'],
179                                                 $bottom + $o['bottom'],
180                                                 $left + $o['right'],
181                                                 $temp,
182                                                 $page,
183                                                 $o);
184                 if (is_string($t)) {
185                         $ok = false;
186                     $this->pdf->_push_error(6013, "Text overflowed available area: $t");
187                 }
188                 break;
189
190             case 'ifield' :
191                 $temp = ($o['name'] === false) || empty($data[$o['name']]) ? $o['default'] : $data[$o['name']];
192                 if ($temp === false) {
193                         break;
194                 }
195                 $id = $this->pdf->get_image_size($temp);
196                 unset($o['scale']);
197                 $o['scale']['x'] = $o['width'] / $id['width'];
198                 $o['scale']['y'] = $o['height'] / $id['height'];
199                 $ok = $ok && $this->pdf->image_place($temp,
200                                                                              $o['bottom'] + $bottom,
201                                                      $o['left'] + $left,
202                                                      $page,
203                                                      $o);
204                 break;
205             }
206         }
207         return $ok;
208     }
209
210         /* Private methods
211      */
212
213     function _add_object($objarray, $tid)
214     {
215         $oid = $this->templ[$tid]["next"];
216         $this->templ[$tid]["next"] ++;
217         $this->templ[$tid]["objects"][$oid] = $objarray;
218         return $oid;
219     }
220
221 }
222 ?>