source: trunk/filemanager/tp/dompdf/include/dompdf.cls.php @ 6057

Revision 6057, 18.5 KB checked in by marcosw, 12 years ago (diff)

Ticket #2398 - Compatibilizacao com PHP-5.3 em alguns módulos do expresso

Line 
1<?php
2/**
3 * DOMPDF - PHP5 HTML to PDF renderer
4 *
5 * File: $RCSfile: dompdf.cls.php,v $
6 * Created on: 2004-06-09
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
40/* $Id: dompdf.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */
41
42/**
43 * DOMPDF - PHP5 HTML to PDF renderer
44 *
45 * DOMPDF loads HTML and does its best to render it as a PDF.  It gets its
46 * name from the new DomDocument PHP5 extension.  Source HTML is first
47 * parsed by a DomDocument object.  DOMPDF takes the resulting DOM tree and
48 * attaches a {@link Frame} object to each node.  {@link Frame} objects store
49 * positioning and layout information and each has a reference to a {@link
50 * Style} object.
51 *
52 * Style information is loaded and parsed (see {@link Stylesheet}) and is
53 * applied to the frames in the tree by using XPath.  CSS selectors are
54 * converted into XPath queries, and the computed {@link Style} objects are
55 * applied to the {@link Frame}s.
56 *
57 * {@link Frame}s are then decorated (in the design pattern sense of the
58 * word) based on their CSS display property ({@link
59 * http://www.w3.org/TR/CSS21/visuren.html#propdef-display}).
60 * Frame_Decorators augment the basic {@link Frame} class by adding
61 * additional properties and methods specific to the particular type of
62 * {@link Frame}.  For example, in the CSS layout model, block frames
63 * (display: block;) contain line boxes that are usually filled with text or
64 * other inline frames.  The Block_Frame_Decorator therefore adds a $lines
65 * property as well as methods to add {@link Frame}s to lines and to add
66 * additional lines.  {@link Frame}s also are attached to specific
67 * Positioner and {@link Frame_Reflower} objects that contain the
68 * positioining and layout algorithm for a specific type of frame,
69 * respectively.  This is an application of the Strategy pattern.
70 *
71 * Layout, or reflow, proceeds recursively (post-order) starting at the root
72 * of the document.  Space constraints (containing block width & height) are
73 * pushed down, and resolved positions and sizes bubble up.  Thus, every
74 * {@link Frame} in the document tree is traversed once (except for tables
75 * which use a two-pass layout algorithm).  If you are interested in the
76 * details, see the reflow() method of the Reflower classes.
77 *
78 * Rendering is relatively straightforward once layout is complete. {@link
79 * Frame}s are rendered using an adapted {@link Cpdf} class, originally
80 * written by Wayne Munro, http://www.ros.co.nz/pdf/.  (Some performance
81 * related changes have been made to the original {@link Cpdf} class, and
82 * the {@link CPDF_Adapter} class provides a simple, stateless interface to
83 * PDF generation.)  PDFLib support has now also been added, via the {@link
84 * PDFLib_Adapter}.
85 *
86 *
87 * @package dompdf
88 */
89class DOMPDF {
90
91
92  /**
93   * DomDocument representing the HTML document
94   *
95   * @var DomDocument
96   */
97  protected $_xml;
98
99  /**
100   * Frame_Tree derived from the DOM tree
101   *
102   * @var Frame_Tree
103   */
104  protected $_tree;
105
106  /**
107   * Stylesheet for the document
108   *
109   * @var Stylesheet
110   */
111  protected $_css;
112
113  /**
114   * Actual PDF renderer
115   *
116   * @var Canvas
117   */
118  protected $_pdf;
119
120  /**
121   * Desired paper size ('letter', 'legal', 'A4', etc.)
122   *
123   * @var string
124   */
125  protected $_paper_size;
126
127  /**
128   * Paper orientation ('portrait' or 'landscape')
129   *
130   * @var string
131   */
132  protected $_paper_orientation;
133
134  /**
135   * Callbacks on new page and new element
136   *
137   * @var array
138   */
139  protected $_callbacks;
140
141  /**
142   * Experimental caching capability
143   *
144   * @var string
145   */
146  private $_cache_id;
147
148  /**
149   * Base hostname
150   *
151   * Used for relative paths/urls
152   * @var string
153   */
154  protected $_base_host;
155
156  /**
157   * Absolute base path
158   *
159   * Used for relative paths/urls
160   * @var string
161   */
162  protected $_base_path;
163
164  /**
165   * Protcol used to request file (file://, http://, etc)
166   *
167   * @var string
168   */
169  protected $_protocol;
170
171
172  /**
173   * Class constructor
174   */
175  function __construct() {
176    $this->_messages = array();
177    $this->_xml = new DomDocument();
178    $this->_xml->preserveWhiteSpace = true;
179    $this->_tree = new Frame_Tree($this->_xml);
180    $this->_css = new Stylesheet();
181    $this->_pdf = null;
182    $this->_paper_size = "letter";
183    $this->_paper_orientation = "portrait";
184    $this->_base_protocol = "";
185    $this->_base_host = "";
186    $this->_base_path = "";
187    $this->_callbacks = array();
188    $this->_cache_id = null;
189  }
190
191  /**
192   * Returns the underlying {@link Frame_Tree} object
193   *
194   * @return Frame_Tree
195   */
196  function get_tree() { return $this->_tree; }
197
198  //........................................................................
199
200  /**
201   * Sets the protocol to use
202   *
203   * @param string $proto
204   */
205  // FIXME: validate these
206  function set_protocol($proto) { $this->_protocol = $proto; }
207
208  /**
209   * Sets the base hostname
210   *
211   * @param string $host
212   */
213  function set_host($host) { $this->_base_host = $host; }
214
215  /**
216   * Sets the base path
217   *
218   * @param string $path
219   */
220  function set_base_path($path) { $this->_base_path = $path; }
221
222  /**
223   * Returns the protocol in use
224   *
225   * @return string
226   */
227  function get_protocol() { return $this->_protocol; }
228
229  /**
230   * Returns the base hostname
231   *
232   * @return string
233   */
234  function get_host() { return $this->_base_host; }
235
236  /**
237   * Returns the base path
238   *
239   * @return string
240   */
241  function get_base_path() { return $this->_base_path; }
242
243  /**
244   * Return the underlying Canvas instance (e.g. CPDF_Adapter, GD_Adapter)
245   *
246   * @return Canvas
247   */
248  function get_canvas() { return $this->_pdf; }
249
250  /**
251   * Returns the callbacks array
252   *
253   * @return array
254   */
255  function get_callbacks() { return $this->_callbacks; }
256 
257  //........................................................................
258
259  /**
260   * Loads an HTML file
261   *
262   * Parse errors are stored in the global array _dompdf_warnings.
263   *
264   * @param string $file a filename or url to load
265   */
266  function load_html_file($file) {
267    // Store parsing warnings as messages (this is to prevent output to the
268    // browser if the html is ugly and the dom extension complains,
269    // preventing the pdf from being streamed.)
270    if ( !$this->_protocol && !$this->_base_host && !$this->_base_path )
271      list($this->_protocol, $this->_base_host, $this->_base_path) = explode_url($file);
272
273    if ( !DOMPDF_ENABLE_REMOTE &&
274         ($this->_protocol != "" && $this->_protocol != "file://" ) )
275      throw new DOMPDF_Exception("Remote file requested, but DOMPDF_ENABLE_REMOTE is false.");
276
277    if ($this->_protocol == "" || $this->_protocol == "file://") {
278
279      $realfile = dompdf_realpath($file);
280      if ( !$file )
281        throw new DOMPDF_Exception("File '$file' not found.");
282
283      if ( strpos($realfile, DOMPDF_CHROOT) !== 0 )
284        throw new DOMPDF_Exception("Permission denied on $file.");
285
286      // Exclude dot files (e.g. .htaccess)
287      if ( substr(basename($realfile),0,1) == "." )
288        throw new DOMPDF_Exception("Permission denied on $file.");
289
290      $file = $realfile;
291    }
292
293    $this->load_html(file_get_contents($file));
294  }
295
296  /**
297   * Loads an HTML string
298   *
299   * Parse errors are stored in the global array _dompdf_warnings.
300   *
301   * @param string $str HTML text to load
302   */
303  function load_html($str) {
304    // FIXME: Determine character encoding, switch to UTF8, update meta tag. Need better http/file stream encoding detection, currently relies on text or meta tag.
305    mb_detect_order('auto');
306    if (mb_detect_encoding($str) != 'UTF-8') {
307      if (mb_detect_encoding($str) == '') {
308        if (preg_match('@<meta\s+http-equiv="Content-Type"\s+content="([\w/]+)(;\s+charset=([^\s"]+))?@i',$str,$matches)) {
309          $encoding = strtoupper($matches[3]);
310        } else {
311          $encoding = 'UTF-8';
312        }
313      } else {
314        if (preg_match('@<meta\s+http-equiv="Content-Type"\s+content="([\w/]+)(;\s+charset=([^\s"]+))?@i',$str,$matches)) {
315          $encoding = strtoupper($matches[3]);
316        } else {
317          $encoding = 'auto';
318        }
319      }
320      if ($encoding != 'UTF-8') { $str = mb_convert_encoding($str, 'UTF-8', $encoding); }
321      if (preg_match('@<meta\s+http-equiv="Content-Type"\s+content="([\w/]+)(;\s+charset=([^\s"]+))?@i',$str,$matches)) {
322        $str = preg_replace('/charset=([^\s"]+)/i','charset=UTF-8',$str);
323      } else {
324        $str = str_replace('<head>', '<head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">', $str);
325      }
326    }
327
328    // Parse embedded php, first-pass
329    if ( DOMPDF_ENABLE_PHP ) {
330      ob_start();
331      eval("?" . ">$str");
332      $str = ob_get_contents();
333      ob_end_clean();
334    }
335
336    // Store parsing warnings as messages
337    set_error_handler("record_warnings");
338    $this->_xml->loadHTML($str);
339    restore_error_handler();
340  }
341
342  /**
343   * Builds the {@link Frame_Tree}, loads any CSS and applies the styles to
344   * the {@link Frame_Tree}
345   */
346  protected function _process_html() {
347    $this->_tree->build_tree();
348
349    $this->_css->load_css_file(Stylesheet::DEFAULT_STYLESHEET);
350
351    $acceptedmedia = Stylesheet::$ACCEPTED_GENERIC_MEDIA_TYPES;
352    if ( defined("DOMPDF_DEFAULT_MEDIA_TYPE") ) {
353      $acceptedmedia[] = DOMPDF_DEFAULT_MEDIA_TYPE;
354    } else {
355      $acceptedmedia[] = Stylesheet::$ACCEPTED_DEFAULT_MEDIA_TYPE;
356    }
357         
358    // load <link rel="STYLESHEET" ... /> tags
359    $links = $this->_xml->getElementsByTagName("link");
360    foreach ($links as $link) {
361      if ( mb_strtolower($link->getAttribute("rel")) == "stylesheet" ||
362           mb_strtolower($link->getAttribute("type")) == "text/css" ) {
363        //Check if the css file is for an accepted media type
364        //media not given then always valid
365        $formedialist = preg_split('/[\s\n,]/', $link->getAttribute("media"),-1, PREG_SPLIT_NO_EMPTY);
366        if ( count($formedialist) > 0 ) {
367          $accept = false;
368          foreach ( $formedialist as $type ) {
369            if ( in_array(mb_strtolower(trim($type)), $acceptedmedia) ) {
370              $accept = true;
371              break;
372            }
373          }
374          if (!$accept) {
375            //found at least one mediatype, but none of the accepted ones
376            //Skip this css file.
377            continue;
378          }
379        }
380           
381        $url = $link->getAttribute("href");
382        $url = build_url($this->_protocol, $this->_base_host, $this->_base_path, $url);
383
384        $this->_css->load_css_file($url);
385      }
386
387    }
388
389    // load <style> tags
390    $styles = $this->_xml->getElementsByTagName("style");
391    foreach ($styles as $style) {
392
393      // Accept all <style> tags by default (note this is contrary to W3C
394      // HTML 4.0 spec:
395      // http://www.w3.org/TR/REC-html40/present/styles.html#adef-media
396      // which states that the default media type is 'screen'
397      if ( $style->hasAttributes() &&
398           ($media = $style->getAttribute("media")) &&
399           !in_array($media, $acceptedmedia) )
400        continue;
401
402      $css = "";
403      if ( $style->hasChildNodes() ) {
404
405        $child = $style->firstChild;
406        while ( $child ) {
407          $css .= $child->nodeValue; // Handle <style><!-- blah --></style>
408          $child = $child->nextSibling;
409        }
410
411      } else
412        $css = $style->nodeValue;
413     
414      // Set the base path of the Stylesheet to that of the file being processed
415      $this->_css->set_protocol($this->_protocol);
416      $this->_css->set_host($this->_base_host);
417      $this->_css->set_base_path($this->_base_path);
418
419      $this->_css->load_css($css);
420    }
421
422  }
423
424  //........................................................................
425
426  /**
427   * Sets the paper size & orientation
428   *
429   * @param string $size 'letter', 'legal', 'A4', etc. {@link CPDF_Adapter::$PAPER_SIZES}
430   * @param string $orientation 'portrait' or 'landscape'
431   */
432  function set_paper($size, $orientation = "portrait") {
433    $this->_paper_size = $size;
434    $this->_paper_orientation = $orientation;
435  }
436
437  //........................................................................
438
439  /**
440   * Enable experimental caching capability
441   * @access private
442   */
443  function enable_caching($cache_id) {
444    $this->_cache_id = $cache_id;
445  }
446
447  //........................................................................
448
449  /**
450   * Sets callbacks for events like rendering of pages and elements.
451   * The callbacks array contains arrays with 'event' set to 'begin_page',
452   * 'end_page', 'begin_frame', or 'end_frame' and 'f' set to a function or
453   * object plus method to be called.
454   *
455   * The function 'f' must take an array as argument, which contains info
456   * about the event.
457   *
458   * @param array $callbacks the set of callbacks to set
459   */
460  function set_callbacks($callbacks) {
461    if (is_array($callbacks)) {
462      $this->_callbacks = array();
463      foreach ($callbacks as $c) {
464        if (is_array($c) && isset($c['event']) && isset($c['f'])) {
465          $event = $c['event'];
466          $f = $c['f'];
467          if (is_callable($f) && is_string($event)) {
468            $this->_callbacks[$event][] = $f;
469          }
470        }
471      }
472    }
473  }
474 
475  //........................................................................
476
477  /**
478   * Renders the HTML to PDF
479   */
480  function render() {
481
482    //enable_mem_profile();
483
484    $this->_process_html();
485
486    $this->_css->apply_styles($this->_tree);
487
488    $root = null;
489
490    foreach ($this->_tree->get_frames() as $frame) {
491      // Set up the root frame
492
493      if ( is_null($root) ) {
494        $root = Frame_Factory::decorate_root( $this->_tree->get_root(), $this );
495        continue;
496      }
497
498      // Create the appropriate decorators, reflowers & positioners.
499      $deco = Frame_Factory::decorate_frame($frame, $this);
500      $deco->set_root($root);
501
502      // FIXME: handle generated content
503      if ( $frame->get_style()->display == "list-item" ) {
504
505        // Insert a list-bullet frame
506        $node = $this->_xml->createElement("bullet"); // arbitrary choice
507        $b_f = new Frame($node);
508
509        $style = $this->_css->create_style();
510        $style->display = "-dompdf-list-bullet";
511        $style->inherit($frame->get_style());
512        $b_f->set_style($style);
513
514        $deco->prepend_child( Frame_Factory::decorate_frame($b_f, $this) );
515      }
516
517    }
518
519    $this->_pdf = Canvas_Factory::get_instance($this->_paper_size, $this->_paper_orientation);
520
521    $root->set_containing_block(0, 0, $this->_pdf->get_width(), $this->_pdf->get_height());
522    $root->set_renderer(new Renderer($this));
523
524    // This is where the magic happens:
525    $root->reflow();
526
527    // Clean up cached images
528    Image_Cache::clear();
529   
530    if ( $GLOBALS['_dompdf_show_warnings'] ) {
531      global $_dompdf_warnings;
532        echo '<b>DOMPDF Warnings</b><br><pre>';
533      foreach ($_dompdf_warnings as $msg)
534        echo $msg . "\n";
535      echo $this->get_canvas()->get_cpdf()->messages;
536        echo '</pre>';
537      flush();
538    }
539  }
540
541  //........................................................................
542
543  /**
544   * Add meta information to the PDF after rendering
545   */
546  function add_info($label, $value) {
547    if (!is_null($this->_pdf))
548      $this->_pdf->add_info($label, $value);
549  }
550 
551  //........................................................................
552
553  /**
554   * Streams the PDF to the client
555   *
556   * The file will open a download dialog by default.  The options
557   * parameter controls the output.  Accepted options are:
558   *
559   * 'Accept-Ranges' => 1 or 0 - if this is not set to 1, then this
560   *    header is not included, off by default this header seems to
561   *    have caused some problems despite the fact that it is supposed
562   *    to solve them, so I am leaving it off by default.
563   *
564   * 'compress' = > 1 or 0 - apply content stream compression, this is
565   *    on (1) by default
566   *
567   * 'Attachment' => 1 or 0 - if 1, force the browser to open a
568   *    download dialog, on (1) by default
569   *
570   * @param string $filename the name of the streamed file
571   * @param array  $options header options (see above)
572   */
573  function stream($filename, $options = null) {
574    if (!is_null($this->_pdf))
575      $this->_pdf->stream($filename, $options);
576  }
577
578  /**
579   * Returns the PDF as a string
580   *
581   * The file will open a download dialog by default.  The options
582   * parameter controls the output.  Accepted options are:
583   *
584   *
585   * 'compress' = > 1 or 0 - apply content stream compression, this is
586   *    on (1) by default
587   *
588   *
589   * @param array  $options options (see above)
590   * @return string
591   */
592  function output($options = null) {
593
594    if ( is_null($this->_pdf) )
595      return null;
596
597    return $this->_pdf->output( $options );
598  }
599
600
601  /**
602   * Returns the underlying HTML document as a string
603   *
604   * @return string
605   */
606  function output_html() {
607    return $this->_xml->saveHTML();
608  }
609  //........................................................................
610
611}
612?>
Note: See TracBrowser for help on using the repository browser.