DOMPDF : Missing Images and Unapplied Stylesheets

I'm currently using DOMPDF to convert an HTML page to PDF. The generated PDFs are fine, except that some styles doesn't seem to get applied and some images ended up missing. After setting $_dompdf_show_warnings to true to debug the process, I found out that relative paths with leading slashes are interpreted as absolute file paths in the local file system. For example, if my document root is at /var/www/html/ and the relative path of the stylesheet (in the html markup) is /css , DOMPDF interprets it as /css instead of /var/www/html/css. The same happens with the case of images.

The application is created with the Yii Framework and uses pretty urls (the usual stuff you see), so it will be almost impossible to use paths relative to the current directory (../). I needed to them to point relative to the top level (/), keeping the leading slashes.

The solution: trim leading slashes accordingly. I had to be careful doing that though, since DOMPDF loads some default styles found inside its include folder and referenced using absolute file path (with leading slashes).

Here are the files with the necessary changes applied. It's just a matter of ltrim()-ming the url.

For the stylesheets:
dompdf/include/dompdf.cls.php

  /**
   * Builds the [email protected] Frame_Tree}, loads any CSS and applies the styles to
   * the [email protected] Frame_Tree}
   */
  protected function _process_html() {

    // ... long code removed for brevity. DO NOT REMOVE THE CODE FROM THE ACTUAL FILE
          
    // load <link rel="STYLESHEET" ... /> tags
    $links = $this->_xml->getElementsByTagName("link");
    foreach ($links as $link) {
      if ( mb_strtolower($link->getAttribute("rel")) === "stylesheet" ||
           mb_strtolower($link->getAttribute("type")) === "text/css" ) {
        //Check if the css file is for an accepted media type
        //media not given then always valid
        $formedialist = preg_split("/[\s\n,]/", $link->getAttribute("media"),-1, PREG_SPLIT_NO_EMPTY);
        if ( count($formedialist) > 0 ) {
          $accept = false;
          foreach ( $formedialist as $type ) {
            if ( in_array(mb_strtolower(trim($type)), $acceptedmedia) ) {
              $accept = true;
              break;
            }
          }
          if (!$accept) {
            //found at least one mediatype, but none of the accepted ones
            //Skip this css file.
            continue;
          }
        }
           
        $url = $link->getAttribute("href");
        
        // Fix for relative paths with leading slash - Rolan
        $url = ltrim($url, '/');
        
        $url = build_url($this->_protocol, $this->_base_host, $this->_base_path, $url);

        $this->_css->load_css_file($url);
      }

    }

    // ... more long code removed for brevity. DO NOT REMOVE THE CODE FROM THE ACTUAL FILE

  }

For the images:
dompdf/include/image_frame_decorator.cls.php
==

<br />
  /**<br />
   * Class constructor<br />
   *<br />
   * @param Frame $frame the frame to decorate<br />
   * @param <span class="caps">DOMPDF </span>$dompdf the document's dompdf object (required to resolve relative &amp; remote urls)<br />
   */<br />
  function __construct(Frame $frame, <span class="caps">DOMPDF </span>$dompdf) {<br />
    global $_dompdf_warnings;<br />
    <br />
    parent::__construct($frame, $dompdf);<br />
    $url = $frame-&gt;get_node()-&gt;getAttribute("src");<br />
      <br />
    // Fix for relative paths with leading slash - Rolan<br />
    $url = ltrim($url, '/');<br />
    <br />
    //debugpng<br />
    if (DEBUGPNG) print '[__construct '.$url.']';</p>

<p>    list($this-&gt;_image_url, $this-&gt;_image_ext) = Image_Cache::resolve_url($url,<br />
                                                                          $dompdf-&gt;get_protocol(),<br />
                                                                          $dompdf-&gt;get_host(),<br />
                                                                          $dompdf-&gt;get_base_path());</p>

<p>    if ( strrpos( $this-&gt;_image_url, <span class="caps">DOMPDF</span>_LIB_DIR . "/res/broken_image.png", 0) !== false &amp;&amp;<br />
         $alt = $frame-&gt;get_node()-&gt;getAttribute("alt") ) {<br />
      $style = $frame-&gt;get_style();<br />
      $style-&gt;width  = (4/3)*Font_Metrics::get_text_width($alt, $style-&gt;font_family, $style-&gt;font_size, $style-&gt;word_spacing);<br />
      $style-&gt;height = Font_Metrics::get_font_height($style-&gt;font_family, $style-&gt;font_size);<br />
    }<br />
  }<br />

==

Note that I'm using the latest release (DOMPDF 0.6 beta 2) at the time of this writing.

 

Comments

No comments so far.

Leave a Reply

 
(will not be published)
 
 
Comment
 
 

 

Resources