Sin descripción

cpdf_adapter.cls.php 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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. // The file might be already deleted by 3rd party tmp cleaner,
  195. // the file might not have been created at all
  196. // (if image outputting commands failed)
  197. // or because the destructor was called twice accidentally.
  198. if (!file_exists($img)) {
  199. continue;
  200. }
  201. if (DEBUGPNG) print '[__destruct unlink '.$img.']';
  202. if (!DEBUGKEEPTEMP) unlink($img);
  203. }
  204. }
  205. /**
  206. * Returns the Cpdf instance
  207. *
  208. * @return Cpdf
  209. */
  210. function get_cpdf() {
  211. return $this->_pdf;
  212. }
  213. /**
  214. * Add meta information to the PDF
  215. *
  216. * @param string $label label of the value (Creator, Producer, etc.)
  217. * @param string $value the text to set
  218. */
  219. function add_info($label, $value) {
  220. $this->_pdf->addInfo($label, $value);
  221. }
  222. /**
  223. * Opens a new 'object'
  224. *
  225. * While an object is open, all drawing actions are recored in the object,
  226. * as opposed to being drawn on the current page. Objects can be added
  227. * later to a specific page or to several pages.
  228. *
  229. * The return value is an integer ID for the new object.
  230. *
  231. * @see CPDF_Adapter::close_object()
  232. * @see CPDF_Adapter::add_object()
  233. *
  234. * @return int
  235. */
  236. function open_object() {
  237. $ret = $this->_pdf->openObject();
  238. $this->_pdf->saveState();
  239. return $ret;
  240. }
  241. /**
  242. * Reopens an existing 'object'
  243. *
  244. * @see CPDF_Adapter::open_object()
  245. * @param int $object the ID of a previously opened object
  246. */
  247. function reopen_object($object) {
  248. $this->_pdf->reopenObject($object);
  249. $this->_pdf->saveState();
  250. }
  251. /**
  252. * Closes the current 'object'
  253. *
  254. * @see CPDF_Adapter::open_object()
  255. */
  256. function close_object() {
  257. $this->_pdf->restoreState();
  258. $this->_pdf->closeObject();
  259. }
  260. /**
  261. * Adds a specified 'object' to the document
  262. *
  263. * $object int specifying an object created with {@link
  264. * CPDF_Adapter::open_object()}. $where can be one of:
  265. * - 'add' add to current page only
  266. * - 'all' add to every page from the current one onwards
  267. * - 'odd' add to all odd numbered pages from now on
  268. * - 'even' add to all even numbered pages from now on
  269. * - 'next' add the object to the next page only
  270. * - 'nextodd' add to all odd numbered pages from the next one
  271. * - 'nexteven' add to all even numbered pages from the next one
  272. *
  273. * @see Cpdf::addObject()
  274. *
  275. * @param int $object
  276. * @param string $where
  277. */
  278. function add_object($object, $where = 'all') {
  279. $this->_pdf->addObject($object, $where);
  280. }
  281. /**
  282. * Stops the specified 'object' from appearing in the document.
  283. *
  284. * The object will stop being displayed on the page following the current
  285. * one.
  286. *
  287. * @param int $object
  288. */
  289. function stop_object($object) {
  290. $this->_pdf->stopObject($object);
  291. }
  292. /**
  293. * @access private
  294. */
  295. function serialize_object($id) {
  296. // Serialize the pdf object's current state for retrieval later
  297. return $this->_pdf->serializeObject($id);
  298. }
  299. /**
  300. * @access private
  301. */
  302. function reopen_serialized_object($obj) {
  303. return $this->_pdf->restoreSerializedObject($obj);
  304. }
  305. //........................................................................
  306. /**
  307. * Returns the PDF's width in points
  308. * @return float
  309. */
  310. function get_width() { return $this->_width; }
  311. /**
  312. * Returns the PDF's height in points
  313. * @return float
  314. */
  315. function get_height() { return $this->_height; }
  316. /**
  317. * Returns the current page number
  318. * @return int
  319. */
  320. function get_page_number() { return $this->_page_number; }
  321. /**
  322. * Returns the total number of pages in the document
  323. * @return int
  324. */
  325. function get_page_count() { return $this->_page_count; }
  326. /**
  327. * Sets the current page number
  328. *
  329. * @param int $num
  330. */
  331. function set_page_number($num) { $this->_page_number = $num; }
  332. /**
  333. * Sets the page count
  334. *
  335. * @param int $count
  336. */
  337. function set_page_count($count) { $this->_page_count = $count; }
  338. /**
  339. * Sets the stroke color
  340. *
  341. * See {@link Style::set_color()} for the format of the color array.
  342. * @param array $color
  343. */
  344. protected function _set_stroke_color($color) {
  345. $this->_pdf->setStrokeColor($color);
  346. }
  347. /**
  348. * Sets the fill colour
  349. *
  350. * See {@link Style::set_color()} for the format of the colour array.
  351. * @param array $color
  352. */
  353. protected function _set_fill_color($color) {
  354. $this->_pdf->setColor($color);
  355. }
  356. /**
  357. * Sets line transparency
  358. * @see Cpdf::setLineTransparency()
  359. *
  360. * Valid blend modes are (case-sensitive):
  361. *
  362. * Normal, Multiply, Screen, Overlay, Darken, Lighten,
  363. * ColorDodge, ColorBurn, HardLight, SoftLight, Difference,
  364. * Exclusion
  365. *
  366. * @param string $mode the blending mode to use
  367. * @param float $opacity 0.0 fully transparent, 1.0 fully opaque
  368. */
  369. protected function _set_line_transparency($mode, $opacity) {
  370. $this->_pdf->setLineTransparency($mode, $opacity);
  371. }
  372. /**
  373. * Sets fill transparency
  374. * @see Cpdf::setFillTransparency()
  375. *
  376. * Valid blend modes are (case-sensitive):
  377. *
  378. * Normal, Multiply, Screen, Overlay, Darken, Lighten,
  379. * ColorDogde, ColorBurn, HardLight, SoftLight, Difference,
  380. * Exclusion
  381. *
  382. * @param string $mode the blending mode to use
  383. * @param float $opacity 0.0 fully transparent, 1.0 fully opaque
  384. */
  385. protected function _set_fill_transparency($mode, $opacity) {
  386. $this->_pdf->setFillTransparency($mode, $opacity);
  387. }
  388. /**
  389. * Sets the line style
  390. *
  391. * @see Cpdf::setLineStyle()
  392. *
  393. * @param float $width
  394. * @param string $cap
  395. * @param string $join
  396. * @param array $dash
  397. */
  398. protected function _set_line_style($width, $cap, $join, $dash) {
  399. $this->_pdf->setLineStyle($width, $cap, $join, $dash);
  400. }
  401. /**
  402. * Sets the opacity
  403. *
  404. * @param $opacity
  405. * @param $mode
  406. */
  407. function set_opacity($opacity, $mode = "Normal") {
  408. $this->_set_line_transparency($mode, $opacity);
  409. $this->_set_fill_transparency($mode, $opacity);
  410. }
  411. function set_default_view($view, $options = array()) {
  412. array_unshift($options, $view);
  413. call_user_func_array(array($this->_pdf, "openHere"), $options);
  414. }
  415. /**
  416. * Remaps y coords from 4th to 1st quadrant
  417. *
  418. * @param float $y
  419. * @return float
  420. */
  421. protected function y($y) {
  422. return $this->_height - $y;
  423. }
  424. // Canvas implementation
  425. function line($x1, $y1, $x2, $y2, $color, $width, $style = array()) {
  426. $this->_set_stroke_color($color);
  427. $this->_set_line_style($width, "butt", "", $style);
  428. $this->_pdf->line($x1, $this->y($y1),
  429. $x2, $this->y($y2));
  430. }
  431. function arc($x, $y, $r1, $r2, $astart, $aend, $color, $width, $style = array()) {
  432. $this->_set_stroke_color($color);
  433. $this->_set_line_style($width, "butt", "", $style);
  434. $this->_pdf->ellipse($x, $this->y($y), $r1, $r2, 0, 8, $astart, $aend, false, false, true, false);
  435. }
  436. //........................................................................
  437. /**
  438. * Convert a GIF or BMP image to a PNG image
  439. *
  440. * @param string $image_url
  441. * @param integer $type
  442. *
  443. * @throws DOMPDF_Exception
  444. * @return string The url of the newly converted image
  445. */
  446. protected function _convert_gif_bmp_to_png($image_url, $type) {
  447. $image_type = Image_Cache::type_to_ext($type);
  448. $func_name = "imagecreatefrom$image_type";
  449. if ( !function_exists($func_name) ) {
  450. throw new DOMPDF_Exception("Function $func_name() not found. Cannot convert $image_type image: $image_url. Please install the image PHP extension.");
  451. }
  452. set_error_handler("record_warnings");
  453. $im = $func_name($image_url);
  454. if ( $im ) {
  455. imageinterlace($im, false);
  456. $tmp_dir = $this->_dompdf->get_option("temp_dir");
  457. $tmp_name = tempnam($tmp_dir, "{$image_type}dompdf_img_");
  458. @unlink($tmp_name);
  459. $filename = "$tmp_name.png";
  460. $this->_image_cache[] = $filename;
  461. imagepng($im, $filename);
  462. imagedestroy($im);
  463. }
  464. else {
  465. $filename = Image_Cache::$broken_image;
  466. }
  467. restore_error_handler();
  468. return $filename;
  469. }
  470. function rectangle($x1, $y1, $w, $h, $color, $width, $style = array()) {
  471. $this->_set_stroke_color($color);
  472. $this->_set_line_style($width, "butt", "", $style);
  473. $this->_pdf->rectangle($x1, $this->y($y1) - $h, $w, $h);
  474. }
  475. function filled_rectangle($x1, $y1, $w, $h, $color) {
  476. $this->_set_fill_color($color);
  477. $this->_pdf->filledRectangle($x1, $this->y($y1) - $h, $w, $h);
  478. }
  479. function clipping_rectangle($x1, $y1, $w, $h) {
  480. $this->_pdf->clippingRectangle($x1, $this->y($y1) - $h, $w, $h);
  481. }
  482. function clipping_roundrectangle($x1, $y1, $w, $h, $rTL, $rTR, $rBR, $rBL) {
  483. $this->_pdf->clippingRectangleRounded($x1, $this->y($y1) - $h, $w, $h, $rTL, $rTR, $rBR, $rBL);
  484. }
  485. function clipping_end() {
  486. $this->_pdf->clippingEnd();
  487. }
  488. function save() {
  489. $this->_pdf->saveState();
  490. }
  491. function restore() {
  492. $this->_pdf->restoreState();
  493. }
  494. function rotate($angle, $x, $y) {
  495. $this->_pdf->rotate($angle, $x, $y);
  496. }
  497. function skew($angle_x, $angle_y, $x, $y) {
  498. $this->_pdf->skew($angle_x, $angle_y, $x, $y);
  499. }
  500. function scale($s_x, $s_y, $x, $y) {
  501. $this->_pdf->scale($s_x, $s_y, $x, $y);
  502. }
  503. function translate($t_x, $t_y) {
  504. $this->_pdf->translate($t_x, $t_y);
  505. }
  506. function transform($a, $b, $c, $d, $e, $f) {
  507. $this->_pdf->transform(array($a, $b, $c, $d, $e, $f));
  508. }
  509. function polygon($points, $color, $width = null, $style = array(), $fill = false) {
  510. $this->_set_fill_color($color);
  511. $this->_set_stroke_color($color);
  512. // Adjust y values
  513. for ( $i = 1; $i < count($points); $i += 2) {
  514. $points[$i] = $this->y($points[$i]);
  515. }
  516. $this->_pdf->polygon($points, count($points) / 2, $fill);
  517. }
  518. function circle($x, $y, $r1, $color, $width = null, $style = null, $fill = false) {
  519. $this->_set_fill_color($color);
  520. $this->_set_stroke_color($color);
  521. if ( !$fill && isset($width) ) {
  522. $this->_set_line_style($width, "round", "round", $style);
  523. }
  524. $this->_pdf->ellipse($x, $this->y($y), $r1, 0, 0, 8, 0, 360, 1, $fill);
  525. }
  526. function image($img, $x, $y, $w, $h, $resolution = "normal") {
  527. list($width, $height, $type) = dompdf_getimagesize($img, $this->_dompdf->get_http_context());
  528. $debug_png = $this->_dompdf->get_option("debug_png");
  529. if ($debug_png) print "[image:$img|$width|$height|$type]";
  530. switch ($type) {
  531. case IMAGETYPE_JPEG:
  532. if ($debug_png) print '!!!jpg!!!';
  533. $this->_pdf->addJpegFromFile($img, $x, $this->y($y) - $h, $w, $h);
  534. break;
  535. case IMAGETYPE_GIF:
  536. case IMAGETYPE_BMP:
  537. if ($debug_png) print '!!!bmp or gif!!!';
  538. // @todo use cache for BMP and GIF
  539. $img = $this->_convert_gif_bmp_to_png($img, $type);
  540. case IMAGETYPE_PNG:
  541. if ($debug_png) print '!!!png!!!';
  542. $this->_pdf->addPngFromFile($img, $x, $this->y($y) - $h, $w, $h);
  543. break;
  544. default:
  545. if ($debug_png) print '!!!unknown!!!';
  546. }
  547. }
  548. function text($x, $y, $text, $font, $size, $color = array(0,0,0), $word_space = 0.0, $char_space = 0.0, $angle = 0.0) {
  549. $pdf = $this->_pdf;
  550. $pdf->setColor($color);
  551. $font .= ".afm";
  552. $pdf->selectFont($font);
  553. //Font_Metrics::get_font_height($font, $size) ==
  554. //$this->get_font_height($font, $size) ==
  555. //$this->_pdf->selectFont($font),$this->_pdf->getFontHeight($size)
  556. //- FontBBoxheight+FontHeightOffset, scaled to $size, in pt
  557. //$this->_pdf->getFontDescender($size)
  558. //- Descender scaled to size
  559. //
  560. //$this->_pdf->fonts[$this->_pdf->currentFont] sizes:
  561. //['FontBBox'][0] left, ['FontBBox'][1] bottom, ['FontBBox'][2] right, ['FontBBox'][3] top
  562. //Maximum extent of all glyphs of the font from the baseline point
  563. //['Ascender'] maximum height above baseline except accents
  564. //['Descender'] maximum depth below baseline, negative number means below baseline
  565. //['FontHeightOffset'] manual enhancement of .afm files to trim windows fonts. currently not used.
  566. //Values are in 1/1000 pt for a font size of 1 pt
  567. //
  568. //['FontBBox'][1] should be close to ['Descender']
  569. //['FontBBox'][3] should be close to ['Ascender']+Accents
  570. //in practice, FontBBox values are a little bigger
  571. //
  572. //The text position is referenced to the baseline, not to the lower corner of the FontBBox,
  573. //for what the left,top corner is given.
  574. //FontBBox spans also the background box for the text.
  575. //If the lower corner would be used as reference point, the Descents of the glyphs would
  576. //hang over the background box border.
  577. //Therefore compensate only the extent above the Baseline.
  578. //
  579. //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>';
  580. //
  581. //$pdf->addText($x, $this->y($y) - ($pdf->fonts[$pdf->currentFont]['FontBBox'][3]*$size)/1000, $size, $text, $angle, $word_space, $char_space);
  582. $pdf->addText($x, $this->y($y) - $pdf->getFontHeight($size), $size, $text, $angle, $word_space, $char_space);
  583. }
  584. //........................................................................
  585. function javascript($code) {
  586. $this->_pdf->addJavascript($code);
  587. }
  588. //........................................................................
  589. /**
  590. * Add a named destination (similar to <a name="foo">...</a> in html)
  591. *
  592. * @param string $anchorname The name of the named destination
  593. */
  594. function add_named_dest($anchorname) {
  595. $this->_pdf->addDestination($anchorname, "Fit");
  596. }
  597. //........................................................................
  598. /**
  599. * Add a link to the pdf
  600. *
  601. * @param string $url The url to link to
  602. * @param float $x The x position of the link
  603. * @param float $y The y position of the link
  604. * @param float $width The width of the link
  605. * @param float $height The height of the link
  606. */
  607. function add_link($url, $x, $y, $width, $height) {
  608. $y = $this->y($y) - $height;
  609. if ( strpos($url, '#') === 0 ) {
  610. // Local link
  611. $name = substr($url,1);
  612. if ( $name ) {
  613. $this->_pdf->addInternalLink($name, $x, $y, $x + $width, $y + $height);
  614. }
  615. }
  616. else {
  617. $this->_pdf->addLink(rawurldecode($url), $x, $y, $x + $width, $y + $height);
  618. }
  619. }
  620. function get_text_width($text, $font, $size, $word_spacing = 0, $char_spacing = 0) {
  621. $this->_pdf->selectFont($font);
  622. $unicode = $this->_dompdf->get_option("enable_unicode");
  623. if (!$unicode) {
  624. $text = mb_convert_encoding($text, 'Windows-1252', 'UTF-8');
  625. }
  626. return $this->_pdf->getTextWidth($size, $text, $word_spacing, $char_spacing);
  627. }
  628. function register_string_subset($font, $string) {
  629. $this->_pdf->registerText($font, $string);
  630. }
  631. function get_font_height($font, $size) {
  632. $this->_pdf->selectFont($font);
  633. $ratio = $this->_dompdf->get_option("font_height_ratio");
  634. return $this->_pdf->getFontHeight($size) * $ratio;
  635. }
  636. /*function get_font_x_height($font, $size) {
  637. $this->_pdf->selectFont($font);
  638. $ratio = $this->_dompdf->get_option("font_height_ratio");
  639. return $this->_pdf->getFontXHeight($size) * $ratio;
  640. }*/
  641. function get_font_baseline($font, $size) {
  642. $ratio = $this->_dompdf->get_option("font_height_ratio");
  643. return $this->get_font_height($font, $size) / $ratio;
  644. }
  645. /**
  646. * Writes text at the specified x and y coordinates on every page
  647. *
  648. * The strings '{PAGE_NUM}' and '{PAGE_COUNT}' are automatically replaced
  649. * with their current values.
  650. *
  651. * See {@link Style::munge_color()} for the format of the colour array.
  652. *
  653. * @param float $x
  654. * @param float $y
  655. * @param string $text the text to write
  656. * @param string $font the font file to use
  657. * @param float $size the font size, in points
  658. * @param array $color
  659. * @param float $word_space word spacing adjustment
  660. * @param float $char_space char spacing adjustment
  661. * @param float $angle angle to write the text at, measured CW starting from the x-axis
  662. */
  663. function page_text($x, $y, $text, $font, $size, $color = array(0,0,0), $word_space = 0.0, $char_space = 0.0, $angle = 0.0) {
  664. $_t = "text";
  665. $this->_page_text[] = compact("_t", "x", "y", "text", "font", "size", "color", "word_space", "char_space", "angle");
  666. }
  667. /**
  668. * Processes a script on every page
  669. *
  670. * The variables $pdf, $PAGE_NUM, and $PAGE_COUNT are available.
  671. *
  672. * This function can be used to add page numbers to all pages
  673. * after the first one, for example.
  674. *
  675. * @param string $code the script code
  676. * @param string $type the language type for script
  677. */
  678. function page_script($code, $type = "text/php") {
  679. $_t = "script";
  680. $this->_page_text[] = compact("_t", "code", "type");
  681. }
  682. function new_page() {
  683. $this->_page_number++;
  684. $this->_page_count++;
  685. $ret = $this->_pdf->newPage();
  686. $this->_pages[] = $ret;
  687. return $ret;
  688. }
  689. /**
  690. * Add text to each page after rendering is complete
  691. */
  692. protected function _add_page_text() {
  693. if ( !count($this->_page_text) ) {
  694. return;
  695. }
  696. $page_number = 1;
  697. $eval = null;
  698. foreach ($this->_pages as $pid) {
  699. $this->reopen_object($pid);
  700. foreach ($this->_page_text as $pt) {
  701. extract($pt);
  702. switch ($_t) {
  703. case "text":
  704. $text = str_replace(array("{PAGE_NUM}","{PAGE_COUNT}"),
  705. array($page_number, $this->_page_count), $text);
  706. $this->text($x, $y, $text, $font, $size, $color, $word_space, $char_space, $angle);
  707. break;
  708. case "script":
  709. if ( !$eval ) {
  710. $eval = new PHP_Evaluator($this);
  711. }
  712. $eval->evaluate($code, array('PAGE_NUM' => $page_number, 'PAGE_COUNT' => $this->_page_count));
  713. break;
  714. }
  715. }
  716. $this->close_object();
  717. $page_number++;
  718. }
  719. }
  720. /**
  721. * Streams the PDF directly to the browser
  722. *
  723. * @param string $filename the name of the PDF file
  724. * @param array $options associative array, 'Attachment' => 0 or 1, 'compress' => 1 or 0
  725. */
  726. function stream($filename, $options = null) {
  727. // Add page text
  728. $this->_add_page_text();
  729. $options["Content-Disposition"] = $filename;
  730. $this->_pdf->stream($options);
  731. }
  732. /**
  733. * Returns the PDF as a string
  734. *
  735. * @param array $options Output options
  736. * @return string
  737. */
  738. function output($options = null) {
  739. $this->_add_page_text();
  740. $debug = isset($options["compress"]) && $options["compress"] != 1;
  741. return $this->_pdf->output($debug);
  742. }
  743. /**
  744. * Returns logging messages generated by the Cpdf class
  745. *
  746. * @return string
  747. */
  748. function get_messages() {
  749. return $this->_pdf->messages;
  750. }
  751. }