Pop\Pdf\Pdf::addFont PHP Method

addFont() public method

Method to add a font to the PDF.
public addFont ( string $font, boolean $embedOverride = false ) : Pdf
$font string
$embedOverride boolean
return Pdf
    public function addFont($font, $embedOverride = false)
    {
        // Embed the font file.
        if (file_exists($font)) {
            $fontIndex = count($this->objects[$this->objects[$this->pages[$this->curPage]]->index]->fonts) == 0 ? 1 : count($this->objects[$this->objects[$this->pages[$this->curPage]]->index]->fonts) + 1;
            $objectIndex = $this->lastIndex($this->objects) + 1;
            $fontParser = new Parser\Font($font, $fontIndex, $objectIndex, $this->compress);
            if (!$fontParser->isEmbeddable() && !$embedOverride) {
                throw new Exception('Error: The font license does not allow for it to be embedded.');
            }
            $this->objects[$this->objects[$this->pages[$this->curPage]]->index]->fonts[$fontParser->getFontName()] = $fontParser->getFontRef();
            $fontObjects = $fontParser->getObjects();
            foreach ($fontObjects as $key => $value) {
                $this->objects[$key] = $value;
            }
            $this->lastFontName = $fontParser->getFontName();
            // Else, use a standard font.
        } else {
            // Check to make sure the font is a standard PDF font.
            if (!array_key_exists($font, $this->standardFonts)) {
                throw new Exception('Error: That font is not contained within the standard PDF fonts.');
            }
            // Set the font index.
            $ft_index = count($this->objects[$this->objects[$this->pages[$this->curPage]]->index]->fonts) == 0 ? 1 : count($this->objects[$this->objects[$this->pages[$this->curPage]]->index]->fonts) + 1;
            // Set the font name and the next object index.
            $f = 'MF' . $ft_index;
            $i = $this->lastIndex($this->objects) + 1;
            // Add the font to the current page's fonts and add the font to _objects array.
            $this->objects[$this->objects[$this->pages[$this->curPage]]->index]->fonts[$font] = "/{$f} {$i} 0 R";
            $this->objects[$i] = new Object\Object("{$i} 0 obj\n<<\n    /Type /Font\n    /Subtype /Type1\n    /Name /{$f}\n    /BaseFont /{$font}\n    /Encoding /WinAnsiEncoding\n>>\nendobj\n\n");
            $this->lastFontName = $font;
        }
        if (!in_array($this->lastFontName, $this->fonts)) {
            $this->fonts[] = $this->lastFontName;
        }
        return $this;
    }

Usage Example

Exemplo n.º 1
0
<?php

require_once '../../bootstrap.php';
use Pop\Color\Space\Rgb;
use Pop\Pdf\Pdf;
try {
    $pdf = new Pdf('../tmp/doc.pdf');
    $pdf->addPage('Letter');
    $pdf->setVersion('1.4')->setTitle('Test Title')->setAuthor('Test Author')->setSubject('Test Subject')->setCreateDate(date('D, M j, Y h:i A'));
    $pdf->setCompression(true);
    $pdf->setTextParams(6, 6, 100, 100, 30, 0)->setFillColor(new Rgb(12, 101, 215))->setStrokeColor(new Rgb(215, 101, 12));
    $pdf->addFont('Arial');
    $pdf->addText(50, 620, 18, 'Hello World! & You!', 'Arial');
    $pdf->setTextParams();
    $pdf->addFont('Courier-Bold');
    $pdf->addText(150, 350, 48, 'Hello World!', 'Courier-Bold');
    $sz = $pdf->getStringSize('Hello World!', 'Courier-Bold', 48);
    $pdf->addUrl(150, 350 - $sz['baseline'], $sz['width'], $sz['height'], 'http://www.google.com/');
    $pdf->addPage('Letter');
    $pdf->setFillColor(new Rgb(12, 101, 215))->setStrokeColor(new Rgb(215, 101, 12))->setStrokeWidth(4, 10, 5);
    $pdf->drawCircle(150, 700, 60, false);
    $pdf->setPage(1)->setFillColor(new Rgb(0, 0, 255));
    $pdf->drawRectangle(100, 550, 175, 50);
    $pdf->addLink(100, 550, 175, 50, 150, 550, 1, 2);
    $pdf->setPage(2)->setFillColor(new Rgb(12, 101, 215))->setStrokeColor(new Rgb(215, 101, 12))->setStrokeWidth(4, 10, 5);
    $pdf->drawCircle(250, 650, 25);
    $pdf->addImage('../assets/images/logo-rgb.jpg', 150, 400);
    $pdf->setPage(1)->setFillColor(new Rgb(255, 10, 25))->setStrokeColor(new Rgb(12, 101, 215))->setStrokeWidth(2);
    $pdf->drawEllipse(300, 150, 200, 100, false);
    $pdf->addPage('Legal');
    $pdf->addFont('Courier-Bold');
All Usage Examples Of Pop\Pdf\Pdf::addFont