PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/application/libraries/dompdf/include/tcpdf_adapter.cls.php

https://gitlab.com/devdoblea/factutextil.local
PHP | 498 lines | 115 code | 62 blank | 321 comment | 18 complexity | 3a73ad951fadba0a3c461c13c6723e67 MD5 | raw file
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile$
  6. * Created on: 2004-08-04
  7. *
  8. * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library in the file LICENSE.LGPL; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  23. * 02111-1307 USA
  24. *
  25. * Alternatively, you may distribute this software under the terms of the
  26. * PHP License, version 3.0 or later. A copy of this license should have
  27. * been distributed with this file in the file LICENSE.PHP . If this is not
  28. * the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
  29. *
  30. * The latest version of DOMPDF might be available at:
  31. * http://www.dompdf.com/
  32. *
  33. * @link http://www.dompdf.com/
  34. * @copyright 2004 Benj Carson
  35. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  36. * @package dompdf
  37. */
  38. /* $Id: tcpdf_adapter.cls.php 311 2010-09-05 20:02:01Z fabien.menager $ */
  39. require_once(DOMPDF_LIB_DIR . '/tcpdf/tcpdf.php');
  40. /**
  41. * TCPDF PDF Rendering interface
  42. *
  43. * TCPDF_Adapter provides a simple, stateless interface to TCPDF.
  44. *
  45. * Unless otherwise mentioned, all dimensions are in points (1/72 in).
  46. * The coordinate origin is in the top left corner and y values
  47. * increase downwards.
  48. *
  49. * See {@link http://tcpdf.sourceforge.net} for more information on
  50. * the underlying TCPDF class.
  51. *
  52. * @package dompdf
  53. */
  54. class TCPDF_Adapter implements Canvas {
  55. /**
  56. * Dimensions of paper sizes in points
  57. *
  58. * @var array;
  59. */
  60. static public $PAPER_SIZES = array(); // Set to
  61. // CPDF_Adapter::$PAPER_SIZES below.
  62. /**
  63. * Instance of the TCPDF class
  64. *
  65. * @var TCPDF
  66. */
  67. private $_pdf;
  68. /**
  69. * PDF width in points
  70. *
  71. * @var float
  72. */
  73. private $_width;
  74. /**
  75. * PDF height in points
  76. *
  77. * @var float
  78. */
  79. private $_height;
  80. /**
  81. * Last fill colour used
  82. *
  83. * @var array
  84. */
  85. private $_last_fill_color;
  86. /**
  87. * Last stroke colour used
  88. *
  89. * @var array
  90. */
  91. private $_last_stroke_color;
  92. /**
  93. * Last line width used
  94. *
  95. * @var float
  96. */
  97. private $_last_line_width;
  98. /**
  99. * Total number of pages
  100. *
  101. * @var int
  102. */
  103. private $_page_count;
  104. /**
  105. * Text to display on every page
  106. *
  107. * @var array
  108. */
  109. private $_page_text;
  110. /**
  111. * Array of pages for accessing after initial rendering is complete
  112. *
  113. * @var array
  114. */
  115. private $_pages;
  116. /**
  117. * Class constructor
  118. *
  119. * @param mixed $paper The size of paper to use either a string (see {@link CPDF_Adapter::$PAPER_SIZES}) or
  120. * an array(xmin,ymin,xmax,ymax)
  121. * @param string $orientation The orientation of the document (either 'landscape' or 'portrait')
  122. */
  123. function __construct($paper = "letter", $orientation = "portrait") {
  124. if ( is_array($paper) )
  125. $size = $paper;
  126. else if ( isset(self::$PAPER_SIZES[mb_strtolower($paper)]) )
  127. $size = self::$PAPER_SIZE[$paper];
  128. else
  129. $size = self::$PAPER_SIZE["letter"];
  130. if ( mb_strtolower($orientation) === "landscape" ) {
  131. list($size[2], $size[3]) = array($size[3], $size[2]);
  132. }
  133. $this->_width = $size[2] - $size[0];
  134. $this->_height = $size[3] - $size[1];
  135. $this->_pdf = new TCPDF("P", "pt", array($this->_width, $this->_height));
  136. $this->_pdf->Setcreator("DOMPDF Converter");
  137. $this->_pdf->AddPage();
  138. $this->_page_number = $this->_page_count = 1;
  139. $this->_page_text = array();
  140. $this->_last_fill_color =
  141. $this->_last_stroke_color =
  142. $this->_last_line_width = null;
  143. }
  144. /**
  145. * Remaps y coords from 4th to 1st quadrant
  146. *
  147. * @param float $y
  148. * @return float
  149. */
  150. protected function y($y) { return $this->_height - $y; }
  151. /**
  152. * Sets the stroke colour
  153. *
  154. * @param array $color
  155. */
  156. protected function _set_stroke_colour($colour) {
  157. $colour[0] = round(255 * $colour[0]);
  158. $colour[1] = round(255 * $colour[1]);
  159. $colour[2] = round(255 * $colour[2]);
  160. if ( is_null($this->_last_stroke_color) || $color != $this->_last_stroke_color ) {
  161. $this->_pdf->SetDrawColor($color[0],$color[1],$color[2]);
  162. $this->_last_stroke_color = $color;
  163. }
  164. }
  165. /**
  166. * Sets the fill colour
  167. *
  168. * @param array $color
  169. */
  170. protected function _set_fill_colour($colour) {
  171. $colour[0] = round(255 * $colour[0]);
  172. $colour[1] = round(255 * $colour[1]);
  173. $colour[2] = round(255 * $colour[2]);
  174. if ( is_null($this->_last_fill_color) || $color != $this->_last_fill_color ) {
  175. $this->_pdf->SetDrawColor($color[0],$color[1],$color[2]);
  176. $this->_last_fill_color = $color;
  177. }
  178. }
  179. /**
  180. * Return the TCPDF instance
  181. *
  182. * @return TCPDF
  183. */
  184. function get_tcpdf() { return $this->_pdf; }
  185. /**
  186. * Returns the current page number
  187. *
  188. * @return int
  189. */
  190. function get_page_number() {
  191. return $this->_page_number;
  192. }
  193. /**
  194. * Returns the total number of pages
  195. *
  196. * @return int
  197. */
  198. function get_page_count() {
  199. return $this->_page_count;
  200. }
  201. /**
  202. * Sets the total number of pages
  203. *
  204. * @param int $count
  205. */
  206. function set_page_count($count) {
  207. $this->_page_count = (int)$count;
  208. }
  209. /**
  210. * Draws a line from x1,y1 to x2,y2
  211. *
  212. * See {@link Style::munge_colour()} for the format of the colour array.
  213. * See {@link Cpdf::setLineStyle()} for a description of the format of the
  214. * $style parameter (aka dash).
  215. *
  216. * @param float $x1
  217. * @param float $y1
  218. * @param float $x2
  219. * @param float $y2
  220. * @param array $color
  221. * @param float $width
  222. * @param array $style
  223. */
  224. function line($x1, $y1, $x2, $y2, $color, $width, $style = null) {
  225. if ( is_null($this->_last_line_width) || $width != $this->_last_line_width ) {
  226. $this->_pdf->SetLineWidth($width);
  227. $this->_last_line_width = $width;
  228. }
  229. $this->_set_stroke_colour($color);
  230. // FIXME: ugh, need to handle different styles here
  231. $this->_pdf->line($x1, $y1, $x2, $y2);
  232. }
  233. /**
  234. * Draws a rectangle at x1,y1 with width w and height h
  235. *
  236. * See {@link Style::munge_colour()} for the format of the colour array.
  237. * See {@link Cpdf::setLineStyle()} for a description of the $style
  238. * parameter (aka dash)
  239. *
  240. * @param float $x1
  241. * @param float $y1
  242. * @param float $w
  243. * @param float $h
  244. * @param array $color
  245. * @param float $width
  246. * @param array $style
  247. */
  248. function rectangle($x1, $y1, $w, $h, $color, $width, $style = null) {
  249. if ( is_null($this->_last_line_width) || $width != $this->_last_line_width ) {
  250. $this->_pdf->SetLineWidth($width);
  251. $this->_last_line_width = $width;
  252. }
  253. $this->_set_stroke_colour($color);
  254. // FIXME: ugh, need to handle styles here
  255. $this->_pdf->rect($x1, $y1, $w, $h);
  256. }
  257. /**
  258. * Draws a filled rectangle at x1,y1 with width w and height h
  259. *
  260. * See {@link Style::munge_colour()} for the format of the colour array.
  261. *
  262. * @param float $x1
  263. * @param float $y1
  264. * @param float $w
  265. * @param float $h
  266. * @param array $color
  267. */
  268. function filled_rectangle($x1, $y1, $w, $h, $color) {
  269. $this->_set_fill_colour($color);
  270. // FIXME: ugh, need to handle styles here
  271. $this->_pdf->rect($x1, $y1, $w, $h, "F");
  272. }
  273. /**
  274. * Draws a polygon
  275. *
  276. * The polygon is formed by joining all the points stored in the $points
  277. * array. $points has the following structure:
  278. * <code>
  279. * array(0 => x1,
  280. * 1 => y1,
  281. * 2 => x2,
  282. * 3 => y2,
  283. * ...
  284. * );
  285. * </code>
  286. *
  287. * See {@link Style::munge_colour()} for the format of the colour array.
  288. * See {@link Cpdf::setLineStyle()} for a description of the $style
  289. * parameter (aka dash)
  290. *
  291. * @param array $points
  292. * @param array $color
  293. * @param float $width
  294. * @param array $style
  295. * @param bool $fill Fills the polygon if true
  296. */
  297. function polygon($points, $color, $width = null, $style = null, $fill = false) {
  298. // FIXME
  299. }
  300. /**
  301. * Draws a circle at $x,$y with radius $r
  302. *
  303. * See {@link Style::munge_colour()} for the format of the colour array.
  304. * See {@link Cpdf::setLineStyle()} for a description of the $style
  305. * parameter (aka dash)
  306. *
  307. * @param float $x
  308. * @param float $y
  309. * @param float $r
  310. * @param array $color
  311. * @param float $width
  312. * @param array $style
  313. * @param bool $fill Fills the circle if true
  314. */
  315. function circle($x, $y, $r, $color, $width = null, $style = null, $fill = false) {
  316. // FIXME
  317. }
  318. /**
  319. * Add an image to the pdf.
  320. *
  321. * The image is placed at the specified x and y coordinates with the
  322. * given width and height.
  323. *
  324. * @param string $img_url the path to the image
  325. * @param string $img_type the type (e.g. extension) of the image
  326. * @param float $x x position
  327. * @param float $y y position
  328. * @param int $w width (in pixels)
  329. * @param int $h height (in pixels)
  330. */
  331. function image($img_url, $img_type, $x, $y, $w, $h) {
  332. // FIXME
  333. }
  334. /**
  335. * Writes text at the specified x and y coordinates
  336. *
  337. * See {@link Style::munge_colour()} for the format of the colour array.
  338. *
  339. * @param float $x
  340. * @param float $y
  341. * @param string $text the text to write
  342. * @param string $font the font file to use
  343. * @param float $size the font size, in points
  344. * @param array $color
  345. * @param float $adjust word spacing adjustment
  346. */
  347. function text($x, $y, $text, $font, $size, $color = array(0,0,0), $adjust = 0) {
  348. // FIXME
  349. }
  350. function javascript($code) {
  351. // FIXME
  352. }
  353. /**
  354. * Add a named destination (similar to <a name="foo">...</a> in html)
  355. *
  356. * @param string $anchorname The name of the named destination
  357. */
  358. function add_named_dest($anchorname) {
  359. // FIXME
  360. }
  361. /**
  362. * Add a link to the pdf
  363. *
  364. * @param string $url The url to link to
  365. * @param float $x The x position of the link
  366. * @param float $y The y position of the link
  367. * @param float $width The width of the link
  368. * @param float $height The height of the link
  369. */
  370. function add_link($url, $x, $y, $width, $height) {
  371. // FIXME
  372. }
  373. /**
  374. * Add meta information to the PDF
  375. *
  376. * @param string $label label of the value (Creator, Producer, etc.)
  377. * @param string $value the text to set
  378. */
  379. function add_info($label, $value) {
  380. $method = "Set$label";
  381. if ( in_array("Title", "Author", "Keywords", "Subject") && method_exists($this->_pdf, $method) ) {
  382. $this->_pdf->$method($value);
  383. }
  384. }
  385. /**
  386. * Calculates text size, in points
  387. *
  388. * @param string $text the text to be sized
  389. * @param string $font the desired font
  390. * @param float $size the desired font size
  391. * @param float $spacing word spacing, if any
  392. * @return float
  393. */
  394. function get_text_width($text, $font, $size, $spacing = 0) {
  395. // FIXME
  396. }
  397. /**
  398. * Calculates font height, in points
  399. *
  400. * @param string $font
  401. * @param float $size
  402. * @return float
  403. */
  404. function get_font_height($font, $size) {
  405. // FIXME
  406. }
  407. /**
  408. * Starts a new page
  409. *
  410. * Subsequent drawing operations will appear on the new page.
  411. */
  412. function new_page() {
  413. // FIXME
  414. }
  415. /**
  416. * Streams the PDF directly to the browser
  417. *
  418. * @param string $filename the name of the PDF file
  419. * @param array $options associative array, 'Attachment' => 0 or 1, 'compress' => 1 or 0
  420. */
  421. function stream($filename, $options = null) {
  422. // FIXME
  423. }
  424. /**
  425. * Returns the PDF as a string
  426. *
  427. * @param array $options associative array: 'compress' => 1 or 0
  428. * @return string
  429. */
  430. function output($options = null) {
  431. // FIXME
  432. }
  433. }
  434. // Workaround for idiotic limitation on statics...
  435. PDFLib_Adapter::$PAPER_SIZES = CPDF_Adapter::$PAPER_SIZES;