PageRenderTime 74ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/dompdf/include/tcpdf_adapter.cls.php

http://dompdf.googlecode.com/
PHP | 618 lines | 142 code | 73 blank | 403 comment | 18 complexity | 9290225adcb85afd73a4be1e14107163 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.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 504 2012-11-04 16:10:14Z 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 CPDF_Adapter::$PAPER_SIZES below.
  31. /**
  32. * Instance of the TCPDF class
  33. *
  34. * @var TCPDF
  35. */
  36. private $_pdf;
  37. /**
  38. * PDF width in points
  39. *
  40. * @var float
  41. */
  42. private $_width;
  43. /**
  44. * PDF height in points
  45. *
  46. * @var float
  47. */
  48. private $_height;
  49. /**
  50. * Last fill colour used
  51. *
  52. * @var array
  53. */
  54. private $_last_fill_color;
  55. /**
  56. * Last stroke colour used
  57. *
  58. * @var array
  59. */
  60. private $_last_stroke_color;
  61. /**
  62. * Last line width used
  63. *
  64. * @var float
  65. */
  66. private $_last_line_width;
  67. /**
  68. * Total number of pages
  69. *
  70. * @var int
  71. */
  72. private $_page_count;
  73. /**
  74. * Text to display on every page
  75. *
  76. * @var array
  77. */
  78. private $_page_text;
  79. /**
  80. * Array of pages for accessing after initial rendering is complete
  81. *
  82. * @var array
  83. */
  84. private $_pages;
  85. /**
  86. * Class constructor
  87. *
  88. * @param mixed $paper The size of paper to use either a string (see {@link CPDF_Adapter::$PAPER_SIZES}) or
  89. * an array(xmin,ymin,xmax,ymax)
  90. * @param string $orientation The orientation of the document (either 'landscape' or 'portrait')
  91. */
  92. function __construct($paper = "letter", $orientation = "portrait") {
  93. if ( is_array($paper) )
  94. $size = $paper;
  95. else if ( isset(self::$PAPER_SIZES[mb_strtolower($paper)]) )
  96. $size = self::$PAPER_SIZES[$paper];
  97. else
  98. $size = self::$PAPER_SIZES["letter"];
  99. if ( mb_strtolower($orientation) === "landscape" ) {
  100. list($size[2], $size[3]) = array($size[3], $size[2]);
  101. }
  102. $this->_width = $size[2] - $size[0];
  103. $this->_height = $size[3] - $size[1];
  104. $this->_pdf = new TCPDF("P", "pt", array($this->_width, $this->_height));
  105. $this->_pdf->Setcreator("DOMPDF Converter");
  106. $this->_pdf->AddPage();
  107. $this->_page_number = $this->_page_count = 1;
  108. $this->_page_text = array();
  109. $this->_last_fill_color =
  110. $this->_last_stroke_color =
  111. $this->_last_line_width = null;
  112. }
  113. /**
  114. * Remaps y coords from 4th to 1st quadrant
  115. *
  116. * @param float $y
  117. * @return float
  118. */
  119. protected function y($y) { return $this->_height - $y; }
  120. /**
  121. * Sets the stroke colour
  122. *
  123. * @param array $color
  124. *
  125. * @return void
  126. */
  127. protected function _set_stroke_colour($color) {
  128. $color[0] = round(255 * $color[0]);
  129. $color[1] = round(255 * $color[1]);
  130. $color[2] = round(255 * $color[2]);
  131. if ( is_null($this->_last_stroke_color) || $color != $this->_last_stroke_color ) {
  132. $this->_pdf->SetDrawColor($color[0],$color[1],$color[2]);
  133. $this->_last_stroke_color = $color;
  134. }
  135. }
  136. /**
  137. * Sets the fill colour
  138. *
  139. * @param array $color
  140. */
  141. protected function _set_fill_colour($color) {
  142. $color[0] = round(255 * $color[0]);
  143. $color[1] = round(255 * $color[1]);
  144. $color[2] = round(255 * $color[2]);
  145. if ( is_null($this->_last_fill_color) || $color != $this->_last_fill_color ) {
  146. $this->_pdf->SetDrawColor($color[0],$color[1],$color[2]);
  147. $this->_last_fill_color = $color;
  148. }
  149. }
  150. /**
  151. * Return the TCPDF instance
  152. *
  153. * @return TCPDF
  154. */
  155. function get_tcpdf() { return $this->_pdf; }
  156. /**
  157. * Returns the current page number
  158. *
  159. * @return int
  160. */
  161. function get_page_number() {
  162. return $this->_page_number;
  163. }
  164. /**
  165. * Returns the total number of pages
  166. *
  167. * @return int
  168. */
  169. function get_page_count() {
  170. return $this->_page_count;
  171. }
  172. /**
  173. * Sets the total number of pages
  174. *
  175. * @param int $count
  176. */
  177. function set_page_count($count) {
  178. $this->_page_count = (int)$count;
  179. }
  180. /**
  181. * Draws a line from x1,y1 to x2,y2
  182. *
  183. * See {@link Style::munge_colour()} for the format of the colour array.
  184. * See {@link Cpdf::setLineStyle()} for a description of the format of the
  185. * $style parameter (aka dash).
  186. *
  187. * @param float $x1
  188. * @param float $y1
  189. * @param float $x2
  190. * @param float $y2
  191. * @param array $color
  192. * @param float $width
  193. * @param array $style
  194. */
  195. function line($x1, $y1, $x2, $y2, $color, $width, $style = null) {
  196. if ( is_null($this->_last_line_width) || $width != $this->_last_line_width ) {
  197. $this->_pdf->SetLineWidth($width);
  198. $this->_last_line_width = $width;
  199. }
  200. $this->_set_stroke_colour($color);
  201. // FIXME: ugh, need to handle different styles here
  202. $this->_pdf->line($x1, $y1, $x2, $y2);
  203. }
  204. /**
  205. * Draws a rectangle at x1,y1 with width w and height h
  206. *
  207. * See {@link Style::munge_colour()} for the format of the colour array.
  208. * See {@link Cpdf::setLineStyle()} for a description of the $style
  209. * parameter (aka dash)
  210. *
  211. * @param float $x1
  212. * @param float $y1
  213. * @param float $w
  214. * @param float $h
  215. * @param array $color
  216. * @param float $width
  217. * @param array $style
  218. */
  219. function rectangle($x1, $y1, $w, $h, $color, $width, $style = null) {
  220. if ( is_null($this->_last_line_width) || $width != $this->_last_line_width ) {
  221. $this->_pdf->SetLineWidth($width);
  222. $this->_last_line_width = $width;
  223. }
  224. $this->_set_stroke_colour($color);
  225. // FIXME: ugh, need to handle styles here
  226. $this->_pdf->rect($x1, $y1, $w, $h);
  227. }
  228. /**
  229. * Draws a filled rectangle at x1,y1 with width w and height h
  230. *
  231. * See {@link Style::munge_colour()} for the format of the colour array.
  232. *
  233. * @param float $x1
  234. * @param float $y1
  235. * @param float $w
  236. * @param float $h
  237. * @param array $color
  238. */
  239. function filled_rectangle($x1, $y1, $w, $h, $color) {
  240. $this->_set_fill_colour($color);
  241. // FIXME: ugh, need to handle styles here
  242. $this->_pdf->rect($x1, $y1, $w, $h, "F");
  243. }
  244. /**
  245. * Draws a polygon
  246. *
  247. * The polygon is formed by joining all the points stored in the $points
  248. * array. $points has the following structure:
  249. * <code>
  250. * array(0 => x1,
  251. * 1 => y1,
  252. * 2 => x2,
  253. * 3 => y2,
  254. * ...
  255. * );
  256. * </code>
  257. *
  258. * See {@link Style::munge_colour()} for the format of the colour array.
  259. * See {@link Cpdf::setLineStyle()} for a description of the $style
  260. * parameter (aka dash)
  261. *
  262. * @param array $points
  263. * @param array $color
  264. * @param float $width
  265. * @param array $style
  266. * @param bool $fill Fills the polygon if true
  267. */
  268. function polygon($points, $color, $width = null, $style = null, $fill = false) {
  269. // FIXME
  270. }
  271. /**
  272. * Draws a circle at $x,$y with radius $r
  273. *
  274. * See {@link Style::munge_colour()} for the format of the colour array.
  275. * See {@link Cpdf::setLineStyle()} for a description of the $style
  276. * parameter (aka dash)
  277. *
  278. * @param float $x
  279. * @param float $y
  280. * @param float $r
  281. * @param array $color
  282. * @param float $width
  283. * @param array $style
  284. * @param bool $fill Fills the circle if true
  285. */
  286. function circle($x, $y, $r, $color, $width = null, $style = null, $fill = false) {
  287. // FIXME
  288. }
  289. /**
  290. * Add an image to the pdf.
  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 float $x x position
  296. * @param float $y y position
  297. * @param int $w width (in pixels)
  298. * @param int $h height (in pixels)
  299. * @param string $resolution
  300. *
  301. * @return void
  302. */
  303. function image($img_url, $x, $y, $w, $h, $resolution = "normal") {
  304. // FIXME
  305. }
  306. /**
  307. * Writes text at the specified x and y coordinates
  308. * See {@link Style::munge_colour()} for the format of the colour array.
  309. *
  310. * @param float $x
  311. * @param float $y
  312. * @param string $text the text to write
  313. * @param string $font the font file to use
  314. * @param float $size the font size, in points
  315. * @param array $color
  316. * @param float $word_space word spacing adjustment
  317. * @param float $char_space
  318. * @param float $angle
  319. *
  320. * @return void
  321. */
  322. function text($x, $y, $text, $font, $size, $color = array(0,0,0), $word_space = 0.0, $char_space = 0.0, $angle = 0.0) {
  323. // FIXME
  324. }
  325. function javascript($code) {
  326. // FIXME
  327. }
  328. /**
  329. * Add a named destination (similar to <a name="foo">...</a> in html)
  330. *
  331. * @param string $anchorname The name of the named destination
  332. */
  333. function add_named_dest($anchorname) {
  334. // FIXME
  335. }
  336. /**
  337. * Add a link to the pdf
  338. *
  339. * @param string $url The url to link to
  340. * @param float $x The x position of the link
  341. * @param float $y The y position of the link
  342. * @param float $width The width of the link
  343. * @param float $height The height of the link
  344. */
  345. function add_link($url, $x, $y, $width, $height) {
  346. // FIXME
  347. }
  348. /**
  349. * Add meta information to the PDF
  350. *
  351. * @param string $label label of the value (Creator, Producer, etc.)
  352. * @param string $value the text to set
  353. */
  354. function add_info($label, $value) {
  355. $method = "Set$label";
  356. if ( in_array("Title", "Author", "Keywords", "Subject") && method_exists($this->_pdf, $method) ) {
  357. $this->_pdf->$method($value);
  358. }
  359. }
  360. /**
  361. * Calculates text size, in points
  362. *
  363. * @param string $text the text to be sized
  364. * @param string $font the desired font
  365. * @param float $size the desired font size
  366. * @param float $word_spacing word spacing, if any
  367. * @param float $char_spacing
  368. *
  369. * @return float
  370. */
  371. function get_text_width($text, $font, $size, $word_spacing = 0.0, $char_spacing = 0.0) {
  372. // FIXME
  373. }
  374. /**
  375. * Calculates font height, in points
  376. *
  377. * @param string $font
  378. * @param float $size
  379. * @return float
  380. */
  381. function get_font_height($font, $size) {
  382. // FIXME
  383. }
  384. /**
  385. * Starts a new page
  386. *
  387. * Subsequent drawing operations will appear on the new page.
  388. */
  389. function new_page() {
  390. // FIXME
  391. }
  392. /**
  393. * Streams the PDF directly to the browser
  394. *
  395. * @param string $filename the name of the PDF file
  396. * @param array $options associative array, 'Attachment' => 0 or 1, 'compress' => 1 or 0
  397. */
  398. function stream($filename, $options = null) {
  399. // FIXME
  400. }
  401. /**
  402. * Returns the PDF as a string
  403. *
  404. * @param array $options associative array: 'compress' => 1 or 0
  405. * @return string
  406. */
  407. function output($options = null) {
  408. // FIXME
  409. }
  410. /**
  411. * Starts a clipping rectangle at x1,y1 with width w and height h
  412. *
  413. * @param float $x1
  414. * @param float $y1
  415. * @param float $w
  416. * @param float $h
  417. */
  418. function clipping_rectangle($x1, $y1, $w, $h) {
  419. // TODO: Implement clipping_rectangle() method.
  420. }
  421. /**
  422. * Starts a rounded clipping rectangle at x1,y1 with width w and height h
  423. *
  424. * @param float $x1
  425. * @param float $y1
  426. * @param float $w
  427. * @param float $h
  428. * @param float $tl
  429. * @param float $tr
  430. * @param float $br
  431. * @param float $bl
  432. *
  433. * @return void
  434. */
  435. function clipping_roundrectangle($x1, $y1, $w, $h, $tl, $tr, $br, $bl) {
  436. // TODO: Implement clipping_roundrectangle() method.
  437. }
  438. /**
  439. * Ends the last clipping shape
  440. */
  441. function clipping_end() {
  442. // TODO: Implement clipping_end() method.
  443. }
  444. /**
  445. * Save current state
  446. */
  447. function save() {
  448. // TODO: Implement save() method.
  449. }
  450. /**
  451. * Restore last state
  452. */
  453. function restore() {
  454. // TODO: Implement restore() method.
  455. }
  456. /**
  457. * Rotate
  458. */
  459. function rotate($angle, $x, $y) {
  460. // TODO: Implement rotate() method.
  461. }
  462. /**
  463. * Skew
  464. */
  465. function skew($angle_x, $angle_y, $x, $y) {
  466. // TODO: Implement skew() method.
  467. }
  468. /**
  469. * Scale
  470. */
  471. function scale($s_x, $s_y, $x, $y) {
  472. // TODO: Implement scale() method.
  473. }
  474. /**
  475. * Translate
  476. */
  477. function translate($t_x, $t_y) {
  478. // TODO: Implement translate() method.
  479. }
  480. /**
  481. * Transform
  482. */
  483. function transform($a, $b, $c, $d, $e, $f) {
  484. // TODO: Implement transform() method.
  485. }
  486. /**
  487. * Add an arc to the PDF
  488. * See {@link Style::munge_colour()} for the format of the colour array.
  489. *
  490. * @param float $x X coordinate of the arc
  491. * @param float $y Y coordinate of the arc
  492. * @param float $r1 Radius 1
  493. * @param float $r2 Radius 2
  494. * @param float $astart Start angle in degrees
  495. * @param float $aend End angle in degrees
  496. * @param array $color Color
  497. * @param float $width
  498. * @param array $style
  499. *
  500. * @return void
  501. */
  502. function arc($x, $y, $r1, $r2, $astart, $aend, $color, $width, $style = array()) {
  503. // TODO: Implement arc() method.
  504. }
  505. /**
  506. * Calculates font baseline, in points
  507. *
  508. * @param string $font
  509. * @param float $size
  510. *
  511. * @return float
  512. */
  513. function get_font_baseline($font, $size) {
  514. // TODO: Implement get_font_baseline() method.
  515. }
  516. /**
  517. * Sets the opacity
  518. *
  519. * @param float $opacity
  520. * @param string $mode
  521. */
  522. function set_opacity($opacity, $mode = "Normal") {
  523. // TODO: Implement set_opacity() method.
  524. }
  525. /**
  526. * Sets the default view
  527. *
  528. * @param string $view
  529. * 'XYZ' left, top, zoom
  530. * 'Fit'
  531. * 'FitH' top
  532. * 'FitV' left
  533. * 'FitR' left,bottom,right
  534. * 'FitB'
  535. * 'FitBH' top
  536. * 'FitBV' left
  537. * @param array $options
  538. *
  539. * @return void
  540. */
  541. function set_default_view($view, $options = array()) {
  542. // TODO: Implement set_default_view() method.
  543. }}
  544. // Workaround for idiotic limitation on statics...
  545. TCPDF_Adapter::$PAPER_SIZES = CPDF_Adapter::$PAPER_SIZES;