How to embed fonts with FPDF and mPDF

Carson picture Carson · Aug 20, 2010 · Viewed 11.5k times · Source

I'm trying to embed Helvetica Neue into my PDF, but all of the resources I find online are overly complicated and filled with what I think is unneeded information.

Can anyone point me to a step by step tutorial of how to include fonts in FPDF?

Thanks in advance.

Answer

Adam Prax picture Adam Prax · Mar 31, 2011

It's not too hard.

Suppose we want to add the Rockford font.

First, we need to use ttf2pt1 (or some equivalent program) to generate the AFM file for Rockford. Run the following command in a shell.

ttf2pt1 Rockford

The command will create Rockford.afm in the current shell directory.

Change your shell to the the makefont directory in the fpdf installation directory

cd /<...>/fpdf/font/makefont

Execute an interactive php shell

php -a

Run the following commands in the interactive PHP shell.

php > require("makefont.php");
php > MakeFont("/<font location>/Rockford.ttf", "/<font location>/Rockford.afm");
php > exit

You should see two new files, Rockford.php and Rockford.z in the current directory. Copy these two files to the 'fonts' directory in the fpdf installation directory.

cp Rockford.z Rockford.php /<...>/fpdf/font/

At this point, the installation of the new font into FPDF is done.

To use your font when generating a PDF, you must first import the font as follows.

$fpdf->AddFont('Rockford', '', 'Rockford.php');

You can then use the font when needed in your script. For example, to set the font to Rockford size 8, you would write the following statement.

$fpdf->SetFont('Rockford', '', 8);