/** * Installs a new font family * This function maps a font-family name to a font. It tries to locate the * bold, italic, and bold italic versions of the font as well. Once the * files are located, ttf versions of the font are copied to the fonts * directory. Changes to the font lookup table are saved to the cache. * * @param string $fontname the font-family name * @param string $normal the filename of the normal face font subtype * @param string $bold the filename of the bold face font subtype * @param string $italic the filename of the italic face font subtype * @param string $bold_italic the filename of the bold italic face font subtype * * @throws Exception */ function install_font_family($fontname, $normal, $bold = null, $italic = null, $bold_italic = null) { $fontMetrics = new FontMetrics(CanvasFactory::get_instance(new DOMPDF()), new Options()); // Check if the base filename is readable if (!is_readable($normal)) { throw new Exception("Unable to read '{$normal}'."); } $dir = dirname($normal); $basename = basename($normal); $last_dot = strrpos($basename, '.'); if ($last_dot !== false) { $file = substr($basename, 0, $last_dot); $ext = strtolower(substr($basename, $last_dot)); } else { $file = $basename; $ext = ''; } if (!in_array($ext, array(".ttf", ".otf"))) { throw new Exception("Unable to process fonts of type '{$ext}'."); } // Try $file_Bold.$ext etc. $path = "{$dir}/{$file}"; $patterns = array("bold" => array("_Bold", "b", "B", "bd", "BD"), "italic" => array("_Italic", "i", "I"), "bold_italic" => array("_Bold_Italic", "bi", "BI", "ib", "IB")); foreach ($patterns as $type => $_patterns) { if (!isset(${$type}) || !is_readable(${$type})) { foreach ($_patterns as $_pattern) { if (is_readable("{$path}{$_pattern}{$ext}")) { ${$type} = "{$path}{$_pattern}{$ext}"; break; } } if (is_null(${$type})) { echo "Unable to find {$type} face file.\n"; } } } $fonts = compact("normal", "bold", "italic", "bold_italic"); $entry = array(); // Copy the files to the font directory. foreach ($fonts as $var => $src) { if (is_null($src)) { $entry[$var] = DOMPDF_FONT_DIR . mb_substr(basename($normal), 0, -4); continue; } // Verify that the fonts exist and are readable if (!is_readable($src)) { throw new Exception("Requested font '{$src}' is not readable"); } $dest = DOMPDF_FONT_DIR . basename($src); if (!is_writeable(dirname($dest))) { throw new Exception("Unable to write to destination '{$dest}'."); } echo "Copying {$src} to {$dest}...\n"; if (!copy($src, $dest)) { throw new Exception("Unable to copy '{$src}' to '{$dest}'"); } $entry_name = mb_substr($dest, 0, -4); echo "Generating Adobe Font Metrics for {$entry_name}...\n"; $font_obj = Font::load($dest); $font_obj->saveAdobeFontMetrics("{$entry_name}.ufm"); $entry[$var] = $entry_name; } // Store the fonts in the lookup table $fontMetrics->setFontFamily($fontname, $entry); // Save the changes $fontMetrics->saveFontFamilies(); }
/** * 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(); }