cpdf_adapter.cls.php 24 KB

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