Adding new fonts and encoding support

This tutorial explains how to use TrueType or Type1 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 searched 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 recommended to embed.

Adding a new font requires three steps for TrueTypes: For Type1, the first one is theoretically not necessary because the AFM file is usually shipped with the font. In case you have only a metric file in PFM format, use the convertor available here.

Generation of the metric file

The first step for a TrueType consists in generating the AFM file. A 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 [, string type]]])
fontfile

Path to the .ttf or .pfb 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.

type

Type of the font (TrueType or Type1). Default value: TrueType.

The first parameter is the name of the font file. The extension must be either .ttf or .pfb and determines the font type. If you own a Type1 font in ASCII format (.pfa), you can convert it to binary format with t1utils.
If you don't want to embed the font, pass an empty string. In this case, type is given by the type parameter.
Note: in the case of a font with the same name as a standard one, for instance arial.ttf, it is recommended to embed. If you don't, some versions of Acrobat will use their own f