Exemple #1
0
 /**
  * Decorate a Frame
  *
  * @param Frame $frame   The frame to decorate
  * @param Dompdf $dompdf The dompdf instance
  * @param Frame $root    The frame to decorate
  *
  * @throws Exception
  * @return AbstractFrameDecorator
  * FIXME: this is admittedly a little smelly...
  */
 static function decorate_frame(Frame $frame, Dompdf $dompdf, Frame $root = null)
 {
     if (is_null($dompdf)) {
         throw new Exception("The DOMPDF argument is required");
     }
     $style = $frame->get_style();
     // Floating (and more generally out-of-flow) elements are blocks
     // http://coding.smashingmagazine.com/2007/05/01/css-float-theory-things-you-should-know/
     if (!$frame->is_in_flow() && in_array($style->display, Style::$INLINE_TYPES)) {
         $style->display = "block";
     }
     $display = $style->display;
     switch ($display) {
         case "block":
             $positioner = "Block";
             $decorator = "Block";
             $reflower = "Block";
             break;
         case "inline-block":
             $positioner = "Inline";
             $decorator = "Block";
             $reflower = "Block";
             break;
         case "inline":
             $positioner = "Inline";
             if ($frame->is_text_node()) {
                 $decorator = "Text";
                 $reflower = "Text";
             } else {
                 $enable_css_float = $dompdf->get_option("enable_css_float");
                 if ($enable_css_float && $style->float !== "none") {
                     $decorator = "Block";
                     $reflower = "Block";
                 } else {
                     $decorator = "Inline";
                     $reflower = "Inline";
                 }
             }
             break;
         case "table":
             $positioner = "Block";
             $decorator = "Table";
             $reflower = "Table";
             break;
         case "inline-table":
             $positioner = "Inline";
             $decorator = "Table";
             $reflower = "Table";
             break;
         case "table-row-group":
         case "table-header-group":
         case "table-footer-group":
             $positioner = "Null";
             $decorator = "TableRowGroup";
             $reflower = "TableRowGroup";
             break;
         case "table-row":
             $positioner = "Null";
             $decorator = "TableRow";
             $reflower = "TableRow";
             break;
         case "table-cell":
             $positioner = "TableCell";
             $decorator = "TableCell";
             $reflower = "TableCell";
             break;
         case "list-item":
             $positioner = "Block";
             $decorator = "Block";
             $reflower = "Block";
             break;
         case "-dompdf-list-bullet":
             if ($style->list_style_position === "inside") {
                 $positioner = "Inline";
             } else {
                 $positioner = "ListBullet";
             }
             if ($style->list_style_image !== "none") {
                 $decorator = "ListBulletImage";
             } else {
                 $decorator = "ListBullet";
             }
             $reflower = "ListBullet";
             break;
         case "-dompdf-image":
             $positioner = "Inline";
             $decorator = "Image";
             $reflower = "Image";
             break;
         case "-dompdf-br":
             $positioner = "Inline";
             $decorator = "Inline";
             $reflower = "Inline";
             break;
         default:
             // FIXME: should throw some sort of warning or something?
         // FIXME: should throw some sort of warning or something?
         case "none":
             if ($style->_dompdf_keep !== "yes") {
                 // Remove the node and the frame
                 $frame->get_parent()->remove_child($frame);
                 return;
             }
             $positioner = "Null";
             $decorator = "Null";
             $reflower = "Null";
             break;
     }
     // Handle CSS position
     $position = $style->position;
     if ($position === "absolute") {
         $positioner = "Absolute";
     } else {
         if ($position === "fixed") {
             $positioner = "Fixed";
         }
     }
     $node = $frame->get_node();
     // Handle nodeName
     if ($node->nodeName === "img") {
         $style->display = "-dompdf-image";
         $decorator = "Image";
         $reflower = "Image";
     }
     $positioner = "Dompdf\\Positioner\\{$positioner}";
     $decorator = "Dompdf\\FrameDecorator\\{$decorator}";
     $reflower = "Dompdf\\FrameReflower\\{$reflower}";
     /** @var AbstractFrameDecorator $deco */
     $deco = new $decorator($frame, $dompdf);
     $deco->set_positioner(new $positioner($deco));
     $deco->set_reflower(new $reflower($deco, $dompdf->getFontMetrics()));
     if ($root) {
         $deco->set_root($root);
     }
     if ($display === "list-item") {
         // Insert a list-bullet frame
         $xml = $dompdf->get_dom();
         $bullet_node = $xml->createElement("bullet");
         // arbitrary choice
         $b_f = new Frame($bullet_node);
         $node = $frame->get_node();
         $parent_node = $node->parentNode;
         if ($parent_node) {
             if (!$parent_node->hasAttribute("dompdf-children-count")) {
                 $xpath = new DOMXPath($xml);
                 $count = $xpath->query("li", $parent_node)->length;
                 $parent_node->setAttribute("dompdf-children-count", $count);
             }
             if (is_numeric($node->getAttribute("value"))) {
                 $index = intval($node->getAttribute("value"));
             } else {
                 if (!$parent_node->hasAttribute("dompdf-counter")) {
                     $index = $parent_node->hasAttribute("start") ? $parent_node->getAttribute("start") : 1;
                 } else {
                     $index = $parent_node->getAttribute("dompdf-counter") + 1;
                 }
             }
             $parent_node->setAttribute("dompdf-counter", $index);
             $bullet_node->setAttribute("dompdf-counter", $index);
         }
         $new_style = $dompdf->get_css()->create_style();
         $new_style->display = "-dompdf-list-bullet";
         $new_style->inherit($style);
         $b_f->set_style($new_style);
         $deco->prepend_child(Factory::decorate_frame($b_f, $dompdf, $root));
     }
     return $deco;
 }
Exemple #2
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();
 }
 /**
  * Create a deep copy: copy this node and all children
  *
  * @return Frame
  */
 function deep_copy()
 {
     $node = $this->_frame->get_node();
     if ($node instanceof DOMElement && $node->hasAttribute("id")) {
         $node->setAttribute("data-dompdf-original-id", $node->getAttribute("id"));
         $node->removeAttribute("id");
     }
     $frame = new Frame($node->cloneNode());
     $frame->set_style(clone $this->_frame->get_original_style());
     $deco = Factory::decorate_frame($frame, $this->_dompdf, $this->_root);
     foreach ($this->get_children() as $child) {
         $deco->append_child($child->deep_copy());
     }
     return $deco;
 }
Exemple #4
0
 /**
  * Restructure tree so that the table has the correct structure.
  * Invalid children (i.e. all non-table-rows) are moved below the
  * table.
  */
 public function normalise()
 {
     // Store frames generated by invalid tags and move them outside the table
     $erroneous_frames = array();
     $anon_row = false;
     $iter = $this->get_first_child();
     while ($iter) {
         $child = $iter;
         $iter = $iter->get_next_sibling();
         $display = $child->get_style()->display;
         if ($anon_row) {
             if ($display === "table-row") {
                 // Add the previous anonymous row
                 $this->insert_child_before($table_row, $child);
                 $table_row->normalise();
                 $child->normalise();
                 $anon_row = false;
                 continue;
             }
             // add the child to the anonymous row
             $table_row->append_child($child);
             continue;
         } else {
             if ($display === "table-row") {
                 $child->normalise();
                 continue;
             }
             if ($display === "table-cell") {
                 // Create an anonymous table row
                 $tr = $this->get_node()->ownerDocument->createElement("tr");
                 $frame = new Frame($tr);
                 $css = $this->get_style()->get_stylesheet();
                 $style = $css->create_style();
                 $style->inherit($this->get_style());
                 // Lookup styles for tr tags.  If the user wants styles to work
                 // better, they should make the tr explicit... I'm not going to
                 // try to guess what they intended.
                 if ($tr_style = $css->lookup("tr")) {
                     $style->merge($tr_style);
                 }
                 // Okay, I have absolutely no idea why I need this clone here, but
                 // if it's omitted, php (as of 2004-07-28) segfaults.
                 $frame->set_style(clone $style);
                 $table_row = Factory::decorate_frame($frame, $this->_dompdf, $this->_root);
                 // Add the cell to the row
                 $table_row->append_child($child);
                 $anon_row = true;
                 continue;
             }
             if (!in_array($display, self::$VALID_CHILDREN)) {
                 $erroneous_frames[] = $child;
                 continue;
             }
             // Normalise other table parts (i.e. row groups)
             foreach ($child->get_children() as $grandchild) {
                 if ($grandchild->get_style()->display === "table-row") {
                     $grandchild->normalise();
                 }
             }
             // Add headers and footers
             if ($display === "table-header-group") {
                 $this->_headers[] = $child;
             } elseif ($display === "table-footer-group") {
                 $this->_footers[] = $child;
             }
         }
     }
     if ($anon_row && $table_row instanceof DOMNode) {
         // Add the row to the table
         $this->_frame->append_child($table_row);
         $table_row->normalise();
         $this->_cellmap->add_row();
     }
     foreach ($erroneous_frames as $frame) {
         $this->move_after($frame);
     }
 }
 /**
  * Create a deep copy: copy this node and all children
  *
  * @return Frame
  */
 function deep_copy()
 {
     $frame = new Frame($this->get_node()->cloneNode());
     $frame->set_style(clone $this->_frame->get_original_style());
     $deco = Factory::decorate_frame($frame, $this->_dompdf, $this->_root);
     foreach ($this->get_children() as $child) {
         $deco->append_child($child->deep_copy());
     }
     return $deco;
 }
 /**
  * Sets the generated content of a generated frame
  */
 protected function _set_content()
 {
     $frame = $this->_frame;
     $style = $frame->get_style();
     // if the element was pushed to a new page use the saved counter value, otherwise use the CSS reset value
     if ($style->counter_reset && ($reset = $style->counter_reset) !== "none") {
         $vars = preg_split('/\\s+/', trim($reset), 2);
         $frame->reset_counter($vars[0], isset($frame->_counters['__' . $vars[0]]) ? $frame->_counters['__' . $vars[0]] : (isset($vars[1]) ? $vars[1] : 0));
     }
     if ($style->counter_increment && ($increment = $style->counter_increment) !== "none") {
         $frame->increment_counters($increment);
     }
     if ($style->content && !$frame->get_first_child() && $frame->get_node()->nodeName === "dompdf_generated") {
         $content = $this->_parse_content();
         // add generated content to the font subset
         // FIXME: This is currently too late because the font subset has already been generated.
         //        See notes in issue #750.
         if ($frame->get_dompdf()->get_option("enable_font_subsetting") && $frame->get_dompdf()->get_canvas() instanceof CPDF) {
             $frame->get_dompdf()->get_canvas()->register_string_subset($style->font_family, $content);
         }
         $node = $frame->get_node()->ownerDocument->createTextNode($content);
         $new_style = $style->get_stylesheet()->create_style();
         $new_style->inherit($style);
         $new_frame = new Frame($node);
         $new_frame->set_style($new_style);
         Factory::decorate_frame($new_frame, $frame->get_dompdf(), $frame->get_root());
         $frame->append_child($new_frame);
     }
 }