Exemplo n.º 1
0
 /**
  * Renders the HTML to PDF
  */
 public function render()
 {
     $this->saveLocale();
     $logOutputFile = $this->options->getLogOutputFile();
     if ($logOutputFile) {
         if (!file_exists($logOutputFile) && is_writable(dirname($logOutputFile))) {
             touch($logOutputFile);
         }
         $this->startTime = microtime(true);
         ob_start();
     }
     $this->processHtml();
     $this->css->apply_styles($this->tree);
     // @page style rules : size, margins
     $pageStyles = $this->css->get_page_styles();
     $basePageStyle = $pageStyles["base"];
     unset($pageStyles["base"]);
     foreach ($pageStyles as $pageStyle) {
         $pageStyle->inherit($basePageStyle);
     }
     if (is_array($basePageStyle->size)) {
         $this->setPaper(array(0, 0, $basePageStyle->size[0], $basePageStyle->size[1]));
     }
     //TODO: We really shouldn't be doing this; properties were already set in the constructor. We should add Canvas methods to set the page size and orientation after instantiaion.
     $this->setCanvas(CanvasFactory::get_instance($this, $this->paperSize, $this->paperOrientation));
     $this->setFontMetrics(new FontMetrics($this->pdf, $this->getOptions()));
     if ($this->options->isFontSubsettingEnabled() && $this->pdf instanceof CPDF) {
         foreach ($this->tree->get_frames() as $frame) {
             $style = $frame->get_style();
             $node = $frame->get_node();
             // Handle text nodes
             if ($node->nodeName === "#text") {
                 $this->getCanvas()->register_string_subset($style->font_family, $node->nodeValue);
                 continue;
             }
             // Handle generated content (list items)
             if ($style->display === "list-item") {
                 $chars = ListBullet::get_counter_chars($style->list_style_type);
                 $this->getCanvas()->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 = ListBullet::get_counter_chars('decimal');
                 $this->getCanvas()->register_string_subset($style->font_family, $chars);
                 $chars = ListBullet::get_counter_chars('upper-alpha');
                 $this->getCanvas()->register_string_subset($style->font_family, $chars);
                 $chars = ListBullet::get_counter_chars('lower-alpha');
                 $this->getCanvas()->register_string_subset($style->font_family, $chars);
                 $chars = ListBullet::get_counter_chars('lower-greek');
                 $this->getCanvas()->register_string_subset($style->font_family, $chars);
                 // the text of the stylesheet declaration
                 $this->getCanvas()->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 = Factory::decorate_root($this->tree->get_root(), $this);
             continue;
         }
         // Create the appropriate decorators, reflowers & positioners.
         Factory::decorate_frame($frame, $this, $root);
     }
     // Add meta information
     $title = $this->dom->getElementsByTagName("title");
     if ($title->length) {
         $this->getCanvas()->add_info("Title", trim($title->item(0)->nodeValue));
     }
     $metas = $this->dom->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->parseDefaultView($value)) {
             $this->getCanvas()->set_default_view($this->defaultView, $this->defaultViewOptions);
         }
     }
     $root->set_containing_block(0, 0, $this->getCanvas()->get_width(), $this->getCanvas()->get_height());
     $root->set_renderer(new Renderer($this));
     // This is where the magic happens:
     $root->reflow();
     // Clean up cached images
     Cache::clear();
     global $_dompdf_warnings, $_dompdf_show_warnings;
     if ($_dompdf_show_warnings && isset($_dompdf_warnings)) {
         echo '<b>Dompdf Warnings</b><br><pre>';
         foreach ($_dompdf_warnings as $msg) {
             echo $msg . "\n";
         }
         echo $this->getCanvas()->get_cpdf()->messages;
         echo '</pre>';
         flush();
     }
     $this->restoreLocale();
 }