How to fix FPDF error: Undefined font

If you get this error, this could have one of the following causes

1. You forgot to set the font

Before you set the font, its necessary to add the font!

$pdf->AddFont('DejaVuSansCondensed', 'B', 'DejaVuSansCondensed-Bold.ttf', true);

$pdf->SetFont('DejaVuSansCondensed', 'B', 11);

2. You forgot to set the fontpath

Imagine you have specified some custom fonts then you need to set the path to the font folder beofre you add the font:

$pdf->fontpath  = '/fonts/tfpdf/font';

$pdf->AddFont('DejaVuSansCondensed', 'B', 'DejaVuSansCondensed-Bold.ttf', true);

Although the font "DejaVuSansCondensed-Bold.ttf" is in the folder "/fonts/tfpdf/font/unifont/" you still have to set "/fonts/tfpdf/font/" as the fontpath

3. You add the same font by different names

This issue is tricky and took me ages to find. Imagine, that you add somewhere in you code this:

$pdf->AddFont('DejaVuSansCondensed', 'B', 'DejaVuSansCondensed-Bold.ttf', true);

Once this is called, it will save the name in the file dejavusanscondensed-bold.mtx.php.

If you now use in an other method the same font but witha different name:

$pdf->AddFont('DejaVuSansCondensed-Bold', '', 'DejaVuSansCondensed-Bold.ttf', true);

The script will fail with an undefined font error. To fix this, make sure that your script is not using multiple names for the same font, and remove all .dat and .php files inside the /font/unifont/ folder (that clears the cache).

Leave a comment

Your email address will not be published. Required fields are marked *