Esempio n. 1
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();
 }
Esempio n. 2
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);
     // @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->_paper_size, $this->_paper_orientation);
     Font_Metrics::init($this->_pdf);
     if (DOMPDF_ENABLE_FONTSUBSETTING && $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;
             }
             // TODO Handle other generated content (pseudo elements)
         }
     }
     $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")) {
                 $xpath = new DOMXPath($this->_xml);
                 $count = $xpath->query("li", $parent_node)->length;
                 $parent_node->setAttribute("dompdf-children-count", $count);
             }
             if (!$parent_node->hasAttribute("dompdf-counter")) {
                 $index = $parent_node->hasAttribute("start") ? $parent_node->getAttribute("start") - 1 : 0;
             } 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));
         }
     }
     // 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();
 }