pdflib_adapter.cls.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile: pdflib_adapter.cls.php,v $
  6. * Created on: 2005-02-28
  7. *
  8. * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library in the file LICENSE.LGPL; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  23. * 02111-1307 USA
  24. *
  25. * Alternatively, you may distribute this software under the terms of the
  26. * PHP License, version 3.0 or later. A copy of this license should have
  27. * been distributed with this file in the file LICENSE.PHP . If this is not
  28. * the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
  29. *
  30. * The latest version of DOMPDF might be available at:
  31. * http://www.dompdf.com/
  32. *
  33. * @link http://www.dompdf.com/
  34. * @copyright 2004 Benj Carson
  35. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  36. * @contributor Helmut Tischer <htischer@weihenstephan.org>
  37. * @package dompdf
  38. *
  39. * Changes
  40. * @contributor Helmut Tischer <htischer@weihenstephan.org>
  41. * @version 0.5.1.htischer.20090507
  42. * - Clarify temp file name, optional debug output for temp file tracking
  43. */
  44. /* $Id: pdflib_adapter.cls.php 217 2010-03-11 23:03:57Z ryan.masten $ */
  45. /**
  46. * PDF rendering interface
  47. *
  48. * PDFLib_Adapter provides a simple, stateless interface to the one
  49. * provided by PDFLib.
  50. *
  51. * Unless otherwise mentioned, all dimensions are in points (1/72 in).
  52. * The coordinate origin is in the top left corner and y values
  53. * increase downwards.
  54. *
  55. * See {@link http://www.pdflib.com/} for more complete documentation
  56. * on the underlying PDFlib functions.
  57. *
  58. * @package dompdf
  59. */
  60. class PDFLib_Adapter implements Canvas {
  61. /**
  62. * Dimensions of paper sizes in points
  63. *
  64. * @var array;
  65. */
  66. static public $PAPER_SIZES = array(); // Set to
  67. // CPDF_Adapter::$PAPER_SIZES below.
  68. /**
  69. * Fudge factor to adjust reported font heights
  70. *
  71. * CPDF reports larger font heights than PDFLib. This factor
  72. * adjusts the height reported by get_font_height().
  73. *
  74. * @var float
  75. */
  76. const FONT_HEIGHT_SCALE = 1.2;
  77. /**
  78. * Whether to create PDFs in memory or on disk
  79. *
  80. * @var bool
  81. */
  82. static $IN_MEMORY = true;
  83. /**
  84. * Instance of PDFLib class
  85. *
  86. * @var PDFlib
  87. */
  88. private $_pdf;
  89. /**
  90. * Name of temporary file used for PDFs created on disk
  91. *
  92. * @var string
  93. */
  94. private $_file;
  95. /**
  96. * PDF width, in points
  97. *
  98. * @var float
  99. */
  100. private $_width;
  101. /**
  102. * PDF height, in points
  103. *
  104. * @var height
  105. */
  106. private $_height;
  107. /**
  108. * Last fill colour used
  109. *
  110. * @var array
  111. */
  112. private $_last_fill_color;
  113. /**
  114. * Last stroke colour used
  115. *
  116. * @var array
  117. */
  118. private $_last_stroke_color;
  119. /**
  120. * Cache of image handles
  121. *
  122. * @var array
  123. */
  124. private $_imgs;
  125. /**
  126. * Cache of font handles
  127. *
  128. * @var array
  129. */
  130. private $_fonts;
  131. /**
  132. * List of objects (templates) to add to multiple pages
  133. *
  134. * @var array
  135. */
  136. private $_objs;
  137. /**
  138. * Current page number
  139. *
  140. * @var int
  141. */
  142. private $_page_number;
  143. /**
  144. * Total number of pages
  145. *
  146. * @var int
  147. */
  148. private $_page_count;
  149. /**
  150. * Text to display on every page
  151. *
  152. * @var array
  153. */
  154. private $_page_text;
  155. /**
  156. * Array of pages for accesing after rendering is initially complete
  157. *
  158. * @var array
  159. */
  160. private $_pages;
  161. /**
  162. * Class constructor
  163. *
  164. * @param mixed $paper The size of paper to use either a string (see {@link CPDF_Adapter::$PAPER_SIZES}) or
  165. * an array(xmin,ymin,xmax,ymax)
  166. * @param string $orientation The orientation of the document (either 'landscape' or 'portrait')
  167. */
  168. function __construct($paper = "letter", $orientation = "portrait") {
  169. if ( is_array($paper) )
  170. $size = $paper;
  171. else if ( isset(self::$PAPER_SIZES[mb_strtolower($paper)]) )
  172. $size = self::$PAPER_SIZES[mb_strtolower($paper)];
  173. else
  174. $size = self::$PAPER_SIZES["letter"];
  175. if ( mb_strtolower($orientation) === "landscape" ) {
  176. $a = $size[3];
  177. $size[3] = $size[2];
  178. $size[2] = $a;
  179. }
  180. $this->_width = $size[2] - $size[0];
  181. $this->_height= $size[3] - $size[1];
  182. $this->_pdf = new PDFLib();
  183. if ( defined("DOMPDF_PDFLIB_LICENSE") )
  184. $this->_pdf->set_parameter( "license", DOMPDF_PDFLIB_LICENSE);
  185. $this->_pdf->set_parameter("textformat", "utf8");
  186. $this->_pdf->set_parameter("fontwarning", "false");
  187. $this->_pdf->set_info("Creator", "DOMPDF Converter");
  188. // Silence pedantic warnings about missing TZ settings
  189. if ( function_exists("date_default_timezone_get") ) {
  190. $tz = @date_default_timezone_get();
  191. date_default_timezone_set("UTC");
  192. $this->_pdf->set_info("Date", date("Y-m-d"));
  193. date_default_timezone_set($tz);
  194. } else {
  195. $this->_pdf->set_info("Date", date("Y-m-d"));
  196. }
  197. if ( self::$IN_MEMORY )
  198. $this->_pdf->begin_document("","");
  199. else {
  200. $this->_file = tempnam(DOMPDF_TEMP_DIR, "libdompdf_pdf_").'.pdf';
  201. $this->_pdf->begin_document($this->_file,"");
  202. }
  203. $this->_pdf->begin_page_ext($this->_width, $this->_height, "");
  204. $this->_page_number = $this->_page_count = 1;
  205. $this->_page_text = array();
  206. $this->_imgs = array();
  207. $this->_fonts = array();
  208. $this->_objs = array();
  209. // Set up font paths
  210. $families = Font_Metrics::get_font_families();
  211. foreach ($families as $family => $files) {
  212. foreach ($files as $style => $file) {
  213. $face = basename($file);
  214. // Prefer ttfs to afms
  215. if ( file_exists($file.".ttf") ) {
  216. $outline = "$file.ttf";
  217. $afm = null;
  218. } else if ( file_exists($file .".TTF") ) {
  219. $outline = "$file.TTF";
  220. $afm = null;
  221. } else if ( file_exists($file . ".pfb") ) {
  222. $outline = "$file.pfb";
  223. if ( file_exists($file . ".afm") )
  224. $afm = "$file.afm";
  225. } else if ( file_exists($file . ".PFB") ) {
  226. $outline = "$file.PFB";
  227. if ( file_exists($file . ".AFM") )
  228. $afm = "$file.AFM";
  229. } else
  230. continue;
  231. $this->_pdf->set_parameter("FontOutline", "\{$face\}=\{$outline\}");
  232. if ( !is_null($afm) )
  233. $this->_pdf->set_parameter("FontAFM", "\{$face\}=\{$afm\}");
  234. }
  235. }
  236. }
  237. /**
  238. * Close the pdf
  239. */
  240. protected function _close() {
  241. $this->_place_objects();
  242. // Close all pages
  243. $this->_pdf->suspend_page("");
  244. for ($p = 1; $p <= $this->_page_count; $p++) {
  245. $this->_pdf->resume_page("pagenumber=$p");
  246. $this->_pdf->end_page_ext("");
  247. }
  248. $this->_pdf->end_document("");
  249. }
  250. /**
  251. * Returns the PDFLib instance
  252. *
  253. * @return PDFLib
  254. */
  255. function get_pdflib() { return $this->_pdf; }
  256. /**
  257. * Add meta information to the PDF
  258. *
  259. * @param string $label label of the value (Creator, Producter, etc.)
  260. * @param string $value the text to set
  261. */
  262. function add_info($label, $value) {
  263. $this->_pdf->set_info($label, $value);
  264. }
  265. /**
  266. * Opens a new 'object' (template in PDFLib-speak)
  267. *
  268. * While an object is open, all drawing actions are recorded to the
  269. * object instead of being drawn on the current page. Objects can
  270. * be added later to a specific page or to several pages.
  271. *
  272. * The return value is an integer ID for the new object.
  273. *
  274. * @see PDFLib_Adapter::close_object()
  275. * @see PDFLib_Adapter::add_object()
  276. *
  277. * @return int
  278. */
  279. function open_object() {
  280. $this->_pdf->suspend_page("");
  281. $ret = $this->_pdf->begin_template($this->_width, $this->_height);
  282. $this->_pdf->save();
  283. $this->_objs[$ret] = array("start_page" => $this->_page_number);
  284. return $ret;
  285. }
  286. /**
  287. * Reopen an existing object (NOT IMPLEMENTED)
  288. *
  289. * PDFLib does not seem to support reopening templates.
  290. *
  291. * @param int $object the ID of a previously opened object
  292. */
  293. function reopen_object($object) {
  294. throw new DOMPDF_Exception("PDFLib does not support reopening objects.");
  295. }
  296. /**
  297. * Close the current template
  298. *
  299. * @see PDFLib_Adapter::open_object()
  300. */
  301. function close_object() {
  302. $this->_pdf->restore();
  303. $this->_pdf->end_template();
  304. $this->_pdf->resume_page("pagenumber=".$this->_page_number);
  305. }
  306. /**
  307. * Adds the specified object to the document
  308. *
  309. * $where can be one of:
  310. * - 'add' add to current page only
  311. * - 'all' add to every page from the current one onwards
  312. * - 'odd' add to all odd numbered pages from now on
  313. * - 'even' add to all even numbered pages from now on
  314. * - 'next' add the object to the next page only
  315. * - 'nextodd' add to all odd numbered pages from the next one
  316. * - 'nexteven' add to all even numbered pages from the next one
  317. *
  318. * @param int $object the object handle returned by open_object()
  319. * @param string $where
  320. */
  321. function add_object($object, $where = 'all') {
  322. if ( mb_strpos($where, "next") !== false ) {
  323. $this->_objs[$object]["start_page"]++;
  324. $where = str_replace("next", "", $where);
  325. if ( $where == "" )
  326. $where = "add";
  327. }
  328. $this->_objs[$object]["where"] = $where;
  329. }
  330. /**
  331. * Stops the specified template from appearing in the document.
  332. *
  333. * The object will stop being displayed on the page following the
  334. * current one.
  335. *
  336. * @param int $object
  337. */
  338. function stop_object($object) {
  339. if ( !isset($this->_objs[$object]) )
  340. return;
  341. $start = $this->_objs[$object]["start_page"];
  342. $where = $this->_objs[$object]["where"];
  343. // Place the object on this page if required
  344. if ( $this->_page_number >= $start &&
  345. (($this->_page_number % 2 == 0 && $where === "even") ||
  346. ($this->_page_number % 2 == 1 && $where === "odd") ||
  347. ($where === "all")) )
  348. $this->_pdf->fit_image($object,0,0,"");
  349. unset($this->_objs[$object]);
  350. }
  351. /**
  352. * Add all active objects to the current page
  353. */
  354. protected function _place_objects() {
  355. foreach ( $this->_objs as $obj => $props ) {
  356. $start = $props["start_page"];
  357. $where = $props["where"];
  358. // Place the object on this page if required
  359. if ( $this->_page_number >= $start &&
  360. (($this->_page_number % 2 == 0 && $where === "even") ||
  361. ($this->_page_number % 2 == 1 && $where === "odd") ||
  362. ($where === "all")) ) {
  363. $this->_pdf->fit_image($obj,0,0,"");
  364. }
  365. }
  366. }
  367. function get_width() { return $this->_width; }
  368. function get_height() { return $this->_height; }
  369. function get_page_number() { return $this->_page_number; }
  370. function get_page_count() { return $this->_page_count; }
  371. function set_page_number($num) { $this->_page_number = (int)$num; }
  372. function set_page_count($count) { $this->_page_count = (int)$count; }
  373. /**
  374. * Sets the line style
  375. *
  376. * @param float width
  377. * @param string corner
  378. * @param string join
  379. * @param array dash
  380. */
  381. protected function _set_line_style($width, $cap, $join, $dash) {
  382. if ( count($dash) == 1 )
  383. $dash[] = $dash[0];
  384. if ( count($dash) > 1 )
  385. $this->_pdf->setdashpattern("dasharray={" . join(" ", $dash) . "}");
  386. else
  387. $this->_pdf->setdash(0,0);
  388. switch ( $join ) {
  389. case "miter":
  390. $this->_pdf->setlinejoin(0);
  391. break;
  392. case "round":
  393. $this->_pdf->setlinejoin(1);
  394. break;
  395. case "bevel":
  396. $this->_pdf->setlinejoin(2);
  397. break;
  398. default:
  399. break;
  400. }
  401. switch ( $cap ) {
  402. case "butt":
  403. $this->_pdf->setlinecap(0);
  404. break;
  405. case "round":
  406. $this->_pdf->setlinecap(1);
  407. break;
  408. case "square":
  409. $this->_pdf->setlinecap(2);
  410. break;
  411. default:
  412. break;
  413. }
  414. $this->_pdf->setlinewidth($width);
  415. }
  416. /**
  417. * Sets the line color
  418. *
  419. * @param array $color array(r,g,b)
  420. */
  421. protected function _set_stroke_color($color) {
  422. if($this->_last_stroke_color == $color)
  423. return;
  424. $this->_last_stroke_color = $color;
  425. list($r,$g,$b) = $color;
  426. $this->_pdf->setcolor("stroke", "rgb", $r, $g, $b, 0);
  427. }
  428. /**
  429. * Sets the fill color
  430. *
  431. * @param array $color array(r,g,b)
  432. */
  433. protected function _set_fill_color($color) {
  434. if($this->_last_fill_color == $color)
  435. return;
  436. $this->_last_fill_color = $color;
  437. list($r,$g,$b) = $color;
  438. $this->_pdf->setcolor("fill", "rgb", $r, $g, $b, 0);
  439. }
  440. /**
  441. * Loads a specific font and stores the corresponding descriptor.
  442. *
  443. * @param string $font
  444. * @return int the font descriptor for the font
  445. */
  446. protected function _load_font($font, $encoding = null, $options = "") {
  447. // Check if the font is a native PDF font
  448. // Embed non-native fonts
  449. $native_fonts = array("courier", "courier-bold", "courier-oblique", "courier-boldoblique",
  450. "helvetica", "helvetica-bold", "helvetica-oblique", "helvetica-boldoblique",
  451. "times-roman", "times-bold", "times-italic", "times-bolditalic",
  452. "symbol", "zapfdinbats");
  453. $test = strtolower(basename($font));
  454. if ( in_array($test, $native_fonts) ) {
  455. $font = basename($font);
  456. } else {
  457. // Embed non-native fonts
  458. $options .= " embedding=true";
  459. }
  460. if ( is_null($encoding) ) {
  461. // Unicode encoding is only available for the commerical
  462. // version of PDFlib and not PDFlib-Lite
  463. if ( defined("DOMPDF_PDFLIB_LICENSE") )
  464. $encoding = "unicode";
  465. else
  466. $encoding = "auto";
  467. }
  468. $key = $font .":". $encoding .":". $options;
  469. if ( isset($this->_fonts[$key]) )
  470. return $this->_fonts[$key];
  471. else {
  472. $this->_fonts[$key] = $this->_pdf->load_font($font, $encoding, $options);
  473. return $this->_fonts[$key];
  474. }
  475. }
  476. /**
  477. * Remaps y coords from 4th to 1st quadrant
  478. *
  479. * @param float $y
  480. * @return float
  481. */
  482. protected function y($y) { return $this->_height - $y; }
  483. //........................................................................
  484. function line($x1, $y1, $x2, $y2, $color, $width, $style = null) {
  485. $this->_set_line_style($width, "butt", "", $style);
  486. $this->_set_stroke_color($color);
  487. $y1 = $this->y($y1);
  488. $y2 = $this->y($y2);
  489. $this->_pdf->moveto($x1,$y1);
  490. $this->_pdf->lineto($x2, $y2);
  491. $this->_pdf->stroke();
  492. }
  493. //........................................................................
  494. function rectangle($x1, $y1, $w, $h, $color, $width, $style = null) {
  495. $this->_set_stroke_color($color);
  496. $this->_set_line_style($width, "square", "miter", $style);
  497. $y1 = $this->y($y1) - $h;
  498. $this->_pdf->rect($x1, $y1, $w, $h);
  499. $this->_pdf->stroke();
  500. }
  501. //........................................................................
  502. function filled_rectangle($x1, $y1, $w, $h, $color) {
  503. $this->_set_fill_color($color);
  504. $y1 = $this->y($y1) - $h;
  505. $this->_pdf->rect($x1, $y1, $w, $h);
  506. $this->_pdf->fill();
  507. }
  508. //........................................................................
  509. function polygon($points, $color, $width = null, $style = null, $fill = false) {
  510. $this->_set_fill_color($color);
  511. $this->_set_stroke_color($color);
  512. if ( !$fill && isset($width) )
  513. $this->_set_line_style($width, "square", "miter", $style);
  514. $y = $this->y(array_pop($points));
  515. $x = array_pop($points);
  516. $this->_pdf->moveto($x,$y);
  517. while (count($points) > 1) {
  518. $y = $this->y(array_pop($points));
  519. $x = array_pop($points);
  520. $this->_pdf->lineto($x,$y);
  521. }
  522. if ( $fill )
  523. $this->_pdf->fill();
  524. else
  525. $this->_pdf->closepath_stroke();
  526. }
  527. //........................................................................
  528. function circle($x, $y, $r, $color, $width = null, $style = null, $fill = false) {
  529. $this->_set_fill_color($color);
  530. $this->_set_stroke_color($color);
  531. if ( !$fill && isset($width) )
  532. $this->_set_line_style($width, "round", "round", $style);
  533. $y = $this->y($y);
  534. $this->_pdf->circle($x, $y, $r);
  535. if ( $fill )
  536. $this->_pdf->fill();
  537. else
  538. $this->_pdf->stroke();
  539. }
  540. //........................................................................
  541. function image($img_url, $img_type, $x, $y, $w, $h) {
  542. $w = (int)$w;
  543. $h = (int)$h;
  544. $img_type = strtolower($img_type);
  545. if ( $img_type === "jpg" )
  546. $img_type = "jpeg";
  547. if ( isset($this->_imgs[$img_url]) )
  548. $img = $this->_imgs[$img_url];
  549. else {
  550. $img = $this->_imgs[$img_url] = $this->_pdf->load_image($img_type, $img_url, "");
  551. }
  552. $y = $this->y($y) - $h;
  553. $this->_pdf->fit_image($img, $x, $y, 'boxsize={'. "$w $h" .'} fitmethod=entire');
  554. }
  555. //........................................................................
  556. function text($x, $y, $text, $font, $size, $color = array(0,0,0), $adjust = 0, $angle = 0) {
  557. $fh = $this->_load_font($font);
  558. $this->_pdf->setfont($fh, $size);
  559. $this->_set_fill_color($color);
  560. $y = $this->y($y) - Font_Metrics::get_font_height($font, $size);
  561. $adjust = (float)$adjust;
  562. $angle = -(float)$angle;
  563. $this->_pdf->fit_textline($text, $x, $y, "rotate=$angle wordspacing=$adjust");
  564. }
  565. //........................................................................
  566. function javascript($code) {
  567. // Not implemented
  568. }
  569. //........................................................................
  570. /**
  571. * Add a named destination (similar to <a name="foo">...</a> in html)
  572. *
  573. * @param string $anchorname The name of the named destination
  574. */
  575. function add_named_dest($anchorname) {
  576. $this->_pdf->add_nameddest($anchorname,"");
  577. }
  578. //........................................................................
  579. /**
  580. * Add a link to the pdf
  581. *
  582. * @param string $url The url to link to
  583. * @param float $x The x position of the link
  584. * @param float $y The y position of the link
  585. * @param float $width The width of the link
  586. * @param float $height The height of the link
  587. */
  588. function add_link($url, $x, $y, $width, $height) {
  589. $y = $this->y($y) - $height;
  590. if ( strpos($url, '#') === 0 ) {
  591. // Local link
  592. $name = substr($url,1);
  593. if ( $name )
  594. $this->_pdf->create_annotation($x, $y, $x + $width, $y + $height, 'Link', "contents={$url} destname=". substr($url,1) . " linewidth=0");
  595. } else {
  596. list($proto, $host, $path, $file) = explode_url($url);
  597. if ( $proto == "" || $proto === "file://" )
  598. return; // Local links are not allowed
  599. $url = build_url($proto, $host, $path, $file);
  600. $url = '{' . rawurldecode($url) . '}';
  601. $action = $this->_pdf->create_action("URI", "url=" . $url);
  602. $this->_pdf->create_annotation($x, $y, $x + $width, $y + $height, 'Link', "contents={$url} action={activate=$action} linewidth=0");
  603. }
  604. }
  605. //........................................................................
  606. function get_text_width($text, $font, $size, $spacing = 0) {
  607. $fh = $this->_load_font($font);
  608. // Determine the additional width due to extra spacing
  609. $num_spaces = mb_substr_count($text," ");
  610. $delta = $spacing * $num_spaces;
  611. return $this->_pdf->stringwidth($text, $fh, $size) + $delta;
  612. }
  613. //........................................................................
  614. function get_font_height($font, $size) {
  615. $fh = $this->_load_font($font);
  616. $this->_pdf->setfont($fh, $size);
  617. $asc = $this->_pdf->get_value("ascender", $fh);
  618. $desc = $this->_pdf->get_value("descender", $fh);
  619. // $desc is usually < 0,
  620. return self::FONT_HEIGHT_SCALE * $size * ($asc - $desc);
  621. }
  622. //........................................................................
  623. /**
  624. * Writes text at the specified x and y coordinates on every page
  625. *
  626. * The strings '{PAGE_NUM}' and '{PAGE_COUNT}' are automatically replaced
  627. * with their current values.
  628. *
  629. * See {@link Style::munge_colour()} for the format of the colour array.
  630. *
  631. * @param float $x
  632. * @param float $y
  633. * @param string $text the text to write
  634. * @param string $font the font file to use
  635. * @param float $size the font size, in points
  636. * @param array $color
  637. * @param float $adjust word spacing adjustment
  638. * @param float $angle angle to write the text at, measured CW starting from the x-axis
  639. */
  640. function page_text($x, $y, $text, $font, $size, $color = array(0,0,0),
  641. $adjust = 0, $angle = 0, $blend = "Normal", $opacity = 1.0) {
  642. $_t = "text";
  643. $this->_page_text[] = compact("_t", "x", "y", "text", "font", "size", "color", "adjust", "angle");
  644. }
  645. //........................................................................
  646. /**
  647. * Processes a script on every page
  648. *
  649. * The variables $pdf, $PAGE_NUM, and $PAGE_COUNT are available.
  650. *
  651. * This function can be used to add page numbers to all pages
  652. * after the first one, for example.
  653. *
  654. * @param string $code the script code
  655. * @param string $type the language type for script
  656. */
  657. function page_script($code, $type = "text/php") {
  658. $_t = "script";
  659. $this->_page_text[] = compact("_t", "code", "type");
  660. }
  661. //........................................................................
  662. function new_page() {
  663. // Add objects to the current page
  664. $this->_place_objects();
  665. $this->_pdf->suspend_page("");
  666. $this->_pdf->begin_page_ext($this->_width, $this->_height, "");
  667. $this->_page_number = ++$this->_page_count;
  668. }
  669. //........................................................................
  670. /**
  671. * Add text to each page after rendering is complete
  672. */
  673. protected function _add_page_text() {
  674. if ( !count($this->_page_text) )
  675. return;
  676. $this->_pdf->suspend_page("");
  677. for ($p = 1; $p <= $this->_page_count; $p++) {
  678. $this->_pdf->resume_page("pagenumber=$p");
  679. foreach ($this->_page_text as $pt) {
  680. extract($pt);
  681. switch ($_t) {
  682. case "text":
  683. $text = str_replace(array("{PAGE_NUM}","{PAGE_COUNT}"),
  684. array($p, $this->_page_count), $text);
  685. $this->text($x, $y, $text, $font, $size, $color, $adjust, $angle);
  686. break;
  687. case "script":
  688. if (!$eval) {
  689. $eval = new PHP_Evaluator($this);
  690. }
  691. $eval->evaluate($code, array('PAGE_NUM' => $p, 'PAGE_COUNT' => $this->_page_count));
  692. break;
  693. }
  694. }
  695. $this->_pdf->suspend_page("");
  696. }
  697. $this->_pdf->resume_page("pagenumber=".$this->_page_number);
  698. }
  699. //........................................................................
  700. function stream($filename, $options = null) {
  701. // Add page text
  702. $this->_add_page_text();
  703. if ( isset($options["compress"]) && $options["compress"] != 1 )
  704. $this->_pdf->set_value("compress", 0);
  705. else
  706. $this->_pdf->set_value("compress", 6);
  707. $this->_close();
  708. if ( self::$IN_MEMORY ) {
  709. $data = $this->_pdf->get_buffer();
  710. $size = strlen($data);
  711. } else
  712. $size = filesize($this->_file);
  713. $filename = str_replace(array("\n","'"),"", $filename);
  714. $attach = (isset($options["Attachment"]) && $options["Attachment"]) ? "attachment" : "inline";
  715. header("Cache-Control: private");
  716. header("Content-type: application/pdf");
  717. header("Content-Disposition: $attach; filename=\"$filename\"");
  718. //header("Content-length: " . $size);
  719. if ( self::$IN_MEMORY )
  720. echo $data;
  721. else {
  722. // Chunked readfile()
  723. $chunk = (1 << 21); // 2 MB
  724. $fh = fopen($this->_file, "rb");
  725. if ( !$fh )
  726. throw new DOMPDF_Exception("Unable to load temporary PDF file: " . $this->_file);
  727. while ( !feof($fh) )
  728. echo fread($fh,$chunk);
  729. fclose($fh);
  730. //debugpng
  731. if (DEBUGPNG) print '[pdflib stream unlink '.$this->_file.']';
  732. if (!DEBUGKEEPTEMP)
  733. unlink($this->_file);
  734. $this->_file = null;
  735. }
  736. flush();
  737. }
  738. //........................................................................
  739. function output($options = null) {
  740. // Add page text
  741. $this->_add_page_text();
  742. if ( isset($options["compress"]) && $options["compress"] != 1 )
  743. $this->_pdf->set_value("compress", 0);
  744. else
  745. $this->_pdf->set_value("compress", 6);
  746. $this->_close();
  747. if ( self::$IN_MEMORY )
  748. $data = $this->_pdf->get_buffer();
  749. else {
  750. $data = file_get_contents($this->_file);
  751. //debugpng
  752. if (DEBUGPNG) print '[pdflib output unlink '.$this->_file.']';
  753. if (!DEBUGKEEPTEMP)
  754. unlink($this->_file);
  755. $this->_file = null;
  756. }
  757. return $data;
  758. }
  759. }
  760. // Workaround for idiotic limitation on statics...
  761. PDFLib_Adapter::$PAPER_SIZES = CPDF_Adapter::$PAPER_SIZES;