dompdf.cls.php 29 KB

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