/library/dompdf/include/pdflib_adapter.cls.php

https://code.google.com/p/ecartcommerce/ · PHP · 891 lines · 422 code · 186 blank · 283 comment · 77 complexity · a67ba2cd35f0cedac489a408eb11f91a MD5 · raw file

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