source: sandbox/filemanager/tp/dompdf/include/dompdf.cls.php @ 1575

Revision 1575, 13.4 KB checked in by amuller, 14 years ago (diff)

Ticket #597 - Implentação, melhorias do modulo gerenciador de arquivos

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,v 1.18 2006/07/07 21:31:03 benjcarson Exp $ */
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  private $_cache_id;
135
136  /**
137   * Base hostname
138   *
139   * Used for relative paths/urls
140   * @var string
141   */
142  protected $_base_host;
143
144  /**
145   * Absolute base path
146   *
147   * Used for relative paths/urls
148   * @var string
149   */
150  protected $_base_path;
151
152  /**
153   * Protcol used to request file (file://, http://, etc)
154   *
155   * @var string
156   */
157  protected $_protocol;
158 
159
160  /**
161   * Class constructor
162   */
163  function __construct() {
164    $this->_messages = array();
165    $this->_xml = new DomDocument();
166    $this->_xml->preserveWhiteSpace = true;
167    $this->_tree = new Frame_Tree($this->_xml);
168    $this->_css = new Stylesheet();
169    $this->_pdf = null;
170    $this->_paper_size = "letter";
171    $this->_paper_orientation = "portrait";
172    $this->_base_host = "";
173    $this->_base_path = "";
174    $this->_cache_id = null;
175  }
176
177  /**
178   * Returns the underlying {@link Frame_Tree} object
179   *
180   * @return Frame_Tree
181   */
182  function get_tree() { return $this->_tree; }
183
184  //........................................................................
185
186  /**
187   * Sets the protocol to use
188   *
189   * @param string $proto
190   */
191  // FIXME: validate these
192  function set_protocol($proto) { $this->_protocol = $proto; }
193
194  /**
195   * Sets the base hostname
196   *
197   * @param string $host
198   */
199  function set_host($host) { $this->_base_host = $host; }
200
201  /**
202   * Sets the base path
203   *
204   * @param string $path
205   */
206  function set_base_path($path) { $this->_base_path = $path; }
207
208  /**
209   * Returns the protocol in use
210   *
211   * @return string
212   */
213  function get_protocol() { return $this->_protocol; }
214
215  /**
216   * Returns the base hostname
217   *
218   * @return string
219   */
220  function get_host() { return $this->_base_host; }
221
222  /**
223   * Returns the base path
224   *
225   * @return string
226   */
227  function get_base_path() { return $this->_base_path; }
228 
229  /**
230   * Return the underlying Canvas instance (e.g. CPDF_Adapter, GD_Adapter)
231   *
232   * @return Canvas
233   */
234  function get_canvas() { return $this->_pdf; }
235 
236  //........................................................................
237
238  /**
239   * Loads an HTML file
240   *
241   * Parse errors are stored in the global array _dompdf_warnings.
242   *
243   * @param string $file a filename or url to load
244   */
245  function load_html_file($file) {
246    // Store parsing warnings as messages (this is to prevent output to the
247    // browser if the html is ugly and the dom extension complains,
248    // preventing the pdf from being streamed.)
249    list($this->_protocol, $this->_base_host, $this->_base_path) = explode_url($file);
250   
251    if ( !DOMPDF_ENABLE_REMOTE &&
252         ($this->_protocol != "" && $this->_protocol != "file://" ) )
253      throw new DOMPDF_Exception("Remote file requested, but DOMPDF_ENABLE_REMOTE is false.");
254         
255    if ( !DOMPDF_ENABLE_PHP ) {
256      set_error_handler("record_warnings");
257      $this->_xml->loadHTMLFile($file);
258      restore_error_handler();
259
260    } else
261      $this->load_html(file_get_contents($file));
262
263  }
264
265  /**
266   * Loads an HTML string
267   *
268   * Parse errors are stored in the global array _dompdf_warnings.
269   *
270   * @param string $str HTML text to load
271   */
272  function load_html($str) {
273
274    // Parse embedded php, first-pass
275    if ( DOMPDF_ENABLE_PHP ) {
276      ob_start();
277      eval("?" . ">$str");
278      $str = ob_get_contents();     
279      ob_end_clean();
280    }
281
282    // Store parsing warnings as messages
283    set_error_handler("record_warnings");
284    $this->_xml->loadHTML($str);
285    restore_error_handler();
286  }
287
288  /**
289   * Builds the {@link Frame_Tree}, loads any CSS and applies the styles to
290   * the {@link Frame_Tree}
291   */
292  protected function _process_html() {
293    $this->_tree->build_tree();
294   
295    $this->_css->load_css_file(Stylesheet::DEFAULT_STYLESHEET);   
296
297    // load <link rel="STYLESHEET" ... /> tags
298    $links = $this->_xml->getElementsByTagName("link");   
299    foreach ($links as $link) {
300      if ( mb_strtolower($link->getAttribute("rel")) == "stylesheet" ||
301           mb_strtolower($link->getAttribute("type")) == "text/css" ) {
302        $url = $link->getAttribute("href");
303        $url = build_url($this->_protocol, $this->_base_host, $this->_base_path, $url);
304       
305        $this->_css->load_css_file($url);
306      }
307
308    }
309
310    // load <style> tags
311    $styles = $this->_xml->getElementsByTagName("style");
312    foreach ($styles as $style) {
313
314      // Accept all <style> tags by default (note this is contrary to W3C
315      // HTML 4.0 spec:
316      // http://www.w3.org/TR/REC-html40/present/styles.html#adef-media
317      // which states that the default media type is 'screen'
318      if ( $style->hasAttributes() &&
319           ($media = $style->getAttribute("media")) &&
320           !in_array($media, Stylesheet::$ACCEPTED_MEDIA_TYPES) )
321        continue;
322     
323      $css = "";
324      if ( $style->hasChildNodes() ) {
325       
326        $child = $style->firstChild;
327        while ( $child ) {
328          $css .= $child->nodeValue; // Handle <style><!-- blah --></style>
329          $child = $child->nextSibling;
330        }
331       
332      } else
333        $css = $style->nodeValue;
334
335      // Set the base path of the Stylesheet to that of the file being processed
336      $this->_css->set_protocol($this->_protocol);
337      $this->_css->set_host($this->_base_host);
338      $this->_css->set_base_path($this->_base_path);
339
340      $this->_css->load_css($css);
341    }
342   
343  }
344
345  //........................................................................
346
347  /**
348   * Sets the paper size & orientation
349   *
350   * @param string $size 'letter', 'legal', 'A4', etc. {@link CPDF_Adapter::$PAPER_SIZES}
351   * @param string $orientation 'portrait' or 'landscape'
352   */
353  function set_paper($size, $orientation = "portrait") {
354    $this->_paper_size = $size;
355    $this->_paper_orientation = $orientation;
356  }
357 
358  //........................................................................
359
360  /**
361   * Enable experimental caching capability
362   * @access private
363   */
364  function enable_caching($cache_id) {
365    $this->_cache_id = $cache_id;
366  }
367 
368  //........................................................................
369
370  /**
371   * Renders the HTML to PDF
372   */
373  function render() {
374
375    //enable_mem_profile();
376   
377    $this->_process_html();
378
379    $this->_css->apply_styles($this->_tree);
380
381    $root = null;
382   
383    foreach ($this->_tree->get_frames() as $frame) {
384
385      // Set up the root frame
386      if ( is_null($root) ) {
387        $root = Frame_Factory::decorate_root( $this->_tree->get_root(), $this );
388        continue;
389      }
390
391      // Create the appropriate decorators, reflowers & positioners.
392      $deco = Frame_Factory::decorate_frame($frame, $this);
393      $deco->set_root($root);
394
395      // FIXME: handle generated content
396      if ( $frame->get_style()->display == "list-item" ) {
397       
398        // Insert a list-bullet frame
399        $node = $this->_xml->createElement("bullet"); // arbitrary choice
400        $b_f = new Frame($node);
401
402        $style = $this->_css->create_style();
403        $style->display = "-dompdf-list-bullet";
404        $style->inherit($frame->get_style());
405        $b_f->set_style($style);
406       
407        $deco->prepend_child( Frame_Factory::decorate_frame($b_f, $this) );
408      }
409    }
410   
411    $this->_pdf = Canvas_Factory::get_instance($this->_paper_size, $this->_paper_orientation);
412
413    $root->set_containing_block(0, 0, $this->_pdf->get_width(), $this->_pdf->get_height());
414    $root->set_renderer(new Renderer($this));
415   
416    // This is where the magic happens:
417    $root->reflow();
418   
419    // Clean up cached images
420    Image_Cache::clear();
421  }
422   
423  //........................................................................
424
425  /**
426   * Streams the PDF to the client
427   *
428   * The file will open a download dialog by default.  The options
429   * parameter controls the output headers.  Accepted headers are:
430   *
431   * 'Accept-Ranges' => 1 or 0 - if this is not set to 1, then this
432   *    header is not included, off by default this header seems to
433   *    have caused some problems despite tha fact that it is supposed
434   *    to solve them, so I am leaving it off by default.
435   *
436   * 'compress' = > 1 or 0 - apply content stream compression, this is
437   *    on (1) by default
438   *
439   * 'Attachment' => 1 or 0 - if 1, force the browser to open a
440   *    download dialog, on (1) by default
441   *
442   * @param string $filename the name of the streamed file
443   * @param array  $options header options (see above)
444   */
445  function stream($filename, $options = null) {
446    if (!is_null($this->_pdf))
447      $this->_pdf->stream($filename, $options);
448  }
449
450  /**
451   * Returns the PDF as a string
452   *
453   * @return string
454   */
455  function output() {
456    global $_dompdf_debug;
457    if ( is_null($this->_pdf) )
458      return null;
459   
460    return $this->_pdf->output( $_dompdf_debug );
461  }
462 
463  //........................................................................
464 
465}
466?>
Note: See TracBrowser for help on using the repository browser.