tcpdf_adapter.cls.php 14 KB

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