tcpdf_adapter.cls.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 217 2010-03-11 23:03:57Z ryan.masten $ */
  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. $a = $size[3];
  132. $size[3] = $size[2];
  133. $size[2] = $a;
  134. }
  135. $this->_width = $size[2] - $size[0];
  136. $this->_height = $size[3] - $size[1];
  137. $this->_pdf = new TCPDF("P", "pt", array($this->_width, $this->_height));
  138. $this->_pdf->Setcreator("DOMPDF Converter");
  139. $this->_pdf->AddPage();
  140. $this->_page_number = $this->_page_count = 1;
  141. $this->_page_text = array();
  142. $this->_last_fill_color =
  143. $this->_last_stroke_color =
  144. $this->_last_line_width = null;
  145. }
  146. /**
  147. * Remaps y coords from 4th to 1st quadrant
  148. *
  149. * @param float $y
  150. * @return float
  151. */
  152. protected function y($y) { return $this->_height - $y; }
  153. /**
  154. * Sets the stroke colour
  155. *
  156. * @param array $color
  157. */
  158. protected function _set_stroke_colour($colour) {
  159. $colour[0] = round(255 * $colour[0]);
  160. $colour[1] = round(255 * $colour[1]);
  161. $colour[2] = round(255 * $colour[2]);
  162. if ( is_null($this->_last_stroke_color) || $color != $this->_last_stroke_color ) {
  163. $this->_pdf->SetDrawColor($color[0],$color[1],$color[2]);
  164. $this->_last_stroke_color = $color;
  165. }
  166. }
  167. /**
  168. * Sets the fill colour
  169. *
  170. * @param array $color
  171. */
  172. protected function _set_fill_colour($colour) {
  173. $colour[0] = round(255 * $colour[0]);
  174. $colour[1] = round(255 * $colour[1]);
  175. $colour[2] = round(255 * $colour[2]);
  176. if ( is_null($this->_last_fill_color) || $color != $this->_last_fill_color ) {
  177. $this->_pdf->SetDrawColor($color[0],$color[1],$color[2]);
  178. $this->_last_fill_color = $color;
  179. }
  180. }
  181. /**
  182. * Return the TCPDF instance
  183. *
  184. * @return TCPDF
  185. */
  186. function get_tcpdf() { return $this->_pdf; }
  187. /**
  188. * Returns the current page number
  189. *
  190. * @return int
  191. */
  192. function get_page_number() {
  193. return $this->_page_number;
  194. }
  195. /**
  196. * Returns the total number of pages
  197. *
  198. * @return int
  199. */
  200. function get_page_count() {
  201. return $this->_page_count;
  202. }
  203. /**
  204. * Sets the total number of pages
  205. *
  206. * @param int $count
  207. */
  208. function set_page_count($count) {
  209. $this->_page_count = (int)$count;
  210. }
  211. /**
  212. * Draws a line from x1,y1 to x2,y2
  213. *
  214. * See {@link Style::munge_colour()} for the format of the colour array.
  215. * See {@link Cpdf::setLineStyle()} for a description of the format of the
  216. * $style parameter (aka dash).
  217. *
  218. * @param float $x1
  219. * @param float $y1
  220. * @param float $x2
  221. * @param float $y2
  222. * @param array $color
  223. * @param float $width
  224. * @param array $style
  225. */
  226. function line($x1, $y1, $x2, $y2, $color, $width, $style = null) {
  227. if ( is_null($this->_last_line_width) || $width != $this->_last_line_width ) {
  228. $this->_pdf->SetLineWidth($width);
  229. $this->_last_line_width = $width;
  230. }
  231. $this->_set_stroke_colour($color);
  232. // FIXME: ugh, need to handle different styles here
  233. $this->_pdf->line($x1, $y1, $x2, $y2);
  234. }
  235. /**
  236. * Draws a rectangle at x1,y1 with width w and height h
  237. *
  238. * See {@link Style::munge_colour()} for the format of the colour array.
  239. * See {@link Cpdf::setLineStyle()} for a description of the $style
  240. * parameter (aka dash)
  241. *
  242. * @param float $x1
  243. * @param float $y1
  244. * @param float $w
  245. * @param float $h
  246. * @param array $color
  247. * @param float $width
  248. * @param array $style
  249. */
  250. function rectangle($x1, $y1, $w, $h, $color, $width, $style = null) {
  251. if ( is_null($this->_last_line_width) || $width != $this->_last_line_width ) {
  252. $this->_pdf->SetLineWidth($width);
  253. $this->_last_line_width = $width;
  254. }
  255. $this->_set_stroke_colour($color);
  256. // FIXME: ugh, need to handle styles here
  257. $this->_pdf->rect($x1, $y1, $w, $h);
  258. }
  259. /**
  260. * Draws a filled rectangle at x1,y1 with width w and height h
  261. *
  262. * See {@link Style::munge_colour()} for the format of the colour array.
  263. *
  264. * @param float $x1
  265. * @param float $y1
  266. * @param float $w
  267. * @param float $h
  268. * @param array $color
  269. */
  270. function filled_rectangle($x1, $y1, $w, $h, $color) {
  271. $this->_set_fill_colour($color);
  272. // FIXME: ugh, need to handle styles here
  273. $this->_pdf->rect($x1, $y1, $w, $h, "F");
  274. }
  275. /**
  276. * Draws a polygon
  277. *
  278. * The polygon is formed by joining all the points stored in the $points
  279. * array. $points has the following structure:
  280. * <code>
  281. * array(0 => x1,
  282. * 1 => y1,
  283. * 2 => x2,
  284. * 3 => y2,
  285. * ...
  286. * );
  287. * </code>
  288. *
  289. * See {@link Style::munge_colour()} for the format of the colour array.
  290. * See {@link Cpdf::setLineStyle()} for a description of the $style
  291. * parameter (aka dash)
  292. *
  293. * @param array $points
  294. * @param array $color
  295. * @param float $width
  296. * @param array $style
  297. * @param bool $fill Fills the polygon if true
  298. */
  299. function polygon($points, $color, $width = null, $style = null, $fill = false) {
  300. // FIXME: FPDF sucks
  301. }
  302. /**
  303. * Draws a circle at $x,$y with radius $r
  304. *
  305. * See {@link Style::munge_colour()} for the format of the colour array.
  306. * See {@link Cpdf::setLineStyle()} for a description of the $style
  307. * parameter (aka dash)
  308. *
  309. * @param float $x
  310. * @param float $y
  311. * @param float $r
  312. * @param array $color
  313. * @param float $width
  314. * @param array $style
  315. * @param bool $fill Fills the circle if true
  316. */
  317. function circle($x, $y, $r, $color, $width = null, $style = null, $fill = false){}
  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. /**
  333. * Writes text at the specified x and y coordinates
  334. *
  335. * See {@link Style::munge_colour()} for the format of the colour array.
  336. *
  337. * @param float $x
  338. * @param float $y
  339. * @param string $text the text to write
  340. * @param string $font the font file to use
  341. * @param float $size the font size, in points
  342. * @param array $color
  343. * @param float $adjust word spacing adjustment
  344. */
  345. function text($x, $y, $text, $font, $size, $color = array(0,0,0), $adjust = 0){}
  346. function javascript($code){}
  347. /**
  348. * Add a named destination (similar to <a name="foo">...</a> in html)
  349. *
  350. * @param string $anchorname The name of the named destination
  351. */
  352. function add_named_dest($anchorname){}
  353. /**
  354. * Add a link to the pdf
  355. *
  356. * @param string $url The url to link to
  357. * @param float $x The x position of the link
  358. * @param float $y The y position of the link
  359. * @param float $width The width of the link
  360. * @param float $height The height of the link
  361. */
  362. function add_link($url, $x, $y, $width, $height){}
  363. /**
  364. * Calculates text size, in points
  365. *
  366. * @param string $text the text to be sized
  367. * @param string $font the desired font
  368. * @param float $size the desired font size
  369. * @param float $spacing word spacing, if any
  370. * @return float
  371. */
  372. function get_text_width($text, $font, $size, $spacing = 0){}
  373. /**
  374. * Calculates font height, in points
  375. *
  376. * @param string $font
  377. * @param float $size
  378. * @return float
  379. */
  380. function get_font_height($font, $size){}
  381. /**
  382. * Starts a new page
  383. *
  384. * Subsequent drawing operations will appear on the new page.
  385. */
  386. function new_page(){}
  387. /**
  388. * Streams the PDF directly to the browser
  389. *
  390. * @param string $filename the name of the PDF file
  391. * @param array $options associative array, 'Attachment' => 0 or 1, 'compress' => 1 or 0
  392. */
  393. function stream($filename, $options = null){}
  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. }
  402. // Workaround for idiotic limitation on statics...
  403. PDFLib_Adapter::$PAPER_SIZES = CPDF_Adapter::$PAPER_SIZES;