tFPDF::AddFont PHP Метод

AddFont() публичный Метод

public AddFont ( $family, $style = '', $file = '', $uni = false )
    function AddFont($family, $style = '', $file = '', $uni = false)
    {
        // Add a TrueType, OpenType or Type1 font
        $family = strtolower($family);
        $style = strtoupper($style);
        if ($style == 'IB') {
            $style = 'BI';
        }
        if ($file == '') {
            if ($uni) {
                $file = str_replace(' ', '', $family) . strtolower($style) . '.ttf';
            } else {
                $file = str_replace(' ', '', $family) . strtolower($style) . '.php';
            }
        }
        $fontkey = $family . $style;
        if (isset($this->fonts[$fontkey])) {
            return;
        }
        if ($uni) {
            if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS . $file)) {
                $ttffilename = _SYSTEM_TTFONTS . $file;
            } else {
                $ttffilename = $this->_getfontpath() . 'unifont/' . $file;
            }
            $unifilename = $this->_getfontpath() . 'unifont/' . strtolower(substr($file, 0, strpos($file, '.')));
            $name = '';
            $originalsize = 0;
            $ttfstat = stat($ttffilename);
            if (file_exists($unifilename . '.mtx.php')) {
                include $unifilename . '.mtx.php';
            }
            if (!isset($type) || !isset($name) || $originalsize != $ttfstat['size']) {
                $ttffile = $ttffilename;
                require_once $this->_getfontpath() . 'unifont/ttfonts.php';
                $ttf = new TTFontFile();
                $ttf->getMetrics($ttffile);
                $cw = $ttf->charWidths;
                $name = preg_replace('/[ ()]/', '', $ttf->fullName);
                $desc = array('Ascent' => round($ttf->ascent), 'Descent' => round($ttf->descent), 'CapHeight' => round($ttf->capHeight), 'Flags' => $ttf->flags, 'FontBBox' => '[' . round($ttf->bbox[0]) . " " . round($ttf->bbox[1]) . " " . round($ttf->bbox[2]) . " " . round($ttf->bbox[3]) . ']', 'ItalicAngle' => $ttf->italicAngle, 'StemV' => round($ttf->stemV), 'MissingWidth' => round($ttf->defaultWidth));
                $up = round($ttf->underlinePosition);
                $ut = round($ttf->underlineThickness);
                $originalsize = $ttfstat['size'] + 0;
                $type = 'TTF';
                // Generate metrics .php file
                $s = '<?php' . "\n";
                $s .= '$name=\'' . $name . "';\n";
                $s .= '$type=\'' . $type . "';\n";
                $s .= '$desc=' . var_export($desc, true) . ";\n";
                $s .= '$up=' . $up . ";\n";
                $s .= '$ut=' . $ut . ";\n";
                $s .= '$ttffile=\'' . $ttffile . "';\n";
                $s .= '$originalsize=' . $originalsize . ";\n";
                $s .= '$fontkey=\'' . $fontkey . "';\n";
                $s .= "?>";
                if (is_writable(dirname($this->_getfontpath() . 'unifont/' . 'x'))) {
                    $fh = fopen($unifilename . '.mtx.php', "w");
                    fwrite($fh, $s, strlen($s));
                    fclose($fh);
                    $fh = fopen($unifilename . '.cw.dat', "wb");
                    fwrite($fh, $cw, strlen($cw));
                    fclose($fh);
                    @unlink($unifilename . '.cw127.php');
                }
                unset($ttf);
            } else {
                $cw = @file_get_contents($unifilename . '.cw.dat');
            }
            $i = count($this->fonts) + 1;
            if (!empty($this->AliasNbPages)) {
                $sbarr = range(0, 57);
            } else {
                $sbarr = range(0, 32);
            }
            $this->fonts[$fontkey] = array('i' => $i, 'type' => $type, 'name' => $name, 'desc' => $desc, 'up' => $up, 'ut' => $ut, 'cw' => $cw, 'ttffile' => $ttffile, 'fontkey' => $fontkey, 'subset' => $sbarr, 'unifilename' => $unifilename);
            $this->FontFiles[$fontkey] = array('length1' => $originalsize, 'type' => "TTF", 'ttffile' => $ttffile);
            $this->FontFiles[$file] = array('type' => "TTF");
            unset($cw);
        } else {
            $info = $this->_loadfont($file);
            $info['i'] = count($this->fonts) + 1;
            if (!empty($info['diff'])) {
                // Search existing encodings
                $n = array_search($info['diff'], $this->diffs);
                if (!$n) {
                    $n = count($this->diffs) + 1;
                    $this->diffs[$n] = $info['diff'];
                }
                $info['diffn'] = $n;
            }
            if (!empty($info['file'])) {
                // Embedded font
                if ($info['type'] == 'TrueType') {
                    $this->FontFiles[$info['file']] = array('length1' => $info['originalsize']);
                } else {
                    $this->FontFiles[$info['file']] = array('length1' => $info['size1'], 'length2' => $info['size2']);
                }
            }
            $this->fonts[$fontkey] = $info;
        }
    }

Usage Example

Пример #1
0
 /**
  * Creates content that should be printed
  *
  * @param Collection $collection
  * @return string
  */
 public function renderCollectionContent($collection)
 {
     $pdf = new \tFPDF('P', 'mm', [72, 80]);
     $pdf->SetMargins(0, 2);
     // Запретим автоматический разрыв страниц
     $pdf->SetAutoPageBreak(false, 0);
     $pdf->AddPage();
     $pdf->AddFont('arial', '', 'arial.ttf', true);
     $pdf->AddFont('arialbd', '', 'arialbd.ttf', true);
     $this->makeLine($pdf, Settings::get()->terminalName(), 10, 5)->makeLine($pdf, date('H:i:s d.m.Y', time()), 10, 5)->makeLine($pdf, $collection->sum . ' руб.', 16, 15, true)->makeLine($pdf, 'Инкассация:', 10, 10)->makeLine($pdf, (string) $collection->id, 12, 5)->makeLine($pdf, Settings::get()->contactInfo(), 8, 15)->makeLine($pdf, 'Сохраняйте чек', 8, 4);
     $fileName = __DIR__ . '/../runtime/pdf/collection_ticket.pdf';
     $pdf->Output($fileName, 'F');
     return $fileName;
 }
All Usage Examples Of tFPDF::AddFont