FPDF_TPL

Class FPDF_TPL.

Defined (1)

The class is defined in the following location(s).

/vendor/setasign/fpdi/fpdf_tpl.php  
  1. class FPDF_TPL extends fpdi_bridge 
  2. /** 
  3. * Array of template data 
  4. * @var array 
  5. */ 
  6. protected $_tpls = array(); 
  7.  
  8. /** 
  9. * Current Template-Id 
  10. * @var int 
  11. */ 
  12. public $tpl = 0; 
  13.  
  14. /** 
  15. * "In Template"-Flag 
  16. * @var boolean 
  17. */ 
  18. protected $_inTpl = false; 
  19.  
  20. /** 
  21. * Name prefix of templates used in Resources dictionary 
  22. * @var string A String defining the Prefix used as Template-Object-Names. Have to begin with an / 
  23. */ 
  24. public $tplPrefix = "/TPL"; 
  25.  
  26. /** 
  27. * Resources used by templates and pages 
  28. * @var array 
  29. */ 
  30. protected $_res = array(); 
  31.  
  32. /** 
  33. * Last used template data 
  34. * @var array 
  35. */ 
  36. public $lastUsedTemplateData = array(); 
  37.  
  38. /** 
  39. * Start a template. 
  40. * This method starts a template. You can give own coordinates to build an own sized 
  41. * template. Pay attention, that the margins are adapted to the new template size. 
  42. * If you want to write outside the template, for example to build a clipped template,  
  43. * you have to set the margins and "cursor"-position manual after beginTemplate()-call. 
  44. * If no parameter is given, the template uses the current page-size. 
  45. * The method returns an id of the current template. This id is used later for using this template. 
  46. * Warning: A created template is saved in the resulting PDF at all events. Also if you don't use it after creation! 
  47. * @param int $x The x-coordinate given in user-unit 
  48. * @param int $y The y-coordinate given in user-unit 
  49. * @param int $w The width given in user-unit 
  50. * @param int $h The height given in user-unit 
  51. * @return int The id of new created template 
  52. * @throws LogicException 
  53. */ 
  54. public function beginTemplate($x = null, $y = null, $w = null, $h = null) 
  55. if (is_subclass_of($this, 'TCPDF')) { 
  56. throw new LogicException('This method is only usable with FPDF. Use TCPDF methods startTemplate() instead.'); 
  57.  
  58. if ($this->page <= 0) { 
  59. throw new LogicException("You have to add at least a page first!"); 
  60.  
  61. if ($x == null) 
  62. $x = 0; 
  63. if ($y == null) 
  64. $y = 0; 
  65. if ($w == null) 
  66. $w = $this->w; 
  67. if ($h == null) 
  68. $h = $this->h; 
  69.  
  70. // Save settings 
  71. $this->tpl++; 
  72. $tpl =& $this->_tpls[$this->tpl]; 
  73. $tpl = array( 
  74. 'o_x' => $this->x,  
  75. 'o_y' => $this->y,  
  76. 'o_AutoPageBreak' => $this->AutoPageBreak,  
  77. 'o_bMargin' => $this->bMargin,  
  78. 'o_tMargin' => $this->tMargin,  
  79. 'o_lMargin' => $this->lMargin,  
  80. 'o_rMargin' => $this->rMargin,  
  81. 'o_h' => $this->h,  
  82. 'o_w' => $this->w,  
  83. 'o_FontFamily' => $this->FontFamily,  
  84. 'o_FontStyle' => $this->FontStyle,  
  85. 'o_FontSizePt' => $this->FontSizePt,  
  86. 'o_FontSize' => $this->FontSize,  
  87. 'buffer' => '',  
  88. 'x' => $x,  
  89. 'y' => $y,  
  90. 'w' => $w,  
  91. 'h' => $h 
  92. ); 
  93.  
  94. $this->SetAutoPageBreak(false); 
  95.  
  96. // Define own high and width to calculate correct positions 
  97. $this->h = $h; 
  98. $this->w = $w; 
  99.  
  100. $this->_inTpl = true; 
  101. $this->SetXY($x + $this->lMargin, $y + $this->tMargin); 
  102. $this->SetRightMargin($this->w - $w + $this->rMargin); 
  103.  
  104. if ($this->CurrentFont) { 
  105. $fontKey = $this->FontFamily . $this->FontStyle; 
  106. if ($fontKey) { 
  107. $this->_res['tpl'][$this->tpl]['fonts'][$fontKey] =& $this->fonts[$fontKey]; 
  108. $this->_out(sprintf('BT /F%d %.2F Tf ET', $this->CurrentFont['i'], $this->FontSizePt)); 
  109.  
  110. return $this->tpl; 
  111.  
  112. /** 
  113. * End template. 
  114. * This method ends a template and reset initiated variables collected in {@link beginTemplate()}. 
  115. * @return int|boolean If a template is opened, the id is returned. If not a false is returned. 
  116. */ 
  117. public function endTemplate() 
  118. if (is_subclass_of($this, 'TCPDF')) { 
  119. $args = func_get_args(); 
  120. return call_user_func_array(array($this, 'TCPDF::endTemplate'), $args); 
  121.  
  122. if ($this->_inTpl) { 
  123. $this->_inTpl = false; 
  124. $tpl = $this->_tpls[$this->tpl]; 
  125. $this->SetXY($tpl['o_x'], $tpl['o_y']); 
  126. $this->tMargin = $tpl['o_tMargin']; 
  127. $this->lMargin = $tpl['o_lMargin']; 
  128. $this->rMargin = $tpl['o_rMargin']; 
  129. $this->h = $tpl['o_h']; 
  130. $this->w = $tpl['o_w']; 
  131. $this->SetAutoPageBreak($tpl['o_AutoPageBreak'], $tpl['o_bMargin']); 
  132.  
  133. $this->FontFamily = $tpl['o_FontFamily']; 
  134. $this->FontStyle = $tpl['o_FontStyle']; 
  135. $this->FontSizePt = $tpl['o_FontSizePt']; 
  136. $this->FontSize = $tpl['o_FontSize']; 
  137.  
  138. $fontKey = $this->FontFamily . $this->FontStyle; 
  139. if ($fontKey) 
  140. $this->CurrentFont =& $this->fonts[$fontKey]; 
  141.  
  142. return $this->tpl; 
  143. } else { 
  144. return false; 
  145.  
  146. /** 
  147. * Use a template in current page or other template. 
  148. * You can use a template in a page or in another template. 
  149. * You can give the used template a new size. 
  150. * All parameters are optional. The width or height is calculated automatically 
  151. * if one is given. If no parameter is given the origin size as defined in 
  152. * {@link beginTemplate()} method is used. 
  153. * The calculated or used width and height are returned as an array. 
  154. * @param int $tplIdx A valid template-id 
  155. * @param int $x The x-position 
  156. * @param int $y The y-position 
  157. * @param int $w The new width of the template 
  158. * @param int $h The new height of the template 
  159. * @return array The height and width of the template (array('w' => ..., 'h' => ...)) 
  160. * @throws LogicException|InvalidArgumentException 
  161. */ 
  162. public function useTemplate($tplIdx, $x = null, $y = null, $w = 0, $h = 0) 
  163. if ($this->page <= 0) { 
  164. throw new LogicException('You have to add at least a page first!'); 
  165.  
  166. if (!isset($this->_tpls[$tplIdx])) { 
  167. throw new InvalidArgumentException('Template does not exist!'); 
  168.  
  169. if ($this->_inTpl) { 
  170. $this->_res['tpl'][$this->tpl]['tpls'][$tplIdx] =& $this->_tpls[$tplIdx]; 
  171.  
  172. $tpl = $this->_tpls[$tplIdx]; 
  173. $_w = $tpl['w']; 
  174. $_h = $tpl['h']; 
  175.  
  176. if ($x == null) { 
  177. $x = 0; 
  178.  
  179. if ($y == null) { 
  180. $y = 0; 
  181.  
  182. $x += $tpl['x']; 
  183. $y += $tpl['y']; 
  184.  
  185. $wh = $this->getTemplateSize($tplIdx, $w, $h); 
  186. $w = $wh['w']; 
  187. $h = $wh['h']; 
  188.  
  189. $tplData = array( 
  190. 'x' => $this->x,  
  191. 'y' => $this->y,  
  192. 'w' => $w,  
  193. 'h' => $h,  
  194. 'scaleX' => ($w / $_w),  
  195. 'scaleY' => ($h / $_h),  
  196. 'tx' => $x,  
  197. 'ty' => ($this->h - $y - $h),  
  198. 'lty' => ($this->h - $y - $h) - ($this->h - $_h) * ($h / $_h) 
  199. ); 
  200.  
  201. $this->_out(sprintf('q %.4F 0 0 %.4F %.4F %.4F cm',  
  202. $tplData['scaleX'], $tplData['scaleY'], $tplData['tx'] * $this->k, $tplData['ty'] * $this->k) 
  203. ); // Translate 
  204. $this->_out(sprintf('%s%d Do Q', $this->tplPrefix, $tplIdx)); 
  205.  
  206. $this->lastUsedTemplateData = $tplData; 
  207.  
  208. return array('w' => $w, 'h' => $h); 
  209.  
  210. /** 
  211. * Get the calculated size of a template. 
  212. * If one size is given, this method calculates the other one. 
  213. * @param int $tplIdx A valid template-id 
  214. * @param int $w The width of the template 
  215. * @param int $h The height of the template 
  216. * @return array The height and width of the template (array('w' => ..., 'h' => ...)) 
  217. */ 
  218. public function getTemplateSize($tplIdx, $w = 0, $h = 0) 
  219. if (!isset($this->_tpls[$tplIdx])) 
  220. return false; 
  221.  
  222. $tpl = $this->_tpls[$tplIdx]; 
  223. $_w = $tpl['w']; 
  224. $_h = $tpl['h']; 
  225.  
  226. if ($w == 0 && $h == 0) { 
  227. $w = $_w; 
  228. $h = $_h; 
  229.  
  230. if ($w == 0) 
  231. $w = $h * $_w / $_h; 
  232. if($h == 0) 
  233. $h = $w * $_h / $_w; 
  234.  
  235. return array("w" => $w, "h" => $h); 
  236.  
  237. /** 
  238. * Sets the font used to print character strings. 
  239. * See FPDF/TCPDF documentation. 
  240. * @see http://fpdf.org/en/doc/setfont.htm 
  241. * @see http://www.tcpdf.org/doc/code/classTCPDF.html#afd56e360c43553830d543323e81bc045 
  242. */ 
  243. public function SetFont($family, $style = '', $size = null, $fontfile = '', $subset = 'default', $out = true) 
  244. if (is_subclass_of($this, 'TCPDF')) { 
  245. $args = func_get_args(); 
  246. return call_user_func_array(array($this, 'TCPDF::SetFont'), $args); 
  247.  
  248. parent::SetFont($family, $style, $size); 
  249.  
  250. $fontkey = $this->FontFamily . $this->FontStyle; 
  251.  
  252. if ($this->_inTpl) { 
  253. $this->_res['tpl'][$this->tpl]['fonts'][$fontkey] =& $this->fonts[$fontkey]; 
  254. } else { 
  255. $this->_res['page'][$this->page]['fonts'][$fontkey] =& $this->fonts[$fontkey]; 
  256.  
  257. /** 
  258. * Puts an image. 
  259. * See FPDF/TCPDF documentation. 
  260. * @see http://fpdf.org/en/doc/image.htm 
  261. * @see http://www.tcpdf.org/doc/code/classTCPDF.html#a714c2bee7d6b39d4d6d304540c761352 
  262. */ 
  263. public function Image( 
  264. $file, $x = '', $y = '', $w = 0, $h = 0, $type = '', $link = '', $align = '', $resize = false,  
  265. $dpi = 300, $palign = '', $ismask = false, $imgmask = false, $border = 0, $fitbox = false,  
  266. $hidden = false, $fitonpage = false, $alt = false, $altimgs = array() 
  267. if (is_subclass_of($this, 'TCPDF')) { 
  268. $args = func_get_args(); 
  269. return call_user_func_array(array($this, 'TCPDF::Image'), $args); 
  270.  
  271. $ret = parent::Image($file, $x, $y, $w, $h, $type, $link); 
  272. if ($this->_inTpl) { 
  273. $this->_res['tpl'][$this->tpl]['images'][$file] =& $this->images[$file]; 
  274. } else { 
  275. $this->_res['page'][$this->page]['images'][$file] =& $this->images[$file]; 
  276.  
  277. return $ret; 
  278.  
  279. /** 
  280. * Adds a new page to the document. 
  281. * See FPDF/TCPDF documentation. 
  282. * This method cannot be used if you'd started a template. 
  283. * @see http://fpdf.org/en/doc/addpage.htm 
  284. * @see http://www.tcpdf.org/doc/code/classTCPDF.html#a5171e20b366b74523709d84c349c1ced 
  285. */ 
  286. public function AddPage($orientation = '', $format = '', $rotationOrKeepmargins = false, $tocpage = false) 
  287. if (is_subclass_of($this, 'TCPDF')) { 
  288. $args = func_get_args(); 
  289. return call_user_func_array(array($this, 'TCPDF::AddPage'), $args); 
  290.  
  291. if ($this->_inTpl) { 
  292. throw new LogicException('Adding pages in templates is not possible!'); 
  293.  
  294. parent::AddPage($orientation, $format, $rotationOrKeepmargins); 
  295.  
  296. /** 
  297. * Puts a link on a rectangular area of the page. 
  298. * Overwritten because adding links in a template will not work. 
  299. * @see http://fpdf.org/en/doc/link.htm 
  300. * @see http://www.tcpdf.org/doc/code/classTCPDF.html#ab87bf1826384fbfe30eb499d42f1d994 
  301. */ 
  302. public function Link($x, $y, $w, $h, $link, $spaces = 0) 
  303. if (is_subclass_of($this, 'TCPDF')) { 
  304. $args = func_get_args(); 
  305. return call_user_func_array(array($this, 'TCPDF::Link'), $args); 
  306.  
  307. if ($this->_inTpl) { 
  308. throw new LogicException('Using links in templates is not posible!'); 
  309.  
  310. parent::Link($x, $y, $w, $h, $link); 
  311.  
  312. /** 
  313. * Creates a new internal link and returns its identifier. 
  314. * Overwritten because adding links in a template will not work. 
  315. * @see http://fpdf.org/en/doc/addlink.htm 
  316. * @see http://www.tcpdf.org/doc/code/classTCPDF.html#a749522038ed7786c3e1701435dcb891e 
  317. */ 
  318. public function AddLink() 
  319. if (is_subclass_of($this, 'TCPDF')) { 
  320. $args = func_get_args(); 
  321. return call_user_func_array(array($this, 'TCPDF::AddLink'), $args); 
  322.  
  323. if ($this->_inTpl) { 
  324. throw new LogicException('Adding links in templates is not possible!'); 
  325.  
  326. return parent::AddLink(); 
  327.  
  328. /** 
  329. * Defines the page and position a link points to. 
  330. * Overwritten because adding links in a template will not work. 
  331. * @see http://fpdf.org/en/doc/setlink.htm 
  332. * @see http://www.tcpdf.org/doc/code/classTCPDF.html#ace5be60e7857953ea5e2b89cb90df0ae 
  333. */ 
  334. public function SetLink($link, $y = 0, $page = -1) 
  335. if (is_subclass_of($this, 'TCPDF')) { 
  336. $args = func_get_args(); 
  337. return call_user_func_array(array($this, 'TCPDF::SetLink'), $args); 
  338.  
  339. if ($this->_inTpl) { 
  340. throw new LogicException('Setting links in templates is not possible!'); 
  341.  
  342. parent::SetLink($link, $y, $page); 
  343.  
  344. /** 
  345. * Writes the form XObjects to the PDF document. 
  346. */ 
  347. protected function _putformxobjects() 
  348. $filter=($this->compress) ? '/Filter /FlateDecode ' : ''; 
  349. reset($this->_tpls); 
  350.  
  351. foreach($this->_tpls AS $tplIdx => $tpl) { 
  352. $this->_newobj(); 
  353. $this->_tpls[$tplIdx]['n'] = $this->n; 
  354. $this->_out('<<'.$filter.'/Type /XObject'); 
  355. $this->_out('/Subtype /Form'); 
  356. $this->_out('/FormType 1'); 
  357. $this->_out(sprintf('/BBox [%.2F %.2F %.2F %.2F]',  
  358. // llx 
  359. $tpl['x'] * $this->k,  
  360. // lly 
  361. -$tpl['y'] * $this->k,  
  362. // urx 
  363. ($tpl['w'] + $tpl['x']) * $this->k,  
  364. // ury 
  365. ($tpl['h'] - $tpl['y']) * $this->k 
  366. )); 
  367.  
  368. if ($tpl['x'] != 0 || $tpl['y'] != 0) { 
  369. $this->_out(sprintf('/Matrix [1 0 0 1 %.5F %.5F]',  
  370. -$tpl['x'] * $this->k * 2, $tpl['y'] * $this->k * 2 
  371. )); 
  372.  
  373. $this->_out('/Resources '); 
  374. $this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]'); 
  375.  
  376. if (isset($this->_res['tpl'][$tplIdx])) { 
  377. $res = $this->_res['tpl'][$tplIdx]; 
  378. if (isset($res['fonts']) && count($res['fonts'])) { 
  379. $this->_out('/Font <<'); 
  380.  
  381. foreach($res['fonts'] as $font) { 
  382. $this->_out('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R'); 
  383.  
  384. $this->_out('>>'); 
  385.  
  386. if(isset($res['images']) || isset($res['tpls'])) { 
  387. $this->_out('/XObject <<'); 
  388.  
  389. if (isset($res['images'])) { 
  390. foreach($res['images'] as $image) 
  391. $this->_out('/I' . $image['i'] . ' ' . $image['n'] . ' 0 R'); 
  392.  
  393. if (isset($res['tpls'])) { 
  394. foreach($res['tpls'] as $i => $_tpl) 
  395. $this->_out($this->tplPrefix . $i . ' ' . $_tpl['n'] . ' 0 R'); 
  396.  
  397. $this->_out('>>'); 
  398.  
  399. $this->_out('>>'); 
  400.  
  401. $buffer = ($this->compress) ? gzcompress($tpl['buffer']) : $tpl['buffer']; 
  402. $this->_out('/Length ' . strlen($buffer) . ' >>'); 
  403. $this->_putstream($buffer); 
  404. $this->_out('endobj'); 
  405.  
  406. /** 
  407. * Output images. 
  408. * Overwritten to add {@link _putformxobjects()} after _putimages(). 
  409. */ 
  410. public function _putimages() 
  411. parent::_putimages(); 
  412. $this->_putformxobjects(); 
  413.  
  414. /** 
  415. * Writes the references of XObject resources to the document. 
  416. * Overwritten to add the the templates to the XObject resource dictionary. 
  417. */ 
  418. public function _putxobjectdict() 
  419. parent::_putxobjectdict(); 
  420.  
  421. foreach($this->_tpls as $tplIdx => $tpl) { 
  422. $this->_out(sprintf('%s%d %d 0 R', $this->tplPrefix, $tplIdx, $tpl['n'])); 
  423.  
  424. /** 
  425. * Writes bytes to the resulting document. 
  426. * Overwritten to delegate the data to the template buffer. 
  427. * @param string $s 
  428. */ 
  429. public function _out($s) 
  430. if ($this->state == 2 && $this->_inTpl) { 
  431. $this->_tpls[$this->tpl]['buffer'] .= $s . "\n"; 
  432. } else { 
  433. parent::_out($s);