PageRenderTime 64ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/app/protected/extensions/pdf/dompdf/include/tcpdf_adapter.cls.php

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