DOMPDF

DOMPDF - PHP5 HTML to PDF renderer.

Defined (1)

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

/lib/dompdf/include/dompdf.cls.php  
  1. class DOMPDF { 
  2.  
  3. /** 
  4. * DomDocument representing the HTML document 
  5. * @var DOMDocument 
  6. */ 
  7. protected $_xml; 
  8.  
  9. /** 
  10. * Frame_Tree derived from the DOM tree 
  11. * @var Frame_Tree 
  12. */ 
  13. protected $_tree; 
  14.  
  15. /** 
  16. * Stylesheet for the document 
  17. * @var Stylesheet 
  18. */ 
  19. protected $_css; 
  20.  
  21. /** 
  22. * Actual PDF renderer 
  23. * @var Canvas 
  24. */ 
  25. protected $_pdf; 
  26.  
  27. /** 
  28. * Desired paper size ('letter', 'legal', 'A4', etc.) 
  29. * @var string 
  30. */ 
  31. protected $_paper_size; 
  32.  
  33. /** 
  34. * Paper orientation ('portrait' or 'landscape') 
  35. * @var string 
  36. */ 
  37. protected $_paper_orientation; 
  38.  
  39. /** 
  40. * Callbacks on new page and new element 
  41. * @var array 
  42. */ 
  43. protected $_callbacks; 
  44.  
  45. /** 
  46. * Experimental caching capability 
  47. * @var string 
  48. */ 
  49. private $_cache_id; 
  50.  
  51. /** 
  52. * Base hostname 
  53. * Used for relative paths/urls 
  54. * @var string 
  55. */ 
  56. protected $_base_host; 
  57.  
  58. /** 
  59. * Absolute base path 
  60. * Used for relative paths/urls 
  61. * @var string 
  62. */ 
  63. protected $_base_path; 
  64.  
  65. /** 
  66. * Protcol used to request file (file://, http://, etc) 
  67. * @var string 
  68. */ 
  69. protected $_protocol; 
  70.  
  71. /** 
  72. * HTTP context created with stream_context_create() 
  73. * Will be used for file_get_contents 
  74. * @var resource 
  75. */ 
  76. protected $_http_context; 
  77.  
  78. /** 
  79. * Timestamp of the script start time 
  80. * @var int 
  81. */ 
  82. private $_start_time = null; 
  83.  
  84. /** 
  85. * The system's locale 
  86. * @var string 
  87. */ 
  88. private $_system_locale = null; 
  89.  
  90. /** 
  91. * Tells if the system's locale is the C standard one 
  92. * @var bool 
  93. */ 
  94. private $_locale_standard = false; 
  95.  
  96. /** 
  97. * The default view of the PDF in the viewer 
  98. * @var string 
  99. */ 
  100. private $_default_view = "Fit"; 
  101.  
  102. /** 
  103. * The default view options of the PDF in the viewer 
  104. * @var array 
  105. */ 
  106. private $_default_view_options = array(); 
  107.  
  108. /** 
  109. * Tells wether the DOM document is in quirksmode (experimental) 
  110. * @var bool 
  111. */ 
  112. private $_quirksmode = false; 
  113.  
  114. /** 
  115. * The list of built-in fonts 
  116. * @var array 
  117. */ 
  118. public static $native_fonts = array( 
  119. "courier", "courier-bold", "courier-oblique", "courier-boldoblique",  
  120. "helvetica", "helvetica-bold", "helvetica-oblique", "helvetica-boldoblique",  
  121. "times-roman", "times-bold", "times-italic", "times-bolditalic",  
  122. "symbol", "zapfdinbats" 
  123. ); 
  124.  
  125. private $_options = array( 
  126. // Directories 
  127. "temp_dir" => DOMPDF_TEMP_DIR,  
  128. "font_dir" => DOMPDF_FONT_DIR,  
  129. "font_cache" => DOMPDF_FONT_CACHE,  
  130. "chroot" => DOMPDF_CHROOT,  
  131. "log_output_file" => DOMPDF_LOG_OUTPUT_FILE,  
  132.  
  133. // Rendering 
  134. "default_media_type" => DOMPDF_DEFAULT_MEDIA_TYPE,  
  135. "default_paper_size" => DOMPDF_DEFAULT_PAPER_SIZE,  
  136. "default_font" => DOMPDF_DEFAULT_FONT,  
  137. "dpi" => DOMPDF_DPI,  
  138. "font_height_ratio" => DOMPDF_FONT_HEIGHT_RATIO,  
  139.  
  140. // Features 
  141. "enable_unicode" => DOMPDF_UNICODE_ENABLED,  
  142. "enable_php" => DOMPDF_ENABLE_PHP,  
  143. "enable_remote" => DOMPDF_ENABLE_REMOTE,  
  144. "enable_css_float" => DOMPDF_ENABLE_CSS_FLOAT,  
  145. "enable_javascript" => DOMPDF_ENABLE_JAVASCRIPT,  
  146. "enable_html5_parser" => DOMPDF_ENABLE_HTML5PARSER,  
  147. "enable_font_subsetting" => DOMPDF_ENABLE_FONTSUBSETTING,  
  148.  
  149. // Debug 
  150. "debug_png" => DEBUGPNG,  
  151. "debug_keep_temp" => DEBUGKEEPTEMP,  
  152. "debug_css" => DEBUGCSS,  
  153. "debug_layout" => DEBUG_LAYOUT,  
  154. "debug_layout_lines" => DEBUG_LAYOUT_LINES,  
  155. "debug_layout_blocks" => DEBUG_LAYOUT_BLOCKS,  
  156. "debug_layout_inline" => DEBUG_LAYOUT_INLINE,  
  157. "debug_layout_padding_box" => DEBUG_LAYOUT_PADDINGBOX,  
  158.  
  159. // Admin 
  160. "admin_username" => DOMPDF_ADMIN_USERNAME,  
  161. "admin_password" => DOMPDF_ADMIN_PASSWORD,  
  162. ); 
  163.  
  164. /** 
  165. * Class constructor 
  166. */ 
  167. function __construct() { 
  168. $this->_locale_standard = sprintf('%.1f', 1.0) == '1.0'; 
  169.  
  170. $this->save_locale(); 
  171.  
  172. $this->_messages = array(); 
  173. $this->_css = new Stylesheet($this); 
  174. $this->_pdf = null; 
  175. $this->_paper_size = DOMPDF_DEFAULT_PAPER_SIZE; 
  176. $this->_paper_orientation = "portrait"; 
  177. $this->_base_protocol = ""; 
  178. $this->_base_host = ""; 
  179. $this->_base_path = ""; 
  180. $this->_http_context = null; 
  181. $this->_callbacks = array(); 
  182. $this->_cache_id = null; 
  183.  
  184. $this->restore_locale(); 
  185.  
  186. /** 
  187. * Class destructor 
  188. */ 
  189. function __destruct() { 
  190. clear_object($this); 
  191.  
  192. /** 
  193. * Get the dompdf option value 
  194. * @param string $key 
  195. * @return mixed 
  196. * @throws DOMPDF_Exception 
  197. */ 
  198. function get_option($key) { 
  199. if ( !array_key_exists($key, $this->_options) ) { 
  200. throw new DOMPDF_Exception("Option '$key' doesn't exist"); 
  201.  
  202. return $this->_options[$key]; 
  203.  
  204. /** 
  205. * @param string $key 
  206. * @param mixed $value 
  207. * @throws DOMPDF_Exception 
  208. */ 
  209. function set_option($key, $value) { 
  210. if ( !array_key_exists($key, $this->_options) ) { 
  211. throw new DOMPDF_Exception("Option '$key' doesn't exist"); 
  212.  
  213. $this->_options[$key] = $value; 
  214.  
  215. /** 
  216. * @param array $options 
  217. */ 
  218. function set_options(array $options) { 
  219. foreach ($options as $key => $value) { 
  220. $this->set_option($key, $value); 
  221.  
  222. /** 
  223. * Save the system's locale configuration and 
  224. * set the right value for numeric formatting 
  225. */ 
  226. private function save_locale() { 
  227. if ( $this->_locale_standard ) { 
  228. return; 
  229.  
  230. $this->_system_locale = setlocale(LC_NUMERIC, "0"); 
  231. setlocale(LC_NUMERIC, "C"); 
  232.  
  233. /** 
  234. * Restore the system's locale configuration 
  235. */ 
  236. private function restore_locale() { 
  237. if ( $this->_locale_standard ) { 
  238. return; 
  239.  
  240. setlocale(LC_NUMERIC, $this->_system_locale); 
  241.  
  242. /** 
  243. * Returns the underlying {@link Frame_Tree} object 
  244. * @return Frame_Tree 
  245. */ 
  246. function get_tree() { 
  247. return $this->_tree; 
  248.  
  249. /** 
  250. * Sets the protocol to use 
  251. * FIXME validate these 
  252. * @param string $proto 
  253. */ 
  254. function set_protocol($proto) { 
  255. $this->_protocol = $proto; 
  256.  
  257. /** 
  258. * Sets the base hostname 
  259. * @param string $host 
  260. */ 
  261. function set_host($host) { 
  262. $this->_base_host = $host; 
  263.  
  264. /** 
  265. * Sets the base path 
  266. * @param string $path 
  267. */ 
  268. function set_base_path($path) { 
  269. $this->_base_path = $path; 
  270.  
  271. /** 
  272. * Sets the HTTP context 
  273. * @param resource $http_context 
  274. */ 
  275. function set_http_context($http_context) { 
  276. $this->_http_context = $http_context; 
  277.  
  278. /** 
  279. * Sets the default view 
  280. * @param string $default_view The default document view 
  281. * @param array $options The view's options 
  282. */ 
  283. function set_default_view($default_view, $options) { 
  284. $this->_default_view = $default_view; 
  285. $this->_default_view_options = $options; 
  286.  
  287. /** 
  288. * Returns the protocol in use 
  289. * @return string 
  290. */ 
  291. function get_protocol() { 
  292. return $this->_protocol; 
  293.  
  294. /** 
  295. * Returns the base hostname 
  296. * @return string 
  297. */ 
  298. function get_host() { 
  299. return $this->_base_host; 
  300.  
  301. /** 
  302. * Returns the base path 
  303. * @return string 
  304. */ 
  305. function get_base_path() { 
  306. return $this->_base_path; 
  307.  
  308. /** 
  309. * Returns the HTTP context 
  310. * @return resource 
  311. */ 
  312. function get_http_context() { 
  313. return $this->_http_context; 
  314.  
  315. /** 
  316. * Return the underlying Canvas instance (e.g. CPDF_Adapter, GD_Adapter) 
  317. * @return Canvas 
  318. */ 
  319. function get_canvas() { 
  320. return $this->_pdf; 
  321.  
  322. /** 
  323. * Returns the callbacks array 
  324. * @return array 
  325. */ 
  326. function get_callbacks() { 
  327. return $this->_callbacks; 
  328.  
  329. /** 
  330. * Returns the stylesheet 
  331. * @return Stylesheet 
  332. */ 
  333. function get_css() { 
  334. return $this->_css; 
  335.  
  336. /** 
  337. * @return DOMDocument 
  338. */ 
  339. function get_dom() { 
  340. return $this->_xml; 
  341.  
  342. /** 
  343. * Loads an HTML file 
  344. * Parse errors are stored in the global array _dompdf_warnings. 
  345. * @param string $file a filename or url to load 
  346. * @throws DOMPDF_Exception 
  347. */ 
  348. function load_html_file($file) { 
  349. $this->save_locale(); 
  350.  
  351. // Store parsing warnings as messages (this is to prevent output to the 
  352. // browser if the html is ugly and the dom extension complains,  
  353. // preventing the pdf from being streamed.) 
  354. if ( !$this->_protocol && !$this->_base_host && !$this->_base_path ) { 
  355. list($this->_protocol, $this->_base_host, $this->_base_path) = explode_url($file); 
  356.  
  357. if ( !$this->get_option("enable_remote") && ($this->_protocol != "" && $this->_protocol !== "file://" ) ) { 
  358. throw new DOMPDF_Exception("Remote file requested, but DOMPDF_ENABLE_REMOTE is false."); 
  359.  
  360. if ($this->_protocol == "" || $this->_protocol === "file://") { 
  361.  
  362. // Get the full path to $file, returns false if the file doesn't exist 
  363. $realfile = realpath($file); 
  364. if ( !$realfile ) { 
  365. throw new DOMPDF_Exception("File '$file' not found."); 
  366.  
  367. $chroot = $this->get_option("chroot"); 
  368. if ( strpos($realfile, $chroot) !== 0 ) { 
  369. throw new DOMPDF_Exception("Permission denied on $file. The file could not be found under the directory specified by DOMPDF_CHROOT."); 
  370.  
  371. // Exclude dot files (e.g. .htaccess) 
  372. if ( substr(basename($realfile), 0, 1) === "." ) { 
  373. throw new DOMPDF_Exception("Permission denied on $file."); 
  374.  
  375. $file = $realfile; 
  376.  
  377. $contents = file_get_contents($file, null, $this->_http_context); 
  378. $encoding = null; 
  379.  
  380. // See http://the-stickman.com/web-development/php/getting-http-response-headers-when-using-file_get_contents/ 
  381. if ( isset($http_response_header) ) { 
  382. foreach($http_response_header as $_header) { 
  383. if ( preg_match("@Content-Type:\s*[\w/]+;\s*?charset=([^\s]+)@i", $_header, $matches) ) { 
  384. $encoding = strtoupper($matches[1]); 
  385. break; 
  386.  
  387. $this->restore_locale(); 
  388.  
  389. $this->load_html($contents, $encoding); 
  390.  
  391. /** 
  392. * Loads an HTML string 
  393. * Parse errors are stored in the global array _dompdf_warnings. 
  394. * @todo use the $encoding variable 
  395. * @param string $str HTML text to load 
  396. * @param string $encoding Not used yet 
  397. */ 
  398. function load_html($str, $encoding = null) { 
  399. $this->save_locale(); 
  400.  
  401. // FIXME: Determine character encoding, switch to UTF8, update meta tag. Need better http/file stream encoding detection, currently relies on text or meta tag. 
  402. mb_detect_order('auto'); 
  403.  
  404. if (mb_detect_encoding($str) !== 'UTF-8') { 
  405. $metatags = array( 
  406. '@<meta\s+http-equiv="Content-Type"\s+content="(?:[\w/]+)(?:;\s*?charset=([^\s"]+))?@i',  
  407. '@<meta\s+content="(?:[\w/]+)(?:;\s*?charset=([^\s"]+))"?\s+http-equiv="Content-Type"@i',  
  408. '@<meta [^>]*charset\s*=\s*["\']?\s*([^"\' ]+)@i',  
  409. ); 
  410.  
  411. foreach($metatags as $metatag) { 
  412. if (preg_match($metatag, $str, $matches)) break; 
  413.  
  414. if (mb_detect_encoding($str) == '') { 
  415. if (isset($matches[1])) { 
  416. $encoding = strtoupper($matches[1]); 
  417. else { 
  418. $encoding = 'UTF-8'; 
  419. else { 
  420. if ( isset($matches[1]) ) { 
  421. $encoding = strtoupper($matches[1]); 
  422. else { 
  423. $encoding = 'auto'; 
  424.  
  425. if ( $encoding !== 'UTF-8' ) { 
  426. $str = mb_convert_encoding($str, 'UTF-8', $encoding); 
  427.  
  428. if ( isset($matches[1]) ) { 
  429. $str = preg_replace('/charset=([^\s"]+)/i', 'charset=UTF-8', $str); 
  430. else { 
  431. $str = str_replace('<head>', '<head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">', $str); 
  432. else { 
  433. $encoding = 'UTF-8'; 
  434.  
  435. // remove BOM mark from UTF-8, it's treated as document text by DOMDocument 
  436. // FIXME: roll this into the encoding detection using UTF-8/16/32 BOM (http://us2.php.net/manual/en/function.mb-detect-encoding.php#91051)? 
  437. if ( substr($str, 0, 3) == chr(0xEF).chr(0xBB).chr(0xBF) ) { 
  438. $str = substr($str, 3); 
  439.  
  440. // if the document contains non utf-8 with a utf-8 meta tag chars and was  
  441. // detected as utf-8 by mbstring, problems could happen. 
  442. // http://devzone.zend.com/article/8855 
  443. if ( $encoding !== 'UTF-8' ) { 
  444. $re = '/<meta ([^>]*)((?:charset=[^"\' ]+)([^>]*)|(?:charset=["\'][^"\' ]+["\']))([^>]*)>/i'; 
  445. $str = preg_replace($re, '<meta $1$3>', $str); 
  446.  
  447. // Store parsing warnings as messages 
  448. set_error_handler("record_warnings"); 
  449.  
  450. // @todo Take the quirksmode into account 
  451. // http://hsivonen.iki.fi/doctype/ 
  452. // https://developer.mozilla.org/en/mozilla's_quirks_mode 
  453. $quirksmode = false; 
  454.  
  455. if ( $this->get_option("enable_html5_parser") ) { 
  456. $tokenizer = new HTML5_Tokenizer($str); 
  457. $tokenizer->parse(); 
  458. $doc = $tokenizer->save(); 
  459.  
  460. // Remove #text children nodes in nodes that shouldn't have 
  461. $tag_names = array("html", "table", "tbody", "thead", "tfoot", "tr"); 
  462. foreach($tag_names as $tag_name) { 
  463. $nodes = $doc->getElementsByTagName($tag_name); 
  464.  
  465. foreach($nodes as $node) { 
  466. self::remove_text_nodes($node); 
  467.  
  468. $quirksmode = ($tokenizer->getTree()->getQuirksMode() > HTML5_TreeBuilder::NO_QUIRKS); 
  469. else { 
  470. // loadHTML assumes ISO-8859-1 unless otherwise specified, but there are 
  471. // bugs in how DOMDocument determines the actual encoding. Converting to 
  472. // HTML-ENTITIES prior to import appears to resolve the issue. 
  473. // http://devzone.zend.com/1538/php-dom-xml-extension-encoding-processing/ (see #4) 
  474. // http://stackoverflow.com/a/11310258/264628 
  475. $doc = new DOMDocument(); 
  476. $doc->preserveWhiteSpace = true; 
  477. $doc->loadHTML( mb_convert_encoding( $str , 'HTML-ENTITIES' , 'UTF-8' ) ); 
  478.  
  479. // If some text is before the doctype, we are in quirksmode 
  480. if ( preg_match("/^(.+)<!doctype/i", ltrim($str), $matches) ) { 
  481. $quirksmode = true; 
  482. // If no doctype is provided, we are in quirksmode 
  483. elseif ( !preg_match("/^<!doctype/i", ltrim($str), $matches) ) { 
  484. $quirksmode = true; 
  485. else { 
  486. // HTML5 <!DOCTYPE html> 
  487. if ( !$doc->doctype->publicId && !$doc->doctype->systemId ) { 
  488. $quirksmode = false; 
  489.  
  490. // not XHTML 
  491. if ( !preg_match("/xhtml/i", $doc->doctype->publicId) ) { 
  492. $quirksmode = true; 
  493.  
  494. $this->_xml = $doc; 
  495. $this->_quirksmode = $quirksmode; 
  496.  
  497. $this->_tree = new Frame_Tree($this->_xml); 
  498.  
  499. restore_error_handler(); 
  500.  
  501. $this->restore_locale(); 
  502.  
  503. static function remove_text_nodes(DOMNode $node) { 
  504. $children = array(); 
  505. for ($i = 0; $i < $node->childNodes->length; $i++) { 
  506. $child = $node->childNodes->item($i); 
  507. if ( $child->nodeName === "#text" ) { 
  508. $children[] = $child; 
  509.  
  510. foreach($children as $child) { 
  511. $node->removeChild($child); 
  512.  
  513. /** 
  514. * Builds the {@link Frame_Tree}, loads any CSS and applies the styles to 
  515. * the {@link Frame_Tree} 
  516. */ 
  517. protected function _process_html() { 
  518. $this->_tree->build_tree(); 
  519.  
  520. $this->_css->load_css_file(Stylesheet::DEFAULT_STYLESHEET, Stylesheet::ORIG_UA); 
  521.  
  522. $acceptedmedia = Stylesheet::$ACCEPTED_GENERIC_MEDIA_TYPES; 
  523. $acceptedmedia[] = $this->get_option("default_media_type"); 
  524.  
  525. // <base href="" /> 
  526. $base_nodes = $this->_xml->getElementsByTagName("base"); 
  527. if ( $base_nodes->length && ($href = $base_nodes->item(0)->getAttribute("href")) ) { 
  528. list($this->_protocol, $this->_base_host, $this->_base_path) = explode_url($href); 
  529.  
  530. // Set the base path of the Stylesheet to that of the file being processed 
  531. $this->_css->set_protocol($this->_protocol); 
  532. $this->_css->set_host($this->_base_host); 
  533. $this->_css->set_base_path($this->_base_path); 
  534.  
  535. // Get all the stylesheets so that they are processed in document order 
  536. $xpath = new DOMXPath($this->_xml); 
  537. $stylesheets = $xpath->query("//*[name() = 'link' or name() = 'style']"); 
  538.  
  539. foreach($stylesheets as $tag) { 
  540. switch (strtolower($tag->nodeName)) { 
  541. // load <link rel="STYLESHEET" ... /> tags 
  542. case "link": 
  543. if ( mb_strtolower(stripos($tag->getAttribute("rel"), "stylesheet") !== false) || // may be "appendix stylesheet" 
  544. mb_strtolower($tag->getAttribute("type")) === "text/css" ) { 
  545. //Check if the css file is for an accepted media type 
  546. //media not given then always valid 
  547. $formedialist = preg_split("/[\s\n, ]/", $tag->getAttribute("media"), -1, PREG_SPLIT_NO_EMPTY); 
  548. if ( count($formedialist) > 0 ) { 
  549. $accept = false; 
  550. foreach ( $formedialist as $type ) { 
  551. if ( in_array(mb_strtolower(trim($type)), $acceptedmedia) ) { 
  552. $accept = true; 
  553. break; 
  554.  
  555. if (!$accept) { 
  556. //found at least one mediatype, but none of the accepted ones 
  557. //Skip this css file. 
  558. continue; 
  559.  
  560. $url = $tag->getAttribute("href"); 
  561. $url = build_url($this->_protocol, $this->_base_host, $this->_base_path, $url); 
  562.  
  563. $this->_css->load_css_file($url, Stylesheet::ORIG_AUTHOR); 
  564. break; 
  565.  
  566. // load <style> tags 
  567. case "style": 
  568. // Accept all <style> tags by default (note this is contrary to W3C 
  569. // HTML 4.0 spec: 
  570. // http://www.w3.org/TR/REC-html40/present/styles.html#adef-media 
  571. // which states that the default media type is 'screen' 
  572. if ( $tag->hasAttributes() && 
  573. ($media = $tag->getAttribute("media")) && 
  574. !in_array($media, $acceptedmedia) ) { 
  575. continue; 
  576.  
  577. $css = ""; 
  578. if ( $tag->hasChildNodes() ) { 
  579. $child = $tag->firstChild; 
  580. while ( $child ) { 
  581. $css .= $child->nodeValue; // Handle <style><!-- blah --></style> 
  582. $child = $child->nextSibling; 
  583. else { 
  584. $css = $tag->nodeValue; 
  585.  
  586. $this->_css->load_css($css); 
  587. break; 
  588.  
  589. /** 
  590. * Sets the paper size & orientation 
  591. * @param string $size 'letter', 'legal', 'A4', etc. {@link CPDF_Adapter::$PAPER_SIZES} 
  592. * @param string $orientation 'portrait' or 'landscape' 
  593. */ 
  594. function set_paper($size, $orientation = "portrait") { 
  595. $this->_paper_size = $size; 
  596. $this->_paper_orientation = $orientation; 
  597.  
  598. /** 
  599. * Enable experimental caching capability 
  600. * @access private 
  601. */ 
  602. function enable_caching($cache_id) { 
  603. $this->_cache_id = $cache_id; 
  604.  
  605. /** 
  606. * Sets callbacks for events like rendering of pages and elements. 
  607. * The callbacks array contains arrays with 'event' set to 'begin_page',  
  608. * 'end_page', 'begin_frame', or 'end_frame' and 'f' set to a function or 
  609. * object plus method to be called. 
  610. * The function 'f' must take an array as argument, which contains info 
  611. * about the event. 
  612. * @param array $callbacks the set of callbacks to set 
  613. */ 
  614. function set_callbacks($callbacks) { 
  615. if (is_array($callbacks)) { 
  616. $this->_callbacks = array(); 
  617. foreach ($callbacks as $c) { 
  618. if (is_array($c) && isset($c['event']) && isset($c['f'])) { 
  619. $event = $c['event']; 
  620. $f = $c['f']; 
  621. if (is_callable($f) && is_string($event)) { 
  622. $this->_callbacks[$event][] = $f; 
  623.  
  624. /** 
  625. * Get the quirks mode 
  626. * @return boolean true if quirks mode is active 
  627. */ 
  628. function get_quirksmode() { 
  629. return $this->_quirksmode; 
  630.  
  631. function parse_default_view($value) { 
  632. $valid = array("XYZ", "Fit", "FitH", "FitV", "FitR", "FitB", "FitBH", "FitBV"); 
  633.  
  634. $options = preg_split("/\s*, \s*/", trim($value)); 
  635. $default_view = array_shift($options); 
  636.  
  637. if ( !in_array($default_view, $valid) ) { 
  638. return false; 
  639.  
  640. $this->set_default_view($default_view, $options); 
  641. return true; 
  642.  
  643. /** 
  644. * Renders the HTML to PDF 
  645. */ 
  646. function render() { 
  647. $this->save_locale(); 
  648.  
  649. $log_output_file = $this->get_option("log_output_file"); 
  650. if ( $log_output_file ) { 
  651. if ( !file_exists($log_output_file) && is_writable(dirname($log_output_file)) ) { 
  652. touch($log_output_file); 
  653.  
  654. $this->_start_time = microtime(true); 
  655. ob_start(); 
  656.  
  657. //enable_mem_profile(); 
  658.  
  659. $this->_process_html(); 
  660.  
  661. $this->_css->apply_styles($this->_tree); 
  662.  
  663. // @page style rules : size, margins 
  664. $page_styles = $this->_css->get_page_styles(); 
  665.  
  666. $base_page_style = $page_styles["base"]; 
  667. unset($page_styles["base"]); 
  668.  
  669. foreach($page_styles as $_page_style) { 
  670. $_page_style->inherit($base_page_style); 
  671.  
  672. if ( is_array($base_page_style->size) ) { 
  673. $this->set_paper(array(0, 0, $base_page_style->size[0], $base_page_style->size[1])); 
  674.  
  675. $this->_pdf = Canvas_Factory::get_instance($this, $this->_paper_size, $this->_paper_orientation); 
  676. Font_Metrics::init($this->_pdf); 
  677.  
  678. if ( $this->get_option("enable_font_subsetting") && $this->_pdf instanceof CPDF_Adapter ) { 
  679. foreach ($this->_tree->get_frames() as $frame) { 
  680. $style = $frame->get_style(); 
  681. $node = $frame->get_node(); 
  682.  
  683. // Handle text nodes 
  684. if ( $node->nodeName === "#text" ) { 
  685. $this->_pdf->register_string_subset($style->font_family, $node->nodeValue); 
  686. continue; 
  687.  
  688. // Handle generated content (list items) 
  689. if ( $style->display === "list-item" ) { 
  690. $chars = List_Bullet_Renderer::get_counter_chars($style->list_style_type); 
  691. $this->_pdf->register_string_subset($style->font_family, $chars); 
  692. continue; 
  693.  
  694. // Handle other generated content (pseudo elements) 
  695. // FIXME: This only captures the text of the stylesheet declaration,  
  696. // not the actual generated content, and forces all possible counter 
  697. // values. See notes in issue #750. 
  698. if ( $frame->get_node()->nodeName == "dompdf_generated" ) { 
  699. // all possible counter values 
  700. $chars = List_Bullet_Renderer::get_counter_chars('decimal'); 
  701. $this->_pdf->register_string_subset($style->font_family, $chars); 
  702. $chars = List_Bullet_Renderer::get_counter_chars('upper-alpha'); 
  703. $this->_pdf->register_string_subset($style->font_family, $chars); 
  704. $chars = List_Bullet_Renderer::get_counter_chars('lower-alpha'); 
  705. $this->_pdf->register_string_subset($style->font_family, $chars); 
  706. $chars = List_Bullet_Renderer::get_counter_chars('lower-greek'); 
  707. $this->_pdf->register_string_subset($style->font_family, $chars); 
  708. // the text of the stylesheet declaration 
  709. $this->_pdf->register_string_subset($style->font_family, $style->content); 
  710. continue; 
  711.  
  712. $root = null; 
  713.  
  714. foreach ($this->_tree->get_frames() as $frame) { 
  715. // Set up the root frame 
  716. if ( is_null($root) ) { 
  717. $root = Frame_Factory::decorate_root( $this->_tree->get_root(), $this ); 
  718. continue; 
  719.  
  720. // Create the appropriate decorators, reflowers & positioners. 
  721. Frame_Factory::decorate_frame($frame, $this, $root); 
  722.  
  723. // Add meta information 
  724. $title = $this->_xml->getElementsByTagName("title"); 
  725. if ( $title->length ) { 
  726. $this->_pdf->add_info("Title", trim($title->item(0)->nodeValue)); 
  727.  
  728. $metas = $this->_xml->getElementsByTagName("meta"); 
  729. $labels = array( 
  730. "author" => "Author",  
  731. "keywords" => "Keywords",  
  732. "description" => "Subject",  
  733. ); 
  734. foreach($metas as $meta) { 
  735. $name = mb_strtolower($meta->getAttribute("name")); 
  736. $value = trim($meta->getAttribute("content")); 
  737.  
  738. if ( isset($labels[$name]) ) { 
  739. $this->_pdf->add_info($labels[$name], $value); 
  740. continue; 
  741.  
  742. if ( $name === "dompdf.view" && $this->parse_default_view($value) ) { 
  743. $this->_pdf->set_default_view($this->_default_view, $this->_default_view_options); 
  744.  
  745. $root->set_containing_block(0, 0, $this->_pdf->get_width(), $this->_pdf->get_height()); 
  746. $root->set_renderer(new Renderer($this)); 
  747.  
  748. // This is where the magic happens: 
  749. $root->reflow(); 
  750.  
  751. // Clean up cached images 
  752. Image_Cache::clear(); 
  753.  
  754. global $_dompdf_warnings, $_dompdf_show_warnings; 
  755. if ( $_dompdf_show_warnings ) { 
  756. echo '<b>DOMPDF Warnings</b><br><pre>'; 
  757. foreach ($_dompdf_warnings as $msg) { 
  758. echo $msg . "\n"; 
  759. echo $this->get_canvas()->get_cpdf()->messages; 
  760. echo '</pre>'; 
  761. flush(); 
  762.  
  763. $this->restore_locale(); 
  764.  
  765. /** 
  766. * Add meta information to the PDF after rendering 
  767. */ 
  768. function add_info($label, $value) { 
  769. if ( !is_null($this->_pdf) ) { 
  770. $this->_pdf->add_info($label, $value); 
  771.  
  772. /** 
  773. * Writes the output buffer in the log file 
  774. * @return void 
  775. */ 
  776. private function write_log() { 
  777. $log_output_file = $this->get_option("log_output_file"); 
  778. if ( !$log_output_file || !is_writable($log_output_file) ) { 
  779. return; 
  780.  
  781. $frames = Frame::$ID_COUNTER; 
  782. $memory = DOMPDF_memory_usage() / 1024; 
  783. $time = (microtime(true) - $this->_start_time) * 1000; 
  784.  
  785. $out = sprintf( 
  786. "<span style='color: #000' title='Frames'>%6d</span>". 
  787. "<span style='color: #009' title='Memory'>%10.2f KB</span>". 
  788. "<span style='color: #900' title='Time'>%10.2f ms</span>". 
  789. "<span title='Quirksmode'> ". 
  790. ($this->_quirksmode ? "<span style='color: #d00'> ON</span>" : "<span style='color: #0d0'>OFF</span>"). 
  791. "</span><br />", $frames, $memory, $time); 
  792.  
  793. $out .= ob_get_clean(); 
  794.  
  795. $log_output_file = $this->get_option("log_output_file"); 
  796. file_put_contents($log_output_file, $out); 
  797.  
  798. /** 
  799. * Streams the PDF to the client 
  800. * The file will open a download dialog by default. The options 
  801. * parameter controls the output. Accepted options are: 
  802. * 'Accept-Ranges' => 1 or 0 - if this is not set to 1, then this 
  803. * header is not included, off by default this header seems to 
  804. * have caused some problems despite the fact that it is supposed 
  805. * to solve them, so I am leaving it off by default. 
  806. * 'compress' = > 1 or 0 - apply content stream compression, this is 
  807. * on (1) by default 
  808. * 'Attachment' => 1 or 0 - if 1, force the browser to open a 
  809. * download dialog, on (1) by default 
  810. * @param string $filename the name of the streamed file 
  811. * @param array $options header options (see above) 
  812. */ 
  813. function stream($filename, $options = null) { 
  814. $this->save_locale(); 
  815.  
  816. $this->write_log(); 
  817.  
  818. if ( !is_null($this->_pdf) ) { 
  819. $this->_pdf->stream($filename, $options); 
  820.  
  821. $this->restore_locale(); 
  822.  
  823. /** 
  824. * Returns the PDF as a string 
  825. * The file will open a download dialog by default. The options 
  826. * parameter controls the output. Accepted options are: 
  827. * 'compress' = > 1 or 0 - apply content stream compression, this is 
  828. * on (1) by default 
  829. * @param array $options options (see above) 
  830. * @return string 
  831. */ 
  832. function output($options = null) { 
  833. $this->save_locale(); 
  834.  
  835. $this->write_log(); 
  836.  
  837. if ( is_null($this->_pdf) ) { 
  838. return null; 
  839.  
  840. $output = $this->_pdf->output( $options ); 
  841.  
  842. $this->restore_locale(); 
  843.  
  844. return $output; 
  845.  
  846. /** 
  847. * Returns the underlying HTML document as a string 
  848. * @return string 
  849. */ 
  850. function output_html() { 
  851. return $this->_xml->saveHTML();