fpdf_tpl.php 12 KB

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