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