PageRenderTime 27ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/data/module/fpdi/fpdf_tpl.php

https://gitlab.com/raku.takayama/eccube-2_13
PHP | 449 lines | 259 code | 62 blank | 128 comment | 63 complexity | d9c124823d7715c1ae6859d43d30b046 MD5 | raw file
  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. class FPDF_TPL extends FPDF {
  20. /**
  21. * Array of Tpl-Data
  22. * @var array
  23. */
  24. var $tpls = array();
  25. /**
  26. * Current Template-ID
  27. * @var int
  28. */
  29. var $tpl = 0;
  30. /**
  31. * "In Template"-Flag
  32. * @var boolean
  33. */
  34. var $_intpl = false;
  35. /**
  36. * Nameprefix of Templates used in Resources-Dictonary
  37. * @var string A String defining the Prefix used as Template-Object-Names. Have to beginn with an /
  38. */
  39. var $tplprefix = "/TPL";
  40. /**
  41. * Resources used By Templates and Pages
  42. * @var array
  43. */
  44. var $_res = array();
  45. /**
  46. * Last used Template data
  47. *
  48. * @var array
  49. */
  50. var $lastUsedTemplateData = array();
  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 (is_subclass_of($this, 'TCPDF')) {
  71. $this->Error('This method is only usable with FPDF. Use TCPDF methods startTemplate() instead.');
  72. return;
  73. }
  74. if ($this->page <= 0)
  75. $this->error("You have to add a page to fpdf first!");
  76. if ($x == null)
  77. $x = 0;
  78. if ($y == null)
  79. $y = 0;
  80. if ($w == null)
  81. $w = $this->w;
  82. if ($h == null)
  83. $h = $this->h;
  84. // Save settings
  85. $this->tpl++;
  86. $tpl =& $this->tpls[$this->tpl];
  87. $tpl = array(
  88. 'o_x' => $this->x,
  89. 'o_y' => $this->y,
  90. 'o_AutoPageBreak' => $this->AutoPageBreak,
  91. 'o_bMargin' => $this->bMargin,
  92. 'o_tMargin' => $this->tMargin,
  93. 'o_lMargin' => $this->lMargin,
  94. 'o_rMargin' => $this->rMargin,
  95. 'o_h' => $this->h,
  96. 'o_w' => $this->w,
  97. 'buffer' => '',
  98. 'x' => $x,
  99. 'y' => $y,
  100. 'w' => $w,
  101. 'h' => $h
  102. );
  103. $this->SetAutoPageBreak(false);
  104. // Define own high and width to calculate possitions correct
  105. $this->h = $h;
  106. $this->w = $w;
  107. $this->_intpl = true;
  108. $this->SetXY($x + $this->lMargin, $y + $this->tMargin);
  109. $this->SetRightMargin($this->w - $w + $this->rMargin);
  110. return $this->tpl;
  111. }
  112. /**
  113. * End Template
  114. *
  115. * This method ends a template and reset initiated variables on beginTemplate.
  116. *
  117. * @return mixed If a template is opened, the ID is returned. If not a false is returned.
  118. */
  119. function endTemplate() {
  120. if (is_subclass_of($this, 'TCPDF')) {
  121. $args = func_get_args();
  122. return call_user_func_array(array($this, 'TCPDF::endTemplate'), $args);
  123. }
  124. if ($this->_intpl) {
  125. $this->_intpl = false;
  126. $tpl =& $this->tpls[$this->tpl];
  127. $this->SetXY($tpl['o_x'], $tpl['o_y']);
  128. $this->tMargin = $tpl['o_tMargin'];
  129. $this->lMargin = $tpl['o_lMargin'];
  130. $this->rMargin = $tpl['o_rMargin'];
  131. $this->h = $tpl['o_h'];
  132. $this->w = $tpl['o_w'];
  133. $this->SetAutoPageBreak($tpl['o_AutoPageBreak'], $tpl['o_bMargin']);
  134. return $this->tpl;
  135. } else {
  136. return false;
  137. }
  138. }
  139. /**
  140. * Use a Template in current Page or other Template
  141. *
  142. * You can use a template in a page or in another template.
  143. * You can give the used template a new size like you use the Image()-method.
  144. * All parameters are optional. The width or height is calculated automaticaly
  145. * if one is given. If no parameter is given the origin size as defined in
  146. * beginTemplate() is used.
  147. * The calculated or used width and height are returned as an array.
  148. *
  149. * @param int $tplidx A valid template-Id
  150. * @param int $_x The x-position
  151. * @param int $_y The y-position
  152. * @param int $_w The new width of the template
  153. * @param int $_h The new height of the template
  154. * @retrun array The height and width of the template
  155. */
  156. function useTemplate($tplidx, $_x = null, $_y = null, $_w = 0, $_h = 0) {
  157. if ($this->page <= 0)
  158. $this->error('You have to add a page first!');
  159. if (!isset($this->tpls[$tplidx]))
  160. $this->error('Template does not exist!');
  161. if ($this->_intpl) {
  162. $this->_res['tpl'][$this->tpl]['tpls'][$tplidx] =& $this->tpls[$tplidx];
  163. }
  164. $tpl =& $this->tpls[$tplidx];
  165. $w = $tpl['w'];
  166. $h = $tpl['h'];
  167. if ($_x == null)
  168. $_x = 0;
  169. if ($_y == null)
  170. $_y = 0;
  171. $_x += $tpl['x'];
  172. $_y += $tpl['y'];
  173. $wh = $this->getTemplateSize($tplidx, $_w, $_h);
  174. $_w = $wh['w'];
  175. $_h = $wh['h'];
  176. $tData = array(
  177. 'x' => $this->x,
  178. 'y' => $this->y,
  179. 'w' => $_w,
  180. 'h' => $_h,
  181. 'scaleX' => ($_w / $w),
  182. 'scaleY' => ($_h / $h),
  183. 'tx' => $_x,
  184. 'ty' => ($this->h - $_y - $_h),
  185. 'lty' => ($this->h - $_y - $_h) - ($this->h - $h) * ($_h / $h)
  186. );
  187. $this->_out(sprintf('q %.4F 0 0 %.4F %.4F %.4F cm', $tData['scaleX'], $tData['scaleY'], $tData['tx'] * $this->k, $tData['ty'] * $this->k)); // Translate
  188. $this->_out(sprintf('%s%d Do Q', $this->tplprefix, $tplidx));
  189. // reset font in the outer graphic state
  190. if ($this->FontFamily) {
  191. $family = $this->FontFamily;
  192. $this->FontFamily = '';
  193. $this->SetFont($family);
  194. }
  195. $this->lastUsedTemplateData = $tData;
  196. return array('w' => $_w, 'h' => $_h);
  197. }
  198. /**
  199. * Get The calculated Size of a Template
  200. *
  201. * If one size is given, this method calculates the other one.
  202. *
  203. * @param int $tplidx A valid template-Id
  204. * @param int $_w The width of the template
  205. * @param int $_h The height of the template
  206. * @return array The height and width of the template
  207. */
  208. function getTemplateSize($tplidx, $_w = 0, $_h = 0) {
  209. if (!$this->tpls[$tplidx])
  210. return false;
  211. $tpl =& $this->tpls[$tplidx];
  212. $w = $tpl['w'];
  213. $h = $tpl['h'];
  214. if ($_w == 0 and $_h == 0) {
  215. $_w = $w;
  216. $_h = $h;
  217. }
  218. if($_w == 0)
  219. $_w = $_h * $w / $h;
  220. if($_h == 0)
  221. $_h = $_w * $h / $w;
  222. return array("w" => $_w, "h" => $_h);
  223. }
  224. /**
  225. * See FPDF/TCPDF-Documentation ;-)
  226. */
  227. function SetFont($family, $style = '', $size = 0) {
  228. if (is_subclass_of($this, 'TCPDF')) {
  229. $args = func_get_args();
  230. return call_user_func_array(array($this, 'TCPDF::SetFont'), $args);
  231. }
  232. /**
  233. * force the resetting of font changes in a template
  234. */
  235. if ($this->_intpl)
  236. $this->FontFamily = '';
  237. parent::SetFont($family, $style, $size);
  238. $fontkey = $this->FontFamily . $this->FontStyle;
  239. if ($this->_intpl) {
  240. $this->_res['tpl'][$this->tpl]['fonts'][$fontkey] =& $this->fonts[$fontkey];
  241. } else {
  242. $this->_res['page'][$this->page]['fonts'][$fontkey] =& $this->fonts[$fontkey];
  243. }
  244. }
  245. /**
  246. * See FPDF/TCPDF-Documentation ;-)
  247. */
  248. function Image($file, $x = null, $y = null, $w = 0, $h = 0, $type = '', $link = '') {
  249. if (is_subclass_of($this, 'TCPDF')) {
  250. $args = func_get_args();
  251. return call_user_func_array(array($this, 'TCPDF::Image'), $args);
  252. }
  253. $ret = parent::Image($file, $x, $y, $w, $h, $type, $link);
  254. if ($this->_intpl) {
  255. $this->_res['tpl'][$this->tpl]['images'][$file] =& $this->images[$file];
  256. } else {
  257. $this->_res['page'][$this->page]['images'][$file] =& $this->images[$file];
  258. }
  259. return $ret;
  260. }
  261. /**
  262. * See FPDF-Documentation ;-)
  263. *
  264. * AddPage is not available when you're "in" a template.
  265. */
  266. function AddPage($orientation = '', $format = '') {
  267. if (is_subclass_of($this, 'TCPDF')) {
  268. $args = func_get_args();
  269. return call_user_func_array(array($this, 'TCPDF::AddPage'), $args);
  270. }
  271. if ($this->_intpl)
  272. $this->Error('Adding pages in templates isn\'t possible!');
  273. parent::AddPage($orientation, $format);
  274. }
  275. /**
  276. * Preserve adding Links in Templates ...won't work
  277. */
  278. function Link($x, $y, $w, $h, $link) {
  279. if (is_subclass_of($this, 'TCPDF')) {
  280. $args = func_get_args();
  281. return call_user_func_array(array($this, 'TCPDF::Link'), $args);
  282. }
  283. if ($this->_intpl)
  284. $this->Error('Using links in templates aren\'t possible!');
  285. parent::Link($x, $y, $w, $h, $link);
  286. }
  287. function AddLink() {
  288. if (is_subclass_of($this, 'TCPDF')) {
  289. $args = func_get_args();
  290. return call_user_func_array(array($this, 'TCPDF::AddLink'), $args);
  291. }
  292. if ($this->_intpl)
  293. $this->Error('Adding links in templates aren\'t possible!');
  294. return parent::AddLink();
  295. }
  296. function SetLink($link, $y = 0, $page = -1) {
  297. if (is_subclass_of($this, 'TCPDF')) {
  298. $args = func_get_args();
  299. return call_user_func_array(array($this, 'TCPDF::SetLink'), $args);
  300. }
  301. if ($this->_intpl)
  302. $this->Error('Setting links in templates aren\'t possible!');
  303. parent::SetLink($link, $y, $page);
  304. }
  305. /**
  306. * Private Method that writes the form xobjects
  307. */
  308. function _putformxobjects() {
  309. $filter=($this->compress) ? '/Filter /FlateDecode ' : '';
  310. reset($this->tpls);
  311. foreach($this->tpls AS $tplidx => $tpl) {
  312. $p=($this->compress) ? gzcompress($tpl['buffer']) : $tpl['buffer'];
  313. $this->_newobj();
  314. $this->tpls[$tplidx]['n'] = $this->n;
  315. $this->_out('<<'.$filter.'/Type /XObject');
  316. $this->_out('/Subtype /Form');
  317. $this->_out('/FormType 1');
  318. $this->_out(sprintf('/BBox [%.2F %.2F %.2F %.2F]',
  319. // llx
  320. $tpl['x'] * $this->k,
  321. // lly
  322. -$tpl['y'] * $this->k,
  323. // urx
  324. ($tpl['w'] + $tpl['x']) * $this->k,
  325. // ury
  326. ($tpl['h'] - $tpl['y']) * $this->k
  327. ));
  328. if ($tpl['x'] != 0 || $tpl['y'] != 0) {
  329. $this->_out(sprintf('/Matrix [1 0 0 1 %.5F %.5F]',
  330. -$tpl['x'] * $this->k * 2, $tpl['y'] * $this->k * 2
  331. ));
  332. }
  333. $this->_out('/Resources ');
  334. $this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
  335. if (isset($this->_res['tpl'][$tplidx]['fonts']) && count($this->_res['tpl'][$tplidx]['fonts'])) {
  336. $this->_out('/Font <<');
  337. foreach($this->_res['tpl'][$tplidx]['fonts'] as $font)
  338. $this->_out('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R');
  339. $this->_out('>>');
  340. }
  341. if(isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images']) ||
  342. isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls']))
  343. {
  344. $this->_out('/XObject <<');
  345. if (isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images'])) {
  346. foreach($this->_res['tpl'][$tplidx]['images'] as $image)
  347. $this->_out('/I' . $image['i'] . ' ' . $image['n'] . ' 0 R');
  348. }
  349. if (isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls'])) {
  350. foreach($this->_res['tpl'][$tplidx]['tpls'] as $i => $tpl)
  351. $this->_out($this->tplprefix . $i . ' ' . $tpl['n'] . ' 0 R');
  352. }
  353. $this->_out('>>');
  354. }
  355. $this->_out('>>');
  356. $this->_out('/Length ' . strlen($p) . ' >>');
  357. $this->_putstream($p);
  358. $this->_out('endobj');
  359. }
  360. }
  361. /**
  362. * Overwritten to add _putformxobjects() after _putimages()
  363. *
  364. */
  365. function _putimages() {
  366. parent::_putimages();
  367. $this->_putformxobjects();
  368. }
  369. function _putxobjectdict() {
  370. parent::_putxobjectdict();
  371. if (count($this->tpls)) {
  372. foreach($this->tpls as $tplidx => $tpl) {
  373. $this->_out(sprintf('%s%d %d 0 R', $this->tplprefix, $tplidx, $tpl['n']));
  374. }
  375. }
  376. }
  377. /**
  378. * Private Method
  379. */
  380. function _out($s) {
  381. if ($this->state == 2 && $this->_intpl) {
  382. $this->tpls[$this->tpl]['buffer'] .= $s . "\n";
  383. } else {
  384. parent::_out($s);
  385. }
  386. }
  387. }