PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Extracting fonts

Zend_Pdf module provides a possibility to extract fonts from loaded documents.

It may be useful for incremental document updates. Without this functionality you have to attach and possibly embed font into a document each time you want to update it.

Zend_Pdf and Zend_Pdf_Page objects provide special methods to extract all fonts mentioned within a document or a page:

Example 674. Extracting fonts from a loaded document

<?php
...
$pdf Zend_Pdf::load($documentPath);
...
// Get all document fonts
$fontList $pdf->extractFonts();
$pdf->pages[] = ($page $pdf->newPage(Zend_Pdf_Page::SIZE_A4));
$yPosition 700;
foreach (
$fontList as $font) {
    
$page->setFont($font15);
    
$fontName $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT,
                                   
'en',
                                   
'UTF-8');
    
$page->drawText($fontName ': The quick brown fox jumps over the lazy dog',
                    
100,
                    
$yPosition,
                    
'UTF-8');
    
$yPosition -= 30;
}
...
// Get fonts referenced within the first document page
$firstPage reset($pdf->pages);
$firstPageFonts $firstPage->extractFonts();
...

Example 675. Extracting font from a loaded document by specifying font name

<?php
...
$pdf = new Zend_Pdf();
...
$pdf->pages[] = ($page $pdf->newPage(Zend_Pdf_Page::SIZE_A4));

$font Zend_Pdf_Font::fontWithPath($fontPath);
$page->setFont($font$fontSize);
$page->drawText($text$x$y);
...
// This font name should be stored somewhere...
$fontName $font->getFontName(Zend_Pdf_Font::NAME_POSTSCRIPT,
                               
'en',
                               
'UTF-8');
...
$pdf->save($docPath);
...
<?php
...
$pdf Zend_Pdf::load($docPath);
...
$pdf->pages[] = ($page $pdf->newPage(Zend_Pdf_Page::SIZE_A4));

/* $srcPage->extractFont($fontName) can also be used here */
$font $pdf->extractFont($fontName);

$page->setFont($font$fontSize);
$page->drawText($text$x$y);
...
$pdf->save($docPathtrue /* incremental update mode */);
...

Extracted fonts can be used in the place of any other font with the following limitations:

  • Extracted font can be used only in the context of the document from which it was extracted.

  • Possibly embedded font program is actually not extracted. So extracted font can't provide correct font metrics and original font has to be used for text width calculations:

    <?php
    ...
    $font $pdf->extractFont($fontName);
    $originalFont Zend_Pdf_Font::fontWithPath($fontPath);

    $page->setFont($font /* use extracted font for drawing */$fontSize);
    $xPosition $x;
    for (
    $charIndex 0$charIndex strlen($text); $charIndex++) {
        
    $page->drawText($text[$charIndex], xPosition$y);

        
    // Use original font for text width calculation
        
    $width $originalFont->widthForGlyph(
                     
    $originalFont->glyphNumberForCharacter($text[$charIndex])
                 );
        
    $xPosition += $width/$originalFont->getUnitsPerEm()*$fontSize;
    }
    ...

Zend Framework