| 1 |
<?php |
| 2 |
// |
| 3 |
// FPDF_TPL - Version 1.2 |
| 4 |
// |
| 5 |
// Copyright 2004-2010 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 |
require_once( USCES_PLUGIN_DIR. '/pdf/tcpdf/tcpdf.php'); |
| 20 |
|
| 21 |
|
| 22 |
class 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 |
* Last used Template data |
| 55 |
* |
| 56 |
* @var array |
| 57 |
*/ |
| 58 |
var $lastUsedTemplateData = array(); |
| 59 |
|
| 60 |
/** |
| 61 |
* Start a Template |
| 62 |
* |
| 63 |
* This method starts a template. You can give own coordinates to build an own sized |
| 64 |
* Template. Pay attention, that the margins are adapted to the new templatesize. |
| 65 |
* If you want to write outside the template, for example to build a clipped Template, |
| 66 |
* you have to set the Margins and "Cursor"-Position manual after beginTemplate-Call. |
| 67 |
* |
| 68 |
* If no parameter is given, the template uses the current page-size. |
| 69 |
* The Method returns an ID of the current Template. This ID is used later for using this template. |
| 70 |
* Warning: A created Template is used in PDF at all events. Still if you don't use it after creation! |
| 71 |
* |
| 72 |
* @param int $x The x-coordinate given in user-unit |
| 73 |
* @param int $y The y-coordinate given in user-unit |
| 74 |
* @param int $w The width given in user-unit |
| 75 |
* @param int $h The height given in user-unit |
| 76 |
* @return int The ID of new created Template |
| 77 |
*/ |
| 78 |
function beginTemplate($x = null, $y = null, $w = null, $h = null) { |
| 79 |
if (is_subclass_of($this, 'TCPDF')) { |
| 80 |
$this->Error('This method is only usable with FPDF. Use TCPDF methods startTemplate() instead.'); |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
if ($this->page <= 0) |
| 85 |
$this->error("You have to add a page to fpdf first!"); |
| 86 |
|
| 87 |
if ($x == null) |
| 88 |
$x = 0; |
| 89 |
if ($y == null) |
| 90 |
$y = 0; |
| 91 |
if ($w == null) |
| 92 |
$w = $this->w; |
| 93 |
if ($h == null) |
| 94 |
$h = $this->h; |
| 95 |
|
| 96 |
// Save settings |
| 97 |
$this->tpl++; |
| 98 |
$tpl =& $this->tpls[$this->tpl]; |
| 99 |
$tpl = array( |
| 100 |
'o_x' => $this->x, |
| 101 |
'o_y' => $this->y, |
| 102 |
'o_AutoPageBreak' => $this->AutoPageBreak, |
| 103 |
'o_bMargin' => $this->bMargin, |
| 104 |
'o_tMargin' => $this->tMargin, |
| 105 |
'o_lMargin' => $this->lMargin, |
| 106 |
'o_rMargin' => $this->rMargin, |
| 107 |
'o_h' => $this->h, |
| 108 |
'o_w' => $this->w, |
| 109 |
'buffer' => '', |
| 110 |
'x' => $x, |
| 111 |
'y' => $y, |
| 112 |
'w' => $w, |
| 113 |
'h' => $h |
| 114 |
); |
| 115 |
|
| 116 |
$this->SetAutoPageBreak(false); |
| 117 |
|
| 118 |
// Define own high and width to calculate possitions correct |
| 119 |
$this->h = $h; |
| 120 |
$this->w = $w; |
| 121 |
|
| 122 |
$this->_intpl = true; |
| 123 |
$this->SetXY($x + $this->lMargin, $y + $this->tMargin); |
| 124 |
$this->SetRightMargin($this->w - $w + $this->rMargin); |
| 125 |
|
| 126 |
return $this->tpl; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* End Template |
| 131 |
* |
| 132 |
* This method ends a template and reset initiated variables on beginTemplate. |
| 133 |
* |
| 134 |
* @return mixed If a template is opened, the ID is returned. If not a false is returned. |
| 135 |
*/ |
| 136 |
function endTemplate() { |
| 137 |
if (is_subclass_of($this, 'TCPDF')) { |
| 138 |
$args = func_get_args(); |
| 139 |
return call_user_func_array(array($this, 'TCPDF::endTemplate'), $args); |
| 140 |
} |
| 141 |
|
| 142 |
if ($this->_intpl) { |
| 143 |
$this->_intpl = false; |
| 144 |
$tpl =& $this->tpls[$this->tpl]; |
| 145 |
$this->SetXY($tpl['o_x'], $tpl['o_y']); |
| 146 |
$this->tMargin = $tpl['o_tMargin']; |
| 147 |
$this->lMargin = $tpl['o_lMargin']; |
| 148 |
$this->rMargin = $tpl['o_rMargin']; |
| 149 |
$this->h = $tpl['o_h']; |
| 150 |
$this->w = $tpl['o_w']; |
| 151 |
$this->SetAutoPageBreak($tpl['o_AutoPageBreak'], $tpl['o_bMargin']); |
| 152 |
|
| 153 |
return $this->tpl; |
| 154 |
} else { |
| 155 |
return false; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Use a Template in current Page or other Template |
| 161 |
* |
| 162 |
* You can use a template in a page or in another template. |
| 163 |
* You can give the used template a new size like you use the Image()-method. |
| 164 |
* All parameters are optional. The width or height is calculated automaticaly |
| 165 |
* if one is given. If no parameter is given the origin size as defined in |
| 166 |
* beginTemplate() is used. |
| 167 |
* The calculated or used width and height are returned as an array. |
| 168 |
* |
| 169 |
* @param int $tplidx A valid template-Id |
| 170 |
* @param int $_x The x-position |
| 171 |
* @param int $_y The y-position |
| 172 |
* @param int $_w The new width of the template |
| 173 |
* @param int $_h The new height of the template |
| 174 |
* @retrun array The height and width of the template |
| 175 |
*/ |
| 176 |
function useTemplate($tplidx, $_x = null, $_y = null, $_w = 0, $_h = 0) { |
| 177 |
if ($this->page <= 0) |
| 178 |
$this->error('You have to add a page first!'); |
| 179 |
|
| 180 |
if (!isset($this->tpls[$tplidx])) |
| 181 |
$this->error('Template does not exist!'); |
| 182 |
|
| 183 |
if ($this->_intpl) { |
| 184 |
$this->_res['tpl'][$this->tpl]['tpls'][$tplidx] =& $this->tpls[$tplidx]; |
| 185 |
} |
| 186 |
|
| 187 |
$tpl =& $this->tpls[$tplidx]; |
| 188 |
$w = $tpl['w']; |
| 189 |
$h = $tpl['h']; |
| 190 |
|
| 191 |
if ($_x == null) |
| 192 |
$_x = 0; |
| 193 |
if ($_y == null) |
| 194 |
$_y = 0; |
| 195 |
|
| 196 |
$_x += $tpl['x']; |
| 197 |
$_y += $tpl['y']; |
| 198 |
|
| 199 |
$wh = $this->getTemplateSize($tplidx, $_w, $_h); |
| 200 |
$_w = $wh['w']; |
| 201 |
$_h = $wh['h']; |
| 202 |
|
| 203 |
$tData = array( |
| 204 |
'x' => $this->x, |
| 205 |
'y' => $this->y, |
| 206 |
'w' => $_w, |
| 207 |
'h' => $_h, |
| 208 |
'scaleX' => ($_w / $w), |
| 209 |
'scaleY' => ($_h / $h), |
| 210 |
'tx' => $_x, |
| 211 |
'ty' => ($this->h - $_y - $_h), |
| 212 |
'lty' => ($this->h - $_y - $_h) - ($this->h - $h) * ($_h / $h) |
| 213 |
); |
| 214 |
|
| 215 |
$this->_out(sprintf('q %.4F 0 0 %.4F %.4F %.4F cm', $tData['scaleX'], $tData['scaleY'], $tData['tx'] * $this->k, $tData['ty'] * $this->k)); // Translate |
| 216 |
$this->_out(sprintf('%s%d Do Q', $this->tplprefix, $tplidx)); |
| 217 |
|
| 218 |
// reset font in the outer graphic state |
| 219 |
if ($this->FontFamily) { |
| 220 |
$family = $this->FontFamily; |
| 221 |
$this->FontFamily = ''; |
| 222 |
$this->SetFont($family); |
| 223 |
} |
| 224 |
|
| 225 |
$this->lastUsedTemplateData = $tData; |
| 226 |
|
| 227 |
return array('w' => $_w, 'h' => $_h); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Get The calculated Size of a Template |
| 232 |
* |
| 233 |
* If one size is given, this method calculates the other one. |
| 234 |
* |
| 235 |
* @param int $tplidx A valid template-Id |
| 236 |
* @param int $_w The width of the template |
| 237 |
* @param int $_h The height of the template |
| 238 |
* @return array The height and width of the template |
| 239 |
*/ |
| 240 |
function getTemplateSize($tplidx, $_w = 0, $_h = 0) { |
| 241 |
if (!$this->tpls[$tplidx]) |
| 242 |
return false; |
| 243 |
|
| 244 |
$tpl =& $this->tpls[$tplidx]; |
| 245 |
$w = $tpl['w']; |
| 246 |
$h = $tpl['h']; |
| 247 |
|
| 248 |
if ($_w == 0 and $_h == 0) { |
| 249 |
$_w = $w; |
| 250 |
$_h = $h; |
| 251 |
} |
| 252 |
|
| 253 |
if($_w == 0) |
| 254 |
$_w = $_h * $w / $h; |
| 255 |
if($_h == 0) |
| 256 |
$_h = $_w * $h / $w; |
| 257 |
|
| 258 |
return array("w" => $_w, "h" => $_h); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* See FPDF/TCPDF-Documentation ;-) |
| 263 |
*/ |
| 264 |
public function SetFont($family, $style = '', $size = 0, $fontfile='', $subset='default', $out=true) { |
| 265 |
if (is_subclass_of($this, 'TCPDF')) { |
| 266 |
$args = func_get_args(); |
| 267 |
return call_user_func_array(array($this, 'TCPDF::SetFont'), $args); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* force the resetting of font changes in a template |
| 272 |
*/ |
| 273 |
if ($this->_intpl) |
| 274 |
$this->FontFamily = ''; |
| 275 |
|
| 276 |
parent::SetFont($family, $style, $size); |
| 277 |
|
| 278 |
$fontkey = $this->FontFamily . $this->FontStyle; |
| 279 |
|
| 280 |
if ($this->_intpl) { |
| 281 |
$this->_res['tpl'][$this->tpl]['fonts'][$fontkey] =& $this->fonts[$fontkey]; |
| 282 |
} else { |
| 283 |
$this->_res['page'][$this->page]['fonts'][$fontkey] =& $this->fonts[$fontkey]; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* See FPDF/TCPDF-Documentation ;-) |
| 289 |
*/ |
| 290 |
function Image($file, $x = null, $y = null, $w = 0, $h = 0, $type = '', $link = '', $align='', $resize=false, $dpi=300, $palign='', $ismask=false, $imgmask=false, $border=0, $fitbox=false, $hidden=false, $fitonpage=false, $alt=false, $altimgs=array()) { |
| 291 |
if (is_subclass_of($this, 'TCPDF')) { |
| 292 |
$args = func_get_args(); |
| 293 |
return call_user_func_array(array($this, 'TCPDF::Image'), $args); |
| 294 |
} |
| 295 |
|
| 296 |
$ret = parent::Image($file, $x, $y, $w, $h, $type, $link); |
| 297 |
if ($this->_intpl) { |
| 298 |
$this->_res['tpl'][$this->tpl]['images'][$file] =& $this->images[$file]; |
| 299 |
} else { |
| 300 |
$this->_res['page'][$this->page]['images'][$file] =& $this->images[$file]; |
| 301 |
} |
| 302 |
|
| 303 |
return $ret; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* See FPDF-Documentation ;-) |
| 308 |
* |
| 309 |
* AddPage is not available when you're "in" a template. |
| 310 |
*/ |
| 311 |
function AddPage($orientation = '', $format = '', $keepmargins=false, $tocpage=false) { |
| 312 |
if (is_subclass_of($this, 'TCPDF')) { |
| 313 |
$args = func_get_args(); |
| 314 |
return call_user_func_array(array($this, 'TCPDF::AddPage'), $args); |
| 315 |
} |
| 316 |
|
| 317 |
if ($this->_intpl) |
| 318 |
$this->Error('Adding pages in templates isn\'t possible!'); |
| 319 |
|
| 320 |
parent::AddPage($orientation, $format); |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Preserve adding Links in Templates ...won't work |
| 325 |
*/ |
| 326 |
function Link($x, $y, $w, $h, $link, $spaces=0) { |
| 327 |
if (is_subclass_of($this, 'TCPDF')) { |
| 328 |
$args = func_get_args(); |
| 329 |
return call_user_func_array(array($this, 'TCPDF::Link'), $args); |
| 330 |
} |
| 331 |
|
| 332 |
if ($this->_intpl) |
| 333 |
$this->Error('Using links in templates aren\'t possible!'); |
| 334 |
|
| 335 |
parent::Link($x, $y, $w, $h, $link); |
| 336 |
} |
| 337 |
|
| 338 |
function AddLink() { |
| 339 |
if (is_subclass_of($this, 'TCPDF')) { |
| 340 |
$args = func_get_args(); |
| 341 |
return call_user_func_array(array($this, 'TCPDF::AddLink'), $args); |
| 342 |
} |
| 343 |
|
| 344 |
if ($this->_intpl) |
| 345 |
$this->Error('Adding links in templates aren\'t possible!'); |
| 346 |
return parent::AddLink(); |
| 347 |
} |
| 348 |
|
| 349 |
function SetLink($link, $y = 0, $page = -1) { |
| 350 |
if (is_subclass_of($this, 'TCPDF')) { |
| 351 |
$args = func_get_args(); |
| 352 |
return call_user_func_array(array($this, 'TCPDF::SetLink'), $args); |
| 353 |
} |
| 354 |
|
| 355 |
if ($this->_intpl) |
| 356 |
$this->Error('Setting links in templates aren\'t possible!'); |
| 357 |
parent::SetLink($link, $y, $page); |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Private Method that writes the form xobjects |
| 362 |
*/ |
| 363 |
function _putformxobjects() { |
| 364 |
$filter=($this->compress) ? '/Filter /FlateDecode ' : ''; |
| 365 |
reset($this->tpls); |
| 366 |
foreach($this->tpls AS $tplidx => $tpl) { |
| 367 |
|
| 368 |
$p=($this->compress) ? gzcompress($tpl['buffer']) : $tpl['buffer']; |
| 369 |
$this->_newobj(); |
| 370 |
$this->tpls[$tplidx]['n'] = $this->n; |
| 371 |
$this->_out('<<'.$filter.'/Type /XObject'); |
| 372 |
$this->_out('/Subtype /Form'); |
| 373 |
$this->_out('/FormType 1'); |
| 374 |
$this->_out(sprintf('/BBox [%.2F %.2F %.2F %.2F]', |
| 375 |
// llx |
| 376 |
$tpl['x'] * $this->k, |
| 377 |
// lly |
| 378 |
-$tpl['y'] * $this->k, |
| 379 |
// urx |
| 380 |
($tpl['w'] + $tpl['x']) * $this->k, |
| 381 |
// ury |
| 382 |
($tpl['h'] - $tpl['y']) * $this->k |
| 383 |
)); |
| 384 |
|
| 385 |
if ($tpl['x'] != 0 || $tpl['y'] != 0) { |
| 386 |
$this->_out(sprintf('/Matrix [1 0 0 1 %.5F %.5F]', |
| 387 |
-$tpl['x'] * $this->k * 2, $tpl['y'] * $this->k * 2 |
| 388 |
)); |
| 389 |
} |
| 390 |
|
| 391 |
$this->_out('/Resources '); |
| 392 |
|
| 393 |
$this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]'); |
| 394 |
if (isset($this->_res['tpl'][$tplidx]['fonts']) && count($this->_res['tpl'][$tplidx]['fonts'])) { |
| 395 |
$this->_out('/Font <<'); |
| 396 |
foreach($this->_res['tpl'][$tplidx]['fonts'] as $font) |
| 397 |
$this->_out('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R'); |
| 398 |
$this->_out('>>'); |
| 399 |
} |
| 400 |
if(isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images']) || |
| 401 |
isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls'])) |
| 402 |
{ |
| 403 |
$this->_out('/XObject <<'); |
| 404 |
if (isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images'])) { |
| 405 |
foreach($this->_res['tpl'][$tplidx]['images'] as $image) |
| 406 |
$this->_out('/I' . $image['i'] . ' ' . $image['n'] . ' 0 R'); |
| 407 |
} |
| 408 |
if (isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls'])) { |
| 409 |
foreach($this->_res['tpl'][$tplidx]['tpls'] as $i => $tpl) |
| 410 |
$this->_out($this->tplprefix . $i . ' ' . $tpl['n'] . ' 0 R'); |
| 411 |
} |
| 412 |
$this->_out('>>'); |
| 413 |
} |
| 414 |
$this->_out('>>'); |
| 415 |
|
| 416 |
$this->_out('/Length ' . strlen($p) . ' >>'); |
| 417 |
$this->_putstream($p); |
| 418 |
$this->_out('endobj'); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
/** |
| 423 |
* Overwritten to add _putformxobjects() after _putimages() |
| 424 |
* |
| 425 |
*/ |
| 426 |
function _putimages() { |
| 427 |
parent::_putimages(); |
| 428 |
$this->_putformxobjects(); |
| 429 |
} |
| 430 |
|
| 431 |
function _putxobjectdict() { |
| 432 |
parent::_putxobjectdict(); |
| 433 |
|
| 434 |
if (count($this->tpls)) { |
| 435 |
foreach($this->tpls as $tplidx => $tpl) { |
| 436 |
$this->_out(sprintf('%s%d %d 0 R', $this->tplprefix, $tplidx, $tpl['n'])); |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
/** |
| 442 |
* Private Method |
| 443 |
*/ |
| 444 |
function _out($s) { |
| 445 |
if ($this->state == 2 && $this->_intpl) { |
| 446 |
$this->tpls[$this->tpl]['buffer'] .= $s . "\n"; |
| 447 |
} else { |
| 448 |
parent::_out($s); |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
|