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

Using fonts

Zend_Pdf_Page::drawText() uses the page's current font and font size, which is set with the Zend_Pdf_Page::setFont() method:

<?php
/**
 * Set current font.
 *
 * @param Zend_Pdf_Resource_Font $font
 * @param float $fontSize
 * @return Zend_Pdf_Page
 */
public function setFont(Zend_Pdf_Resource_Font $font$fontSize);

PDF documents support PostScript Type 1 and TrueType fonts, as well as two specialized PDF types, Type 3 and composite fonts. There are also 14 standard Type 1 fonts built-in to every PDF viewer: Courier (4 styles), Helvetica (4 styles), Times (4 styles), Symbol, and Zapf Dingbats.

Zend_Pdf currently supports the standard 14 PDF fonts as well as your own custom TrueType fonts. Font objects are obtained via one of two factory methods: Zend_Pdf_Font::fontWithName($fontName) for the standard 14 PDF fonts or Zend_Pdf_Font::fontWithPath($filePath) for custom fonts.

Example 667. Create a standard font

<?php
...
// Create new font
$font Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);

// Apply font
$pdfPage->setFont($font36);
...

Constants for the standard 14 PDF font names are defined in the Zend_Pdf_Font class:

  • Zend_Pdf_Font::FONT_COURIER

  • Zend_Pdf_Font::FONT_COURIER_BOLD

  • Zend_Pdf_Font::FONT_COURIER_ITALIC

  • Zend_Pdf_Font::FONT_COURIER_BOLD_ITALIC

  • Zend_Pdf_Font::FONT_TIMES

  • Zend_Pdf_Font::FONT_TIMES_BOLD

  • Zend_Pdf_Font::FONT_TIMES_ITALIC

  • Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC

  • Zend_Pdf_Font::FONT_HELVETICA

  • Zend_Pdf_Font::FONT_HELVETICA_BOLD

  • Zend_Pdf_Font::FONT_HELVETICA_ITALIC

  • Zend_Pdf_Font::FONT_HELVETICA_BOLD_ITALIC

  • Zend_Pdf_Font::FONT_SYMBOL

  • Zend_Pdf_Font::FONT_ZAPFDINGBATS

You can also use any individual TrueType font (which usually has a '.ttf' extension) or an OpenType font ('.otf' extension) if it contains TrueType outlines. Currently unsupported, but planned for a future release are Mac OS X .dfont files and Microsoft TrueType Collection ('.ttc' extension) files.

To use a TrueType font, you must provide the full file path to the font program. If the font cannot be read for some reason, or if it is not a TrueType font, the factory method will throw an exception:

Example 668. Create a TrueType font

<?php
...
// Create new font
$goodDogCoolFont Zend_Pdf_Font::fontWithPath('/path/to/GOODDC__.TTF');

// Apply font
$pdfPage->setFont($goodDogCoolFont36);
...

By default, custom fonts will be embedded in the resulting PDF document. This allows recipients to view the page as intended, even if they don't have the proper fonts installed on their system. If you are concerned about file size, you can request that the font program not be embedded by passing a 'do not embed' option to the factory method:

Example 669. Create a TrueType font, but do not embed it in the PDF document

<?php
...
// Create new font
$goodDogCoolFont Zend_Pdf_Font::fontWithPath('/path/to/GOODDC__.TTF',
                                               
Zend_Pdf_Font::EMBED_DONT_EMBED);

// Apply font
$pdfPage->setFont($goodDogCoolFont36);
...

If the font program is not embedded but the recipient of the PDF file has the font installed on their system, they will see the document as intended. If they do not have the correct font installed, the PDF viewer application will do its best to synthesize a replacement.

Some fonts have very specific licensing rules which prevent them from being embedded in PDF documents. So you are not caught off-guard by this, if you try to use a font that cannot be embedded, the factory method will throw an exception.

You can still use these fonts, but you must either pass the do not embed flag as described above, or you can simply suppress the exception:

Example 670. Do not throw an exception for fonts that cannot be embedded

<?php
...
$font Zend_Pdf_Font::fontWithPath(
           
'/path/to/unEmbeddableFont.ttf',
           
Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION
        
);
...

This suppression technique is preferred if you allow an end-user to choose their own fonts. Fonts which can be embedded in the PDF document will be; those that cannot, won't.

Font programs can be rather large, some reaching into the tens of megabytes. By default, all embedded fonts are compressed using the Flate compression scheme, resulting in a space savings of 50% on average. If, for some reason, you do not want to compress the font program, you can disable it with an option:

Example 671. Do not compress an embedded font

<?php
...
$font Zend_Pdf_Font::fontWithPath('/path/to/someReallyBigFont.ttf',
                                    
Zend_Pdf_Font::EMBED_DONT_COMPRESS);
...

Finally, when necessary, you can combine the embedding options by using the bitwise OR operator:

Example 672. Combining font embedding options

<?php
...
$font Zend_Pdf_Font::fontWithPath(
            
$someUserSelectedFontPath,
            (
Zend_Pdf_Font::EMBED_SUPPRESS_EMBED_EXCEPTION |
            
Zend_Pdf_Font::EMBED_DONT_COMPRESS));
...

Zend Framework