/** * 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(); }
public function testSetters() { $option = new Options(); $option->set(array('tempDir' => 'test1', 'fontDir' => 'test2', 'fontCache' => 'test3', 'chroot' => 'test4', 'logOutputFile' => 'test5', 'defaultMediaType' => 'test6', 'defaultPaperSize' => 'test7', 'defaultFont' => 'test8', 'dpi' => 300, 'fontHeightRatio' => 1.2, 'isPhpEnabled' => true, 'isRemoteEnabled' => true, 'isJavascriptEnabled' => false, 'isHtml5ParserEnabled' => true, 'isFontSubsettingEnabled' => true, 'debugPng' => true, 'debugKeepTemp' => true, 'debugCss' => true, 'debugLayout' => true, 'debugLayoutLines' => false, 'debugLayoutBlocks' => false, 'debugLayoutInline' => false, 'debugLayoutPaddingBox' => false, 'adminUsername' => 'test9', 'adminPassword' => 'test10')); $this->assertEquals('test1', $option->getTempDir()); $this->assertEquals('test2', $option->getFontDir()); $this->assertEquals('test3', $option->getFontCache()); $this->assertEquals('test4', $option->getChroot()); $this->assertEquals('test5', $option->getLogOutputFile()); $this->assertEquals('test6', $option->getDefaultMediaType()); $this->assertEquals('test7', $option->getDefaultPaperSize()); $this->assertEquals('test8', $option->getDefaultFont()); $this->assertEquals(300, $option->getDpi()); $this->assertEquals(1.2, $option->getFontHeightRatio()); $this->assertTrue($option->getIsPhpEnabled()); $this->assertTrue($option->getIsRemoteEnabled()); $this->assertFalse($option->getIsJavascriptEnabled()); $this->assertTrue($option->getIsHtml5ParserEnabled()); $this->assertTrue($option->getIsFontSubsettingEnabled()); $this->assertTrue($option->getDebugPng()); $this->assertTrue($option->getDebugKeepTemp()); $this->assertTrue($option->getDebugCss()); $this->assertTrue($option->getDebugLayout()); $this->assertFalse($option->getDebugLayoutLines()); $this->assertFalse($option->getDebugLayoutBlocks()); $this->assertFalse($option->getDebugLayoutInline()); $this->assertFalse($option->getDebugLayoutPaddingBox()); $this->assertEquals('test9', $option->getAdminUsername()); $this->assertEquals('test10', $option->getAdminPassword()); }