Adding new fonts and encoding support

This tutorial explains how to use TrueType fonts so that you are not limited to the standard fonts any more. The other interest is that you can choose the font encoding, which allows you to use other languages than the Western ones (the standard fonts having too few available characters).

There are two ways to use a new font: embedding it in the PDF or not. When a font is not embedded, it is sought in the system. The advantage is that the PDF file is lighter; on the other hand, if it is not available, a substitution font is used. So it is preferable to ensure that the needed font is installed on the client systems. If the file is to be viewed by a large audience, it is better to embed.

Adding a new font requires three steps:

Generation of the metric file

The first step consists in generating the AFM file. A free utility exists to do this task: ttf2pt1. The Windows binary is available here. The command line to use is the following:

ttf2pt1 -a font.ttf font

For example, for Comic Sans MS Regular:

ttf2pt1 -a c:\windows\fonts\comic.ttf comic

Two files are created; the one we are interested in is comic.afm.

Generation of the font definition file

The second step consists in generating a PHP file containing all the information needed by FPDF; in addition, the font file is compressed. To do this, a helper script is provided in the font/makefont/ directory of the package: makefont.php. It contains the following function:

MakeFont(string fontfile, string afmfile [, string enc [, array patch]])

fontfile
Path to the .ttf file.
afmfile
Path to the .afm file.
enc
Name of the encoding to use. Default value: cp1252.
patch
Optional modification of the encoding. Empty by default.

If you don't want to embed the font, pass an empty string as first parameter.
Note: in the case of a font with the same name as a standard one, for instance arial.ttf, it is mandatory to embed. If you don't, Acrobat will use its own font.

The AFM file is the one previously generated.

The encoding defines the association between a code (from 0 to 255) and a character. The first 128 are fixed and correspond to ASCII; the following are variable. The encodings are stored in .map files. Those available are: Of course, the font must contain the characters corresponding to the chosen encoding.
In the particular case of a symbolic font (that is to say which does not contain letters, such as Symbol or ZapfDingbats), pass an empty string.
The encodings which begin with cp are those used by Windows; Linux systems usually use ISO.
Remark: the standard fonts use cp1252.

The last parameter gives the possibility to alter the encoding. Sometimes you may want to add some characters. For instance, ISO-8859-1 does not contain the euro symbol. To add it at position 164, pass array(164=>'Euro').

After you have called the function (create a new file for this and include makefont.php, or simply add the call directly inside), a .php file is created, with the same name as the .afm one. You may rename it if you wish. If the case of embedding, the font file is compressed and gives a second file with .z as extension (except if the compression function is not available, it requires Zlib). You may rename it too, but in this case you have to alter the variable $file in the .php file accordingly.

Example:

MakeFont('c:\\windows\\fonts\\comic.ttf','comic.afm','cp1252');

which gives the files comic.php and comic.z.

Then you have to copy the generated file(s) either in the directory of the script which will use the font, or in the directory given by FPDF_FONTPATH if the constant is defined. If the font file could not be compressed, copy the .ttf instead of the .z.

Declaration of the font in the script

The last step is the most simple. You just need to call the AddFont() method. For instance:

$pdf->AddFont('Comic','','comic.php');

or simply:

$pdf->AddFont('Comic');

And the font is now available (in regular and underlined styles), usable like the others. If we had worked with Comic Sans MS Bold (comicbd.ttf), we would have put:

$pdf->AddFont('Comic','B','comicbd.php');

Exemple

Let's now see a small complete example. The font used is Calligrapher, available at www.abstractfonts.com (a site offering numerous free TrueType fonts). The first step is the generation of the AFM file:

ttf2pt1 -a calligra.ttf calligra

which gives calligra.afm (and calligra.t1a that we can delete). Then we generate the definition file:

<?php
require('../font/makefont/makefont.php');

MakeFont('calligra.ttf','calligra.afm');
?>

The function call gives the following report:

Warning: character Euro is missing
Warning: character Zcaron is missing
Warning: character zcaron is missing
Warning: character eth is missing
Font file compressed (calligra.z)
Font definition file generated (calligra.php)

The euro character is not present in the font (it is too old). Three other characters are missing too, but we are not interested in them anyway.
We can now copy the two files in the font directory, or put them in the same directory as our script:

<?php
require('fpdf.php');

$pdf=new FPDF();
$pdf->Open();
$pdf->AddPage();
$pdf->AddFont('Calligrapher','','calligra.php');
$pdf->SetFont('Calligrapher','',35);
$pdf->Cell(0,10,'Enjoy new fonts with FPDF!');
$pdf->Output();
?>

Note: a bug affects the Acrobat 5 plug-in under IE and the new font does not appear immediately (a substitution font is used). You have to cause a window refresh (by resizing, scrolling or switching) to make it show up.

About the euro symbol

The euro character is not present in all encodings, and is not always placed at the same position:

cp1250128
cp1251136
cp1252128
cp1253128
ISO-8859-1absent 
ISO-8859-2absent 
ISO-8859-5absent 
ISO-8859-7absent 
ISO-8859-15 164
ISO-8859-16 164
KOI8-Rabsent 

ISO-8859-1 is widespread but does not include the euro sign. If you need it, the simplest thing to do is using cp1252 or ISO-8859-15 instead, which are nearly identical but contain the precious symbol.
As for ISO-8859-2, it is possible to use ISO-8859-16 instead, but it is recent and shows many differences. It is therefore simpler to patch the encoding to add the symbol to it, as explained above. The same is true for ISO-8859-5, ISO-8859-7 and KOI-R.

Font synthesis under Windows

When a font is not available in a given style, Windows is able to synthesize it from the regular version. For instance, there is no Comic Sans MS Italic, but it can be built from Comic Sans MS Regular. This feature can be used in a PDF file, but unfortunately requires that the regular font be present in the system (just embed it does not work). Here is how to do it: For instance, for the file comici.php:

$name='ComicSansMS,Italic';

It can then be used normally:

$pdf->AddFont('Comic','I','comici.php');