Beispiel #1
0
 /**
  * Renders the HTML to PDF
  */
 function render()
 {
     $this->save_locale();
     if (DOMPDF_LOG_OUTPUT_FILE) {
         if (!file_exists(DOMPDF_LOG_OUTPUT_FILE) && is_writable(dirname(DOMPDF_LOG_OUTPUT_FILE))) {
             touch(DOMPDF_LOG_OUTPUT_FILE);
         }
         $this->_start_time = microtime(true);
         ob_start();
     }
     //enable_mem_profile();
     $this->_process_html();
     $this->_css->apply_styles($this->_tree);
     $root = null;
     foreach ($this->_tree->get_frames() as $frame) {
         // Set up the root frame
         if (is_null($root)) {
             $root = Frame_Factory::decorate_root($this->_tree->get_root(), $this);
             continue;
         }
         // Create the appropriate decorators, reflowers & positioners.
         $deco = Frame_Factory::decorate_frame($frame, $this);
         $deco->set_root($root);
         // FIXME: handle generated content
         if ($frame->get_style()->display === "list-item") {
             // Insert a list-bullet frame
             $node = $this->_xml->createElement("bullet");
             // arbitrary choice
             $b_f = new Frame($node);
             $parent_node = $frame->get_parent()->get_node();
             if (!$parent_node->hasAttribute("dompdf-children-count")) {
                 $count = 0;
                 foreach ($parent_node->childNodes as $_node) {
                     if ($_node instanceof DOMElement) {
                         $count++;
                     }
                 }
                 $parent_node->setAttribute("dompdf-children-count", $count);
             }
             $index = 0;
             if (!$parent_node->hasAttribute("dompdf-counter")) {
                 $index = 1;
                 $parent_node->setAttribute("dompdf-counter", 1);
             } else {
                 $index = $parent_node->getAttribute("dompdf-counter");
                 $index++;
                 $parent_node->setAttribute("dompdf-counter", $index);
             }
             $node->setAttribute("dompdf-counter", $index);
             $style = $this->_css->create_style();
             $style->display = "-dompdf-list-bullet";
             $style->inherit($frame->get_style());
             $b_f->set_style($style);
             $deco->prepend_child(Frame_Factory::decorate_frame($b_f, $this));
         }
     }
     $page_styles = $this->_css->get_page_styles();
     $base_page_style = $page_styles["base"];
     unset($page_styles["base"]);
     foreach ($page_styles as $_page_style) {
         $_page_style->inherit($base_page_style);
     }
     if (is_array($base_page_style->size)) {
         $this->set_paper(array(0, 0, $base_page_style->size[0], $base_page_style->size[1]));
     }
     $this->_pdf = Canvas_Factory::get_instance($this->_paper_size, $this->_paper_orientation);
     // Add meta information
     $title = $this->_xml->getElementsByTagName("title");
     if ($title->length) {
         $this->_pdf->add_info("Title", trim($title->item(0)->nodeValue));
     }
     $metas = $this->_xml->getElementsByTagName("meta");
     $labels = array("author" => "Author", "keywords" => "Keywords", "description" => "Subject");
     foreach ($metas as $meta) {
         $name = mb_strtolower($meta->getAttribute("name"));
         $value = trim($meta->getAttribute("content"));
         if (isset($labels[$name])) {
             $this->_pdf->add_info($labels[$name], $value);
             continue;
         }
         if ($name === "dompdf.view" && $this->parse_default_view($value)) {
             $this->_pdf->set_default_view($this->_default_view, $this->_default_view_options);
         }
     }
     $root->set_containing_block(0, 0, $this->_pdf->get_width(), $this->_pdf->get_height());
     $root->set_renderer(new Renderer($this));
     // This is where the magic happens:
     $root->reflow();
     // Clean up cached images
     Image_Cache::clear();
     global $_dompdf_warnings, $_dompdf_show_warnings;
     if ($_dompdf_show_warnings) {
         echo '<b>DOMPDF Warnings</b><br><pre>';
         foreach ($_dompdf_warnings as $msg) {
             echo $msg . "\n";
         }
         echo $this->get_canvas()->get_cpdf()->messages;
         echo '</pre>';
         flush();
     }
     $this->restore_locale();
 }
Beispiel #2
0
 /**
  * Renders the HTML to PDF
  */
 function render()
 {
     $this->save_locale();
     $log_output_file = $this->get_option("log_output_file");
     if ($log_output_file) {
         if (!file_exists($log_output_file) && is_writable(dirname($log_output_file))) {
             touch($log_output_file);
         }
         $this->_start_time = microtime(true);
         ob_start();
     }
     //enable_mem_profile();
     $this->_process_html();
     $this->_css->apply_styles($this->_tree);
     // @page style rules : size, margins
     $page_styles = $this->_css->get_page_styles();
     $base_page_style = $page_styles["base"];
     unset($page_styles["base"]);
     foreach ($page_styles as $_page_style) {
         $_page_style->inherit($base_page_style);
     }
     if (is_array($base_page_style->size)) {
         $this->set_paper(array(0, 0, $base_page_style->size[0], $base_page_style->size[1]));
     }
     $this->_pdf = Canvas_Factory::get_instance($this, $this->_paper_size, $this->_paper_orientation);
     Font_Metrics::init($this->_pdf);
     if ($this->get_option("enable_font_subsetting") && $this->_pdf instanceof CPDF_Adapter) {
         foreach ($this->_tree->get_frames() as $frame) {
             $style = $frame->get_style();
             $node = $frame->get_node();
             // Handle text nodes
             if ($node->nodeName === "#text") {
                 $this->_pdf->register_string_subset($style->font_family, $node->nodeValue);
                 continue;
             }
             // Handle generated content (list items)
             if ($style->display === "list-item") {
                 $chars = List_Bullet_Renderer::get_counter_chars($style->list_style_type);
                 $this->_pdf->register_string_subset($style->font_family, $chars);
                 continue;
             }
             // Handle other generated content (pseudo elements)
             // FIXME: This only captures the text of the stylesheet declaration,
             //        not the actual generated content, and forces all possible counter
             //        values. See notes in issue #750.
             if ($frame->get_node()->nodeName == "dompdf_generated") {
                 // all possible counter values
                 $chars = List_Bullet_Renderer::get_counter_chars('decimal');
                 $this->_pdf->register_string_subset($style->font_family, $chars);
                 $chars = List_Bullet_Renderer::get_counter_chars('upper-alpha');
                 $this->_pdf->register_string_subset($style->font_family, $chars);
                 $chars = List_Bullet_Renderer::get_counter_chars('lower-alpha');
                 $this->_pdf->register_string_subset($style->font_family, $chars);
                 $chars = List_Bullet_Renderer::get_counter_chars('lower-greek');
                 $this->_pdf->register_string_subset($style->font_family, $chars);
                 // the text of the stylesheet declaration
                 $this->_pdf->register_string_subset($style->font_family, $style->content);
                 continue;
             }
         }
     }
     $root = null;
     foreach ($this->_tree->get_frames() as $frame) {
         // Set up the root frame
         if (is_null($root)) {
             $root = Frame_Factory::decorate_root($this->_tree->get_root(), $this);
             continue;
         }
         // Create the appropriate decorators, reflowers & positioners.
         Frame_Factory::decorate_frame($frame, $this, $root);
     }
     // Add meta information
     $title = $this->_xml->getElementsByTagName("title");
     if ($title->length) {
         $this->_pdf->add_info("Title", trim($title->item(0)->nodeValue));
     }
     $metas = $this->_xml->getElementsByTagName("meta");
     $labels = array("author" => "Author", "keywords" => "Keywords", "description" => "Subject");
     foreach ($metas as $meta) {
         $name = mb_strtolower($meta->getAttribute("name"));
         $value = trim($meta->getAttribute("content"));
         if (isset($labels[$name])) {
             $this->_pdf->add_info($labels[$name], $value);
             continue;
         }
         if ($name === "dompdf.view" && $this->parse_default_view($value)) {
             $this->_pdf->set_default_view($this->_default_view, $this->_default_view_options);
         }
     }
     $root->set_containing_block(0, 0, $this->_pdf->get_width(), $this->_pdf->get_height());
     $root->set_renderer(new Renderer($this));
     // This is where the magic happens:
     $root->reflow();
     // Clean up cached images
     Image_Cache::clear();
     global $_dompdf_warnings, $_dompdf_show_warnings;
     if ($_dompdf_show_warnings) {
         echo '<b>DOMPDF Warnings</b><br><pre>';
         foreach ($_dompdf_warnings as $msg) {
             echo $msg . "\n";
         }
         echo $this->get_canvas()->get_cpdf()->messages;
         echo '</pre>';
         flush();
     }
     $this->restore_locale();
 }
 /**
  * Renders the HTML to PDF
  */
 function render()
 {
     //enable_mem_profile();
     $this->_process_html();
     $this->_css->apply_styles($this->_tree);
     $root = null;
     foreach ($this->_tree->get_frames() as $frame) {
         // Set up the root frame
         if (is_null($root)) {
             $root = Frame_Factory::decorate_root($this->_tree->get_root(), $this);
             continue;
         }
         // Create the appropriate decorators, reflowers & positioners.
         $deco = Frame_Factory::decorate_frame($frame, $this);
         $deco->set_root($root);
         // FIXME: handle generated content
         if ($frame->get_style()->display === "list-item") {
             // Insert a list-bullet frame
             $node = $this->_xml->createElement("bullet");
             // arbitrary choice
             $b_f = new Frame($node);
             $style = $this->_css->create_style();
             $style->display = "-dompdf-list-bullet";
             $style->inherit($frame->get_style());
             $b_f->set_style($style);
             $deco->prepend_child(Frame_Factory::decorate_frame($b_f, $this));
         }
     }
     $this->_pdf = Canvas_Factory::get_instance($this->_paper_size, $this->_paper_orientation);
     $root->set_containing_block(0, 0, $this->_pdf->get_width(), $this->_pdf->get_height());
     $root->set_renderer(new Renderer($this));
     // This is where the magic happens:
     $root->reflow();
     // Clean up cached images
     Image_Cache::clear();
     global $_dompdf_warnings, $_dompdf_show_warnings;
     if ($_dompdf_show_warnings) {
         echo '<b>DOMPDF Warnings</b><br><pre>';
         foreach ($_dompdf_warnings as $msg) {
             echo $msg . "\n";
         }
         echo $this->get_canvas()->get_cpdf()->messages;
         echo '</pre>';
         flush();
     }
 }
Beispiel #4
0
 /**
  * applies all current styles to a particular document tree
  *
  * apply_styles() applies all currently loaded styles to the provided
  * {@link Frame_Tree}.  Aside from parsing CSS, this is the main purpose
  * of this class.
  *
  * @param Frame_Tree $tree
  */
 function apply_styles(Frame_Tree $tree)
 {
     // Use XPath to select nodes.  This would be easier if we could attach
     // Frame objects directly to DOMNodes using the setUserData() method, but
     // we can't do that just yet.  Instead, we set a _node attribute_ in
     // Frame->set_id() and use that as a handle on the Frame object via
     // Frame_Tree::$_registry.
     // We create a scratch array of styles indexed by frame id.  Once all
     // styles have been assigned, we order the cached styles by specificity
     // and create a final style object to assign to the frame.
     // FIXME: this is not particularly robust...
     $styles = array();
     $xp = new DOMXPath($tree->get_dom());
     // Add generated content
     foreach ($this->_styles as $selector => $style) {
         if (strpos($selector, ":before") === false && strpos($selector, ":after") === false) {
             continue;
         }
         $query = $this->_css_selector_to_xpath($selector, true);
         // Retrieve the nodes
         $nodes = @$xp->query($query["query"]);
         if ($nodes == null) {
             record_warnings(E_USER_WARNING, "The CSS selector '{$selector}' is not valid", __FILE__, __LINE__);
             continue;
         }
         foreach ($nodes as $i => $node) {
             foreach ($query["pseudo_elements"] as $pos) {
                 if (($src = $this->_image($style->content)) !== "none") {
                     $new_node = $node->ownerDocument->createElement("img_generated");
                     $new_node->setAttribute("src", $src);
                 } else {
                     $new_node = $node->ownerDocument->createElement("dompdf_generated");
                 }
                 $new_node->setAttribute($pos, $pos);
                 $tree->insert_node($node, $new_node, $pos);
             }
         }
     }
     // Apply all styles in stylesheet
     foreach ($this->_styles as $selector => $style) {
         $query = $this->_css_selector_to_xpath($selector);
         // Retrieve the nodes
         $nodes = @$xp->query($query["query"]);
         if ($nodes == null) {
             record_warnings(E_USER_WARNING, "The CSS selector '{$selector}' is not valid", __FILE__, __LINE__);
             continue;
         }
         foreach ($nodes as $node) {
             // Retrieve the node id
             if ($node->nodeType != XML_ELEMENT_NODE) {
                 // Only DOMElements get styles
                 continue;
             }
             $id = $node->getAttribute("frame_id");
             // Assign the current style to the scratch array
             $spec = $this->_specificity($selector);
             $styles[$id][$spec][] = $style;
         }
     }
     // Now create the styles and assign them to the appropriate frames.  (We
     // iterate over the tree using an implicit Frame_Tree iterator.)
     $root_flg = false;
     foreach ($tree->get_frames() as $frame) {
         // pre_r($frame->get_node()->nodeName . ":");
         if (!$root_flg && $this->_page_style) {
             $style = $this->_page_style;
             $root_flg = true;
         } else {
             $style = $this->create_style();
         }
         // Find nearest DOMElement parent
         $p = $frame;
         while ($p = $p->get_parent()) {
             if ($p->get_node()->nodeType == XML_ELEMENT_NODE) {
                 break;
             }
         }
         // Styles can only be applied directly to DOMElements; anonymous
         // frames inherit from their parent
         if ($frame->get_node()->nodeType != XML_ELEMENT_NODE) {
             if ($p) {
                 $style->inherit($p->get_style());
             }
             $frame->set_style($style);
             continue;
         }
         $id = $frame->get_id();
         // Handle HTML 4.0 attributes
         Attribute_Translator::translate_attributes($frame);
         if (($str = $frame->get_node()->getAttribute(Attribute_Translator::$_style_attr)) !== "") {
             // Lowest specificity
             $styles[$id][1][] = $this->_parse_properties($str);
         }
         // Locate any additional style attributes
         if (($str = $frame->get_node()->getAttribute("style")) !== "") {
             // Destroy CSS comments
             $str = preg_replace("'/\\*.*?\\*/'si", "", $str);
             $spec = $this->_specificity("!style attribute");
             $styles[$id][$spec][] = $this->_parse_properties($str);
         }
         // Grab the applicable styles
         if (isset($styles[$id])) {
             $applied_styles = $styles[$frame->get_id()];
             // Sort by specificity
             ksort($applied_styles);
             if (DEBUGCSS) {
                 $debug_nodename = $frame->get_node()->nodeName;
                 print "<pre>\n[{$debug_nodename}\n";
                 foreach ($applied_styles as $spec => $arr) {
                     printf("specificity: 0x%08x\n", $spec);
                     foreach ($arr as $s) {
                         print "[\n";
                         $s->debug_print();
                         print "]\n";
                     }
                 }
             }
             // Merge the new styles with the inherited styles
             foreach ($applied_styles as $arr) {
                 foreach ($arr as $s) {
                     $style->merge($s);
                 }
             }
         }
         // Inherit parent's styles if required
         if ($p) {
             if (DEBUGCSS) {
                 print "inherit:\n";
                 print "[\n";
                 $p->get_style()->debug_print();
                 print "]\n";
             }
             $style->inherit($p->get_style());
         }
         if (DEBUGCSS) {
             print "DomElementStyle:\n";
             print "[\n";
             $style->debug_print();
             print "]\n";
             print "/{$debug_nodename}]\n</pre>";
         }
         /*DEBUGCSS print: see below different print debugging method
           pre_r($frame->get_node()->nodeName . ":");
           echo "<pre>";
           echo $style;
           echo "</pre>";*/
         $frame->set_style($style);
     }
     // We're done!  Clean out the registry of all styles since we
     // won't be needing this later.
     foreach (array_keys($this->_styles) as $key) {
         $this->_styles[$key] = null;
         unset($this->_styles[$key]);
     }
 }
 /**
  * applies all current styles to a particular document tree
  *
  * apply_styles() applies all currently loaded styles to the provided
  * {@link Frame_Tree}.  Aside from parsing CSS, this is the main purpose
  * of this class.
  *
  * @param Frame_Tree $tree
  */
 function apply_styles(Frame_Tree $tree)
 {
     // Use XPath to select nodes.  This would be easier if we could attach
     // Frame objects directly to DOMNodes using the setUserData() method, but
     // we can't do that just yet.  Instead, we set a _node attribute_ in
     // Frame->set_id() and use that as a handle on the Frame object via
     // Frame_Tree::$_registry.
     // We create a scratch array of styles indexed by frame id.  Once all
     // styles have been assigned, we order the cached styles by specificity
     // and create a final style object to assign to the frame.
     // FIXME: this is not particularly robust...
     $styles = array();
     $xp = new DOMXPath($tree->get_dom());
     // Apply all styles in stylesheet
     foreach ($this->_styles as $selector => $style) {
         $query = $this->_css_selector_to_xpath($selector);
         //       pre_var_dump($selector);
         //       pre_var_dump($query);
         //        echo ($style);
         // Retrieve the nodes
         $nodes = $xp->query($query);
         foreach ($nodes as $node) {
             //echo $node->nodeName . "\n";
             // Retrieve the node id
             if ($node->nodeType != 1) {
                 // Only DOMElements get styles
                 continue;
             }
             $id = $node->getAttribute("frame_id");
             // Assign the current style to the scratch array
             $spec = $this->_specificity($selector);
             $styles[$id][$spec][] = $style;
         }
     }
     // Now create the styles and assign them to the appropriate frames.  (We
     // iterate over the tree using an implicit Frame_Tree iterator.)
     $root_flg = false;
     foreach ($tree->get_frames() as $frame) {
         // pre_r($frame->get_node()->nodeName . ":");
         if (!$root_flg && $this->_page_style) {
             $style = $this->_page_style;
             $root_flg = true;
         } else {
             $style = $this->create_style();
         }
         // Find nearest DOMElement parent
         $p = $frame;
         while ($p = $p->get_parent()) {
             if ($p->get_node()->nodeType == 1) {
                 break;
             }
         }
         // Styles can only be applied directly to DOMElements; anonymous
         // frames inherit from their parent
         if ($frame->get_node()->nodeType != 1) {
             if ($p) {
                 $style->inherit($p->get_style());
             }
             $frame->set_style($style);
             continue;
         }
         $id = $frame->get_id();
         // Handle HTML 4.0 attributes
         Attribute_Translator::translate_attributes($frame);
         // Locate any additional style attributes
         if (($str = $frame->get_node()->getAttribute("style")) !== "") {
             $spec = $this->_specificity("!style attribute");
             $styles[$id][$spec][] = $this->_parse_properties($str);
         }
         // Grab the applicable styles
         if (isset($styles[$id])) {
             $applied_styles = $styles[$frame->get_id()];
             // Sort by specificity
             ksort($applied_styles);
             // Merge the new styles with the inherited styles
             foreach ($applied_styles as $arr) {
                 foreach ($arr as $s) {
                     $style->merge($s);
                 }
             }
         }
         // Inherit parent's styles if required
         if ($p) {
             $style->inherit($p->get_style());
         }
         //       pre_r($frame->get_node()->nodeName . ":");
         //      echo "<pre>";
         //      echo $style;
         //      echo "</pre>";
         $frame->set_style($style);
     }
     // We're done!  Clean out the registry of all styles since we
     // won't be needing this later.
     foreach (array_keys($this->_styles) as $key) {
         unset($this->_styles[$key]);
     }
 }
Beispiel #6
0
 /**
  * Renders the HTML to PDF
  */
 function render()
 {
     $this->_process_html();
     $this->_css->apply_styles($this->_tree);
     $root = null;
     foreach ($this->_tree->get_frames() as $frame) {
         // Set up the root frame
         if (is_null($root)) {
             $root = Frame_Factory::decorate_root($this->_tree->get_root());
             continue;
         }
         // Create the appropriate decorators, reflowers & positioners.
         $deco = Frame_Factory::decorate_frame($frame, $this);
         $deco->set_root($root);
         // FIXME: handle generated content
         if ($frame->get_style()->display == "list-item" && in_array($frame->get_style()->list_style_type, List_Bullet_Frame_Decorator::$BULLET_TYPES)) {
             // Insert a list-bullet frame
             $node = $this->_xml->createElement("bullet");
             // arbitrary choice
             $b_f = new Frame($node);
             $style = $this->_css->create_style();
             $style->display = "-dompdf-list-bullet";
             $style->inherit($frame->get_style());
             $b_f->set_style($style);
             $deco->prepend_child(Frame_Factory::decorate_frame($b_f));
         }
     }
     $this->_pdf = Canvas_Factory::get_instance($this->_paper_size, $this->_orientation);
     $root->set_containing_block(0, 0, $this->_pdf->get_width(), $this->_pdf->get_height());
     // This is where the magic happens:
     $root->reflow();
     $renderer = new Renderer($this->_pdf);
     $renderer->render($root);
 }
Beispiel #7
0
    /**
     * Builds the {@link Frame_Tree}, loads any CSS and applies the styles to
     * the {@link Frame_Tree}
     */
    protected function _process_html()
    {
        $this->save_locale();

        $this->_tree->build_tree();

        $this->_css->load_css_file(Stylesheet::DEFAULT_STYLESHEET, Stylesheet::ORIG_UA);

        $acceptedmedia = Stylesheet::$ACCEPTED_GENERIC_MEDIA_TYPES;
        if (defined("DOMPDF_DEFAULT_MEDIA_TYPE")) {
            $acceptedmedia[] = DOMPDF_DEFAULT_MEDIA_TYPE;
        } else {
            $acceptedmedia[] = Stylesheet::$ACCEPTED_DEFAULT_MEDIA_TYPE;
        }

        // 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");
                $url = build_url($this->_protocol, $this->_base_host, $this->_base_path, $url);

                $this->_css->load_css_file($url, Stylesheet::ORIG_AUTHOR);
            }

        }

        // Set the base path of the Stylesheet to that of the file being processed
        $this->_css->set_protocol($this->_protocol);
        $this->_css->set_host($this->_base_host);
        $this->_css->set_base_path($this->_base_path);

        // load <style> tags
        $styles = $this->_xml->getElementsByTagName("style");
        foreach ($styles as $style) {

            // Accept all <style> tags by default (note this is contrary to W3C
            // HTML 4.0 spec:
            // http://www.w3.org/TR/REC-html40/present/styles.html#adef-media
            // which states that the default media type is 'screen'
            if ($style->hasAttributes() &&
                ($media = $style->getAttribute("media")) &&
                !in_array($media, $acceptedmedia)
            )
                continue;

            $css = "";
            if ($style->hasChildNodes()) {

                $child = $style->firstChild;
                while ($child) {
                    $css .= $child->nodeValue; // Handle <style><!-- blah --></style>
                    $child = $child->nextSibling;
                }

            } else
                $css = $style->nodeValue;

            $this->_css->load_css($css);
        }

        $this->restore_locale();
    }
 /**
  * Builds the {@link Frame_Tree}, loads any CSS and applies the styles to
  * the {@link Frame_Tree}
  */
 protected function _process_html()
 {
     $this->_tree->build_tree();
     $this->_css->load_css_file(Stylesheet::DEFAULT_STYLESHEET, Stylesheet::ORIG_UA);
     $acceptedmedia = Stylesheet::$ACCEPTED_GENERIC_MEDIA_TYPES;
     $acceptedmedia[] = $this->get_option("default_media_type");
     // <base href="" />
     $base_nodes = $this->_xml->getElementsByTagName("base");
     if ($base_nodes->length && ($href = $base_nodes->item(0)->getAttribute("href"))) {
         list($this->_protocol, $this->_base_host, $this->_base_path) = explode_url($href);
     }
     // Set the base path of the Stylesheet to that of the file being processed
     $this->_css->set_protocol($this->_protocol);
     $this->_css->set_host($this->_base_host);
     $this->_css->set_base_path($this->_base_path);
     // Get all the stylesheets so that they are processed in document order
     $xpath = new DOMXPath($this->_xml);
     $stylesheets = $xpath->query("//*[name() = 'link' or name() = 'style']");
     foreach ($stylesheets as $tag) {
         switch (strtolower($tag->nodeName)) {
             // load <link rel="STYLESHEET" ... /> tags
             case "link":
                 if (mb_strtolower(stripos($tag->getAttribute("rel"), "stylesheet") !== false) || mb_strtolower($tag->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,]/", $tag->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 = $tag->getAttribute("href");
                     $url = build_url($this->_protocol, $this->_base_host, $this->_base_path, $url);
                     $this->_css->load_css_file($url, Stylesheet::ORIG_AUTHOR);
                 }
                 break;
                 // load <style> tags
             // load <style> tags
             case "style":
                 // Accept all <style> tags by default (note this is contrary to W3C
                 // HTML 4.0 spec:
                 // http://www.w3.org/TR/REC-html40/present/styles.html#adef-media
                 // which states that the default media type is 'screen'
                 if ($tag->hasAttributes() && ($media = $tag->getAttribute("media")) && !in_array($media, $acceptedmedia)) {
                     continue;
                 }
                 $css = "";
                 if ($tag->hasChildNodes()) {
                     $child = $tag->firstChild;
                     while ($child) {
                         $css .= $child->nodeValue;
                         // Handle <style><!-- blah --></style>
                         $child = $child->nextSibling;
                     }
                 } else {
                     $css = $tag->nodeValue;
                 }
                 $this->_css->load_css($css);
                 break;
         }
     }
 }
 function apply_styles(Frame_Tree $tree)
 {
     $styles = array();
     $xp = new DOMXPath($tree->get_dom());
     foreach ($this->_styles as $selector => $style) {
         if (strpos($selector, ":before") === false && strpos($selector, ":after") === false) {
             continue;
         }
         $query = $this->_css_selector_to_xpath($selector, true);
         $nodes = @$xp->query($query["query"]);
         if ($nodes == null) {
             record_warnings(E_USER_WARNING, "The CSS selector '{$selector}' is not valid", __FILE__, __LINE__);
             continue;
         }
         foreach ($nodes as $i => $node) {
             foreach ($query["pseudo_elements"] as $pos) {
                 if ($node->hasAttribute("dompdf_{$pos}_frame_id")) {
                     continue;
                 }
                 if (($src = $this->_image($style->content)) !== "none") {
                     $new_node = $node->ownerDocument->createElement("img_generated");
                     $new_node->setAttribute("src", $src);
                 } else {
                     $new_node = $node->ownerDocument->createElement("dompdf_generated");
                 }
                 $new_node->setAttribute($pos, $pos);
                 $new_frame_id = $tree->insert_node($node, $new_node, $pos);
                 $node->setAttribute("dompdf_{$pos}_frame_id", $new_frame_id);
             }
         }
     }
     foreach ($this->_styles as $selector => $style) {
         $query = $this->_css_selector_to_xpath($selector);
         $nodes = @$xp->query($query["query"]);
         if ($nodes == null) {
             record_warnings(E_USER_WARNING, "The CSS selector '{$selector}' is not valid", __FILE__, __LINE__);
             continue;
         }
         foreach ($nodes as $node) {
             if ($node->nodeType != XML_ELEMENT_NODE) {
                 continue;
             }
             $id = $node->getAttribute("frame_id");
             $spec = $this->_specificity($selector);
             $styles[$id][$spec][] = $style;
         }
     }
     $root_flg = false;
     foreach ($tree->get_frames() as $frame) {
         if (!$root_flg && $this->_page_styles["base"]) {
             $style = $this->_page_styles["base"];
             $root_flg = true;
         } else {
             $style = $this->create_style();
         }
         $p = $frame;
         while ($p = $p->get_parent()) {
             if ($p->get_node()->nodeType == XML_ELEMENT_NODE) {
                 break;
             }
         }
         if ($frame->get_node()->nodeType != XML_ELEMENT_NODE) {
             if ($p) {
                 $style->inherit($p->get_style());
             }
             $frame->set_style($style);
             continue;
         }
         $id = $frame->get_id();
         Attribute_Translator::translate_attributes($frame);
         if (($str = $frame->get_node()->getAttribute(Attribute_Translator::$_style_attr)) !== "") {
             $styles[$id][1][] = $this->_parse_properties($str);
         }
         if (($str = $frame->get_node()->getAttribute("style")) !== "") {
             $str = preg_replace("'/\\*.*?\\*/'si", "", $str);
             $spec = $this->_specificity("!attr");
             $styles[$id][$spec][] = $this->_parse_properties($str);
         }
         if (isset($styles[$id])) {
             $applied_styles = $styles[$frame->get_id()];
             ksort($applied_styles);
             if (DEBUGCSS) {
                 $debug_nodename = $frame->get_node()->nodeName;
                 print "<pre>\n[{$debug_nodename}\n";
                 foreach ($applied_styles as $spec => $arr) {
                     printf("specificity: 0x%08x\n", $spec);
                     foreach ($arr as $s) {
                         print "[\n";
                         $s->debug_print();
                         print "]\n";
                     }
                 }
             }
             foreach ($applied_styles as $arr) {
                 foreach ($arr as $s) {
                     $style->merge($s);
                 }
             }
         }
         if ($p) {
             if (DEBUGCSS) {
                 print "inherit:\n";
                 print "[\n";
                 $p->get_style()->debug_print();
                 print "]\n";
             }
             $style->inherit($p->get_style());
         }
         if (DEBUGCSS) {
             print "DomElementStyle:\n";
             print "[\n";
             $style->debug_print();
             print "]\n";
             print "/{$debug_nodename}]\n</pre>";
         }
         $frame->set_style($style);
     }
     foreach (array_keys($this->_styles) as $key) {
         $this->_styles[$key] = null;
         unset($this->_styles[$key]);
     }
 }