CPDF_Adapter

PDF rendering interface.

Defined (1)

The class is defined in the following location(s).

/lib/dompdf/include/cpdf_adapter.cls.php  
  1. class CPDF_Adapter implements Canvas { 
  2.  
  3. /** 
  4. * Dimensions of paper sizes in points 
  5. * @var array; 
  6. */ 
  7. static $PAPER_SIZES = array( 
  8. "4a0" => array(0, 0, 4767.87, 6740.79),  
  9. "2a0" => array(0, 0, 3370.39, 4767.87),  
  10. "a0" => array(0, 0, 2383.94, 3370.39),  
  11. "a1" => array(0, 0, 1683.78, 2383.94),  
  12. "a2" => array(0, 0, 1190.55, 1683.78),  
  13. "a3" => array(0, 0, 841.89, 1190.55),  
  14. "a4" => array(0, 0, 595.28, 841.89),  
  15. "a5" => array(0, 0, 419.53, 595.28),  
  16. "a6" => array(0, 0, 297.64, 419.53),  
  17. "a7" => array(0, 0, 209.76, 297.64),  
  18. "a8" => array(0, 0, 147.40, 209.76),  
  19. "a9" => array(0, 0, 104.88, 147.40),  
  20. "a10" => array(0, 0, 73.70, 104.88),  
  21. "b0" => array(0, 0, 2834.65, 4008.19),  
  22. "b1" => array(0, 0, 2004.09, 2834.65),  
  23. "b2" => array(0, 0, 1417.32, 2004.09),  
  24. "b3" => array(0, 0, 1000.63, 1417.32),  
  25. "b4" => array(0, 0, 708.66, 1000.63),  
  26. "b5" => array(0, 0, 498.90, 708.66),  
  27. "b6" => array(0, 0, 354.33, 498.90),  
  28. "b7" => array(0, 0, 249.45, 354.33),  
  29. "b8" => array(0, 0, 175.75, 249.45),  
  30. "b9" => array(0, 0, 124.72, 175.75),  
  31. "b10" => array(0, 0, 87.87, 124.72),  
  32. "c0" => array(0, 0, 2599.37, 3676.54),  
  33. "c1" => array(0, 0, 1836.85, 2599.37),  
  34. "c2" => array(0, 0, 1298.27, 1836.85),  
  35. "c3" => array(0, 0, 918.43, 1298.27),  
  36. "c4" => array(0, 0, 649.13, 918.43),  
  37. "c5" => array(0, 0, 459.21, 649.13),  
  38. "c6" => array(0, 0, 323.15, 459.21),  
  39. "c7" => array(0, 0, 229.61, 323.15),  
  40. "c8" => array(0, 0, 161.57, 229.61),  
  41. "c9" => array(0, 0, 113.39, 161.57),  
  42. "c10" => array(0, 0, 79.37, 113.39),  
  43. "ra0" => array(0, 0, 2437.80, 3458.27),  
  44. "ra1" => array(0, 0, 1729.13, 2437.80),  
  45. "ra2" => array(0, 0, 1218.90, 1729.13),  
  46. "ra3" => array(0, 0, 864.57, 1218.90),  
  47. "ra4" => array(0, 0, 609.45, 864.57),  
  48. "sra0" => array(0, 0, 2551.18, 3628.35),  
  49. "sra1" => array(0, 0, 1814.17, 2551.18),  
  50. "sra2" => array(0, 0, 1275.59, 1814.17),  
  51. "sra3" => array(0, 0, 907.09, 1275.59),  
  52. "sra4" => array(0, 0, 637.80, 907.09),  
  53. "letter" => array(0, 0, 612.00, 792.00),  
  54. "legal" => array(0, 0, 612.00, 1008.00),  
  55. "ledger" => array(0, 0, 1224.00, 792.00),  
  56. "tabloid" => array(0, 0, 792.00, 1224.00),  
  57. "executive" => array(0, 0, 521.86, 756.00),  
  58. "folio" => array(0, 0, 612.00, 936.00),  
  59. "commercial #10 envelope" => array(0, 0, 684, 297),  
  60. "catalog #10 1/2 envelope" => array(0, 0, 648, 864),  
  61. "8.5x11" => array(0, 0, 612.00, 792.00),  
  62. "8.5x14" => array(0, 0, 612.00, 1008.0),  
  63. "11x17" => array(0, 0, 792.00, 1224.00),  
  64. ); 
  65.  
  66. /** 
  67. * The DOMPDF object 
  68. * @var DOMPDF 
  69. */ 
  70. private $_dompdf; 
  71.  
  72. /** 
  73. * Instance of Cpdf class 
  74. * @var Cpdf 
  75. */ 
  76. private $_pdf; 
  77.  
  78. /** 
  79. * PDF width, in points 
  80. * @var float 
  81. */ 
  82. private $_width; 
  83.  
  84. /** 
  85. * PDF height, in points 
  86. * @var float; 
  87. */ 
  88. private $_height; 
  89.  
  90. /** 
  91. * Current page number 
  92. * @var int 
  93. */ 
  94. private $_page_number; 
  95.  
  96. /** 
  97. * Total number of pages 
  98. * @var int 
  99. */ 
  100. private $_page_count; 
  101.  
  102. /** 
  103. * Text to display on every page 
  104. * @var array 
  105. */ 
  106. private $_page_text; 
  107.  
  108. /** 
  109. * Array of pages for accesing after rendering is initially complete 
  110. * @var array 
  111. */ 
  112. private $_pages; 
  113.  
  114. /** 
  115. * Array of temporary cached images to be deleted when processing is complete 
  116. * @var array 
  117. */ 
  118. private $_image_cache; 
  119.  
  120. /** 
  121. * Class constructor 
  122. * @param mixed $paper The size of paper to use in this PDF ({@link CPDF_Adapter::$PAPER_SIZES}) 
  123. * @param string $orientation The orientation of the document (either 'landscape' or 'portrait') 
  124. * @param DOMPDF $dompdf The DOMPDF instance 
  125. */ 
  126. function __construct($paper = "letter", $orientation = "portrait", DOMPDF $dompdf) { 
  127. if ( is_array($paper) ) { 
  128. $size = $paper; 
  129. else if ( isset(self::$PAPER_SIZES[mb_strtolower($paper)]) ) { 
  130. $size = self::$PAPER_SIZES[mb_strtolower($paper)]; 
  131. else { 
  132. $size = self::$PAPER_SIZES["letter"]; 
  133.  
  134. if ( mb_strtolower($orientation) === "landscape" ) { 
  135. list($size[2], $size[3]) = array($size[3], $size[2]); 
  136.  
  137. $this->_dompdf = $dompdf; 
  138.  
  139. $this->_pdf = new Cpdf( 
  140. $size,  
  141. $dompdf->get_option("enable_unicode"),  
  142. $dompdf->get_option("font_cache"),  
  143. $dompdf->get_option("temp_dir") 
  144. ); 
  145.  
  146. $this->_pdf->addInfo("Creator", "DOMPDF"); 
  147. $time = substr_replace(date('YmdHisO'), '\'', -2, 0).'\''; 
  148. $this->_pdf->addInfo("CreationDate", "D:$time"); 
  149. $this->_pdf->addInfo("ModDate", "D:$time"); 
  150.  
  151. $this->_width = $size[2] - $size[0]; 
  152. $this->_height= $size[3] - $size[1]; 
  153.  
  154. $this->_page_number = $this->_page_count = 1; 
  155. $this->_page_text = array(); 
  156.  
  157. $this->_pages = array($this->_pdf->getFirstPageId()); 
  158.  
  159. $this->_image_cache = array(); 
  160.  
  161. function get_dompdf() { 
  162. return $this->_dompdf; 
  163.  
  164. /** 
  165. * Class destructor 
  166. * Deletes all temporary image files 
  167. */ 
  168. function __destruct() { 
  169. foreach ($this->_image_cache as $img) { 
  170. // The file might be already deleted by 3rd party tmp cleaner,  
  171. // the file might not have been created at all 
  172. // (if image outputting commands failed) 
  173. // or because the destructor was called twice accidentally. 
  174. if (!file_exists($img)) { 
  175. continue; 
  176.  
  177. if (DEBUGPNG) print '[__destruct unlink '.$img.']'; 
  178. if (!DEBUGKEEPTEMP) unlink($img); 
  179.  
  180. /** 
  181. * Returns the Cpdf instance 
  182. * @return Cpdf 
  183. */ 
  184. function get_cpdf() { 
  185. return $this->_pdf; 
  186.  
  187. /** 
  188. * Add meta information to the PDF 
  189. * @param string $label label of the value (Creator, Producer, etc.) 
  190. * @param string $value the text to set 
  191. */ 
  192. function add_info($label, $value) { 
  193. $this->_pdf->addInfo($label, $value); 
  194.  
  195. /** 
  196. * Opens a new 'object' 
  197. * While an object is open, all drawing actions are recored in the object,  
  198. * as opposed to being drawn on the current page. Objects can be added 
  199. * later to a specific page or to several pages. 
  200. * The return value is an integer ID for the new object. 
  201. * @see CPDF_Adapter::close_object() 
  202. * @see CPDF_Adapter::add_object() 
  203. * @return int 
  204. */ 
  205. function open_object() { 
  206. $ret = $this->_pdf->openObject(); 
  207. $this->_pdf->saveState(); 
  208. return $ret; 
  209.  
  210. /** 
  211. * Reopens an existing 'object' 
  212. * @see CPDF_Adapter::open_object() 
  213. * @param int $object the ID of a previously opened object 
  214. */ 
  215. function reopen_object($object) { 
  216. $this->_pdf->reopenObject($object); 
  217. $this->_pdf->saveState(); 
  218.  
  219. /** 
  220. * Closes the current 'object' 
  221. * @see CPDF_Adapter::open_object() 
  222. */ 
  223. function close_object() { 
  224. $this->_pdf->restoreState(); 
  225. $this->_pdf->closeObject(); 
  226.  
  227. /** 
  228. * Adds a specified 'object' to the document 
  229. * $object int specifying an object created with {@link 
  230. * CPDF_Adapter::open_object()}. $where can be one of: 
  231. * - 'add' add to current page only 
  232. * - 'all' add to every page from the current one onwards 
  233. * - 'odd' add to all odd numbered pages from now on 
  234. * - 'even' add to all even numbered pages from now on 
  235. * - 'next' add the object to the next page only 
  236. * - 'nextodd' add to all odd numbered pages from the next one 
  237. * - 'nexteven' add to all even numbered pages from the next one 
  238. * @see Cpdf::addObject() 
  239. * @param int $object 
  240. * @param string $where 
  241. */ 
  242. function add_object($object, $where = 'all') { 
  243. $this->_pdf->addObject($object, $where); 
  244.  
  245. /** 
  246. * Stops the specified 'object' from appearing in the document. 
  247. * The object will stop being displayed on the page following the current 
  248. * one. 
  249. * @param int $object 
  250. */ 
  251. function stop_object($object) { 
  252. $this->_pdf->stopObject($object); 
  253.  
  254. /** 
  255. * @access private 
  256. */ 
  257. function serialize_object($id) { 
  258. // Serialize the pdf object's current state for retrieval later 
  259. return $this->_pdf->serializeObject($id); 
  260.  
  261. /** 
  262. * @access private 
  263. */ 
  264. function reopen_serialized_object($obj) { 
  265. return $this->_pdf->restoreSerializedObject($obj); 
  266.  
  267. //........................................................................ 
  268.  
  269. /** 
  270. * Returns the PDF's width in points 
  271. * @return float 
  272. */ 
  273. function get_width() { return $this->_width; } 
  274.  
  275. /** 
  276. * Returns the PDF's height in points 
  277. * @return float 
  278. */ 
  279. function get_height() { return $this->_height; } 
  280.  
  281. /** 
  282. * Returns the current page number 
  283. * @return int 
  284. */ 
  285. function get_page_number() { return $this->_page_number; } 
  286.  
  287. /** 
  288. * Returns the total number of pages in the document 
  289. * @return int 
  290. */ 
  291. function get_page_count() { return $this->_page_count; } 
  292.  
  293. /** 
  294. * Sets the current page number 
  295. * @param int $num 
  296. */ 
  297. function set_page_number($num) { $this->_page_number = $num; } 
  298.  
  299. /** 
  300. * Sets the page count 
  301. * @param int $count 
  302. */ 
  303. function set_page_count($count) { $this->_page_count = $count; } 
  304.  
  305. /** 
  306. * Sets the stroke color 
  307. * See {@link Style::set_color()} for the format of the color array. 
  308. * @param array $color 
  309. */ 
  310. protected function _set_stroke_color($color) { 
  311. $this->_pdf->setStrokeColor($color); 
  312.  
  313. /** 
  314. * Sets the fill colour 
  315. * See {@link Style::set_color()} for the format of the colour array. 
  316. * @param array $color 
  317. */ 
  318. protected function _set_fill_color($color) { 
  319. $this->_pdf->setColor($color); 
  320.  
  321. /** 
  322. * Sets line transparency 
  323. * @see Cpdf::setLineTransparency() 
  324. * Valid blend modes are (case-sensitive): 
  325. * Normal, Multiply, Screen, Overlay, Darken, Lighten,  
  326. * ColorDodge, ColorBurn, HardLight, SoftLight, Difference,  
  327. * Exclusion 
  328. * @param string $mode the blending mode to use 
  329. * @param float $opacity 0.0 fully transparent, 1.0 fully opaque 
  330. */ 
  331. protected function _set_line_transparency($mode, $opacity) { 
  332. $this->_pdf->setLineTransparency($mode, $opacity); 
  333.  
  334. /** 
  335. * Sets fill transparency 
  336. * @see Cpdf::setFillTransparency() 
  337. * Valid blend modes are (case-sensitive): 
  338. * Normal, Multiply, Screen, Overlay, Darken, Lighten,  
  339. * ColorDogde, ColorBurn, HardLight, SoftLight, Difference,  
  340. * Exclusion 
  341. * @param string $mode the blending mode to use 
  342. * @param float $opacity 0.0 fully transparent, 1.0 fully opaque 
  343. */ 
  344. protected function _set_fill_transparency($mode, $opacity) { 
  345. $this->_pdf->setFillTransparency($mode, $opacity); 
  346.  
  347. /** 
  348. * Sets the line style 
  349. * @see Cpdf::setLineStyle() 
  350. * @param float $width 
  351. * @param string $cap 
  352. * @param string $join 
  353. * @param array $dash 
  354. */ 
  355. protected function _set_line_style($width, $cap, $join, $dash) { 
  356. $this->_pdf->setLineStyle($width, $cap, $join, $dash); 
  357.  
  358. /** 
  359. * Sets the opacity  
  360. *  
  361. * @param $opacity 
  362. * @param $mode 
  363. */ 
  364. function set_opacity($opacity, $mode = "Normal") { 
  365. $this->_set_line_transparency($mode, $opacity); 
  366. $this->_set_fill_transparency($mode, $opacity); 
  367.  
  368. function set_default_view($view, $options = array()) { 
  369. array_unshift($options, $view); 
  370. call_user_func_array(array($this->_pdf, "openHere"), $options); 
  371.  
  372. /** 
  373. * Remaps y coords from 4th to 1st quadrant 
  374. * @param float $y 
  375. * @return float 
  376. */ 
  377. protected function y($y) { 
  378. return $this->_height - $y; 
  379.  
  380. // Canvas implementation 
  381. function line($x1, $y1, $x2, $y2, $color, $width, $style = array()) { 
  382. $this->_set_stroke_color($color); 
  383. $this->_set_line_style($width, "butt", "", $style); 
  384.  
  385. $this->_pdf->line($x1, $this->y($y1),  
  386. $x2, $this->y($y2)); 
  387.  
  388. function arc($x, $y, $r1, $r2, $astart, $aend, $color, $width, $style = array()) { 
  389. $this->_set_stroke_color($color); 
  390. $this->_set_line_style($width, "butt", "", $style); 
  391.  
  392. $this->_pdf->ellipse($x, $this->y($y), $r1, $r2, 0, 8, $astart, $aend, false, false, true, false); 
  393.  
  394. //........................................................................ 
  395.  
  396. /** 
  397. * Convert a GIF or BMP image to a PNG image 
  398. * @param string $image_url 
  399. * @param integer $type 
  400. * @throws DOMPDF_Exception 
  401. * @return string The url of the newly converted image 
  402. */ 
  403. protected function _convert_gif_bmp_to_png($image_url, $type) { 
  404. $image_type = Image_Cache::type_to_ext($type); 
  405. $func_name = "imagecreatefrom$image_type"; 
  406.  
  407. if ( !function_exists($func_name) ) { 
  408. throw new DOMPDF_Exception("Function $func_name() not found. Cannot convert $image_type image: $image_url. Please install the image PHP extension."); 
  409.  
  410. set_error_handler("record_warnings"); 
  411. $im = $func_name($image_url); 
  412.  
  413. if ( $im ) { 
  414. imageinterlace($im, false); 
  415.  
  416. $tmp_dir = $this->_dompdf->get_option("temp_dir"); 
  417. $tmp_name = tempnam($tmp_dir, "{$image_type}dompdf_img_"); 
  418. @unlink($tmp_name); 
  419. $filename = "$tmp_name.png"; 
  420. $this->_image_cache[] = $filename; 
  421.  
  422. imagepng($im, $filename); 
  423. imagedestroy($im); 
  424. }  
  425. else { 
  426. $filename = Image_Cache::$broken_image; 
  427.  
  428. restore_error_handler(); 
  429.  
  430. return $filename; 
  431.  
  432. function rectangle($x1, $y1, $w, $h, $color, $width, $style = array()) { 
  433. $this->_set_stroke_color($color); 
  434. $this->_set_line_style($width, "butt", "", $style); 
  435. $this->_pdf->rectangle($x1, $this->y($y1) - $h, $w, $h); 
  436.  
  437. function filled_rectangle($x1, $y1, $w, $h, $color) { 
  438. $this->_set_fill_color($color); 
  439. $this->_pdf->filledRectangle($x1, $this->y($y1) - $h, $w, $h); 
  440.  
  441. function clipping_rectangle($x1, $y1, $w, $h) { 
  442. $this->_pdf->clippingRectangle($x1, $this->y($y1) - $h, $w, $h); 
  443.  
  444. function clipping_roundrectangle($x1, $y1, $w, $h, $rTL, $rTR, $rBR, $rBL) { 
  445. $this->_pdf->clippingRectangleRounded($x1, $this->y($y1) - $h, $w, $h, $rTL, $rTR, $rBR, $rBL); 
  446.  
  447. function clipping_end() { 
  448. $this->_pdf->clippingEnd(); 
  449.  
  450. function save() { 
  451. $this->_pdf->saveState(); 
  452.  
  453. function restore() { 
  454. $this->_pdf->restoreState(); 
  455.  
  456. function rotate($angle, $x, $y) { 
  457. $this->_pdf->rotate($angle, $x, $y); 
  458.  
  459. function skew($angle_x, $angle_y, $x, $y) { 
  460. $this->_pdf->skew($angle_x, $angle_y, $x, $y); 
  461.  
  462. function scale($s_x, $s_y, $x, $y) { 
  463. $this->_pdf->scale($s_x, $s_y, $x, $y); 
  464.  
  465. function translate($t_x, $t_y) { 
  466. $this->_pdf->translate($t_x, $t_y); 
  467.  
  468. function transform($a, $b, $c, $d, $e, $f) { 
  469. $this->_pdf->transform(array($a, $b, $c, $d, $e, $f)); 
  470.  
  471. function polygon($points, $color, $width = null, $style = array(), $fill = false) { 
  472. $this->_set_fill_color($color); 
  473. $this->_set_stroke_color($color); 
  474.  
  475. // Adjust y values 
  476. for ( $i = 1; $i < count($points); $i += 2) { 
  477. $points[$i] = $this->y($points[$i]); 
  478.  
  479. $this->_pdf->polygon($points, count($points) / 2, $fill); 
  480.  
  481. function circle($x, $y, $r1, $color, $width = null, $style = null, $fill = false) { 
  482. $this->_set_fill_color($color); 
  483. $this->_set_stroke_color($color); 
  484.  
  485. if ( !$fill && isset($width) ) { 
  486. $this->_set_line_style($width, "round", "round", $style); 
  487.  
  488. $this->_pdf->ellipse($x, $this->y($y), $r1, 0, 0, 8, 0, 360, 1, $fill); 
  489.  
  490. function image($img, $x, $y, $w, $h, $resolution = "normal") { 
  491. list($width, $height, $type) = dompdf_getimagesize($img); 
  492.  
  493. $debug_png = $this->_dompdf->get_option("debug_png"); 
  494.  
  495. if ($debug_png) print "[image:$img|$width|$height|$type]"; 
  496.  
  497. switch ($type) { 
  498. case IMAGETYPE_JPEG: 
  499. if ($debug_png) print '!!!jpg!!!'; 
  500. $this->_pdf->addJpegFromFile($img, $x, $this->y($y) - $h, $w, $h); 
  501. break; 
  502.  
  503. case IMAGETYPE_GIF: 
  504. case IMAGETYPE_BMP: 
  505. if ($debug_png) print '!!!bmp or gif!!!'; 
  506. // @todo use cache for BMP and GIF 
  507. $img = $this->_convert_gif_bmp_to_png($img, $type); 
  508.  
  509. case IMAGETYPE_PNG: 
  510. if ($debug_png) print '!!!png!!!'; 
  511.  
  512. $this->_pdf->addPngFromFile($img, $x, $this->y($y) - $h, $w, $h); 
  513. break; 
  514.  
  515. default: 
  516. if ($debug_png) print '!!!unknown!!!'; 
  517.  
  518. function text($x, $y, $text, $font, $size, $color = array(0, 0, 0), $word_space = 0.0, $char_space = 0.0, $angle = 0.0) { 
  519. $pdf = $this->_pdf; 
  520.  
  521. $pdf->setColor($color); 
  522.  
  523. $font .= ".afm"; 
  524. $pdf->selectFont($font); 
  525.  
  526. //Font_Metrics::get_font_height($font, $size) == 
  527. //$this->get_font_height($font, $size) == 
  528. //$this->_pdf->selectFont($font), $this->_pdf->getFontHeight($size) 
  529. //- FontBBoxheight+FontHeightOffset, scaled to $size, in pt 
  530. //$this->_pdf->getFontDescender($size) 
  531. //- Descender scaled to size 
  532. // 
  533. //$this->_pdf->fonts[$this->_pdf->currentFont] sizes: 
  534. //['FontBBox'][0] left, ['FontBBox'][1] bottom, ['FontBBox'][2] right, ['FontBBox'][3] top 
  535. //Maximum extent of all glyphs of the font from the baseline point 
  536. //['Ascender'] maximum height above baseline except accents 
  537. //['Descender'] maximum depth below baseline, negative number means below baseline 
  538. //['FontHeightOffset'] manual enhancement of .afm files to trim windows fonts. currently not used. 
  539. //Values are in 1/1000 pt for a font size of 1 pt 
  540. // 
  541. //['FontBBox'][1] should be close to ['Descender'] 
  542. //['FontBBox'][3] should be close to ['Ascender']+Accents 
  543. //in practice, FontBBox values are a little bigger 
  544. // 
  545. //The text position is referenced to the baseline, not to the lower corner of the FontBBox,  
  546. //for what the left, top corner is given. 
  547. //FontBBox spans also the background box for the text. 
  548. //If the lower corner would be used as reference point, the Descents of the glyphs would 
  549. //hang over the background box border. 
  550. //Therefore compensate only the extent above the Baseline. 
  551. // 
  552. //print '<pre>['.$font.', '.$size.', '.$pdf->getFontHeight($size).', '.$pdf->getFontDescender($size).', '.$pdf->fonts[$pdf->currentFont]['FontBBox'][3].', '.$pdf->fonts[$pdf->currentFont]['FontBBox'][1].', '.$pdf->fonts[$pdf->currentFont]['FontHeightOffset'].', '.$pdf->fonts[$pdf->currentFont]['Ascender'].', '.$pdf->fonts[$pdf->currentFont]['Descender'].']</pre>'; 
  553. // 
  554. //$pdf->addText($x, $this->y($y) - ($pdf->fonts[$pdf->currentFont]['FontBBox'][3]*$size)/1000, $size, $text, $angle, $word_space, $char_space); 
  555. $pdf->addText($x, $this->y($y) - $pdf->getFontHeight($size), $size, $text, $angle, $word_space, $char_space); 
  556.  
  557. //........................................................................ 
  558.  
  559. function javascript($code) { 
  560. $this->_pdf->addJavascript($code); 
  561.  
  562. //........................................................................ 
  563.  
  564. /** 
  565. * Add a named destination (similar to <a name="foo">...</a> in html) 
  566. * @param string $anchorname The name of the named destination 
  567. */ 
  568. function add_named_dest($anchorname) { 
  569. $this->_pdf->addDestination($anchorname, "Fit"); 
  570.  
  571. //........................................................................ 
  572.  
  573. /** 
  574. * Add a link to the pdf 
  575. * @param string $url The url to link to 
  576. * @param float $x The x position of the link 
  577. * @param float $y The y position of the link 
  578. * @param float $width The width of the link 
  579. * @param float $height The height of the link 
  580. */ 
  581. function add_link($url, $x, $y, $width, $height) { 
  582.  
  583. $y = $this->y($y) - $height; 
  584.  
  585. if ( strpos($url, '#') === 0 ) { 
  586. // Local link 
  587. $name = substr($url, 1); 
  588. if ( $name ) { 
  589. $this->_pdf->addInternalLink($name, $x, $y, $x + $width, $y + $height); 
  590.  
  591. else { 
  592. $this->_pdf->addLink(rawurldecode($url), $x, $y, $x + $width, $y + $height); 
  593.  
  594. function get_text_width($text, $font, $size, $word_spacing = 0, $char_spacing = 0) { 
  595. $this->_pdf->selectFont($font); 
  596.  
  597. $unicode = $this->_dompdf->get_option("enable_unicode"); 
  598. if (!$unicode) { 
  599. $text = mb_convert_encoding($text, 'Windows-1252', 'UTF-8'); 
  600.  
  601. return $this->_pdf->getTextWidth($size, $text, $word_spacing, $char_spacing); 
  602.  
  603. function register_string_subset($font, $string) { 
  604. $this->_pdf->registerText($font, $string); 
  605.  
  606. function get_font_height($font, $size) { 
  607. $this->_pdf->selectFont($font); 
  608.  
  609. $ratio = $this->_dompdf->get_option("font_height_ratio"); 
  610. return $this->_pdf->getFontHeight($size) * $ratio; 
  611.  
  612. /**function get_font_x_height($font, $size) { 
  613. $this->_pdf->selectFont($font); 
  614. $ratio = $this->_dompdf->get_option("font_height_ratio"); 
  615. return $this->_pdf->getFontXHeight($size) * $ratio; 
  616. }*/ 
  617.  
  618. function get_font_baseline($font, $size) { 
  619. $ratio = $this->_dompdf->get_option("font_height_ratio"); 
  620. return $this->get_font_height($font, $size) / $ratio; 
  621.  
  622. /** 
  623. * Writes text at the specified x and y coordinates on every page 
  624. * The strings '{PAGE_NUM}' and '{PAGE_COUNT}' are automatically replaced 
  625. * with their current values. 
  626. * See {@link Style::munge_color()} for the format of the colour array. 
  627. * @param float $x 
  628. * @param float $y 
  629. * @param string $text the text to write 
  630. * @param string $font the font file to use 
  631. * @param float $size the font size, in points 
  632. * @param array $color 
  633. * @param float $word_space word spacing adjustment 
  634. * @param float $char_space char spacing adjustment 
  635. * @param float $angle angle to write the text at, measured CW starting from the x-axis 
  636. */ 
  637. function page_text($x, $y, $text, $font, $size, $color = array(0, 0, 0), $word_space = 0.0, $char_space = 0.0, $angle = 0.0) { 
  638. $_t = "text"; 
  639. $this->_page_text[] = compact("_t", "x", "y", "text", "font", "size", "color", "word_space", "char_space", "angle"); 
  640.  
  641. /** 
  642. * Processes a script on every page 
  643. * The variables $pdf, $PAGE_NUM, and $PAGE_COUNT are available. 
  644. * This function can be used to add page numbers to all pages 
  645. * after the first one, for example. 
  646. * @param string $code the script code 
  647. * @param string $type the language type for script 
  648. */ 
  649. function page_script($code, $type = "text/php") { 
  650. $_t = "script"; 
  651. $this->_page_text[] = compact("_t", "code", "type"); 
  652.  
  653. function new_page() { 
  654. $this->_page_number++; 
  655. $this->_page_count++; 
  656.  
  657. $ret = $this->_pdf->newPage(); 
  658. $this->_pages[] = $ret; 
  659. return $ret; 
  660.  
  661. /** 
  662. * Add text to each page after rendering is complete 
  663. */ 
  664. protected function _add_page_text() { 
  665.  
  666. if ( !count($this->_page_text) ) { 
  667. return; 
  668.  
  669. $page_number = 1; 
  670. $eval = null; 
  671.  
  672. foreach ($this->_pages as $pid) { 
  673. $this->reopen_object($pid); 
  674.  
  675. foreach ($this->_page_text as $pt) { 
  676. extract($pt); 
  677.  
  678. switch ($_t) { 
  679. case "text": 
  680. $text = str_replace(array("{PAGE_NUM}", "{PAGE_COUNT}"),  
  681. array($page_number, $this->_page_count), $text); 
  682. $this->text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle); 
  683. break; 
  684.  
  685. case "script": 
  686. if ( !$eval ) { 
  687. $eval = new PHP_Evaluator($this); 
  688. $eval->evaluate($code, array('PAGE_NUM' => $page_number, 'PAGE_COUNT' => $this->_page_count)); 
  689. break; 
  690.  
  691. $this->close_object(); 
  692. $page_number++; 
  693.  
  694. /** 
  695. * Streams the PDF directly to the browser 
  696. * @param string $filename the name of the PDF file 
  697. * @param array $options associative array, 'Attachment' => 0 or 1, 'compress' => 1 or 0 
  698. */ 
  699. function stream($filename, $options = null) { 
  700. // Add page text 
  701. $this->_add_page_text(); 
  702.  
  703. $options["Content-Disposition"] = $filename; 
  704. $this->_pdf->stream($options); 
  705.  
  706. /** 
  707. * Returns the PDF as a string 
  708. *  
  709. * @param array $options Output options 
  710. * @return string 
  711. */ 
  712. function output($options = null) { 
  713. $this->_add_page_text(); 
  714.  
  715. $debug = isset($options["compress"]) && $options["compress"] != 1; 
  716.  
  717. return $this->_pdf->output($debug); 
  718.  
  719. /** 
  720. * Returns logging messages generated by the Cpdf class 
  721. * @return string 
  722. */ 
  723. function get_messages() { 
  724. return $this->_pdf->messages;