How do you add custom fonts in TCPDF?

Dev01 picture Dev01 · Apr 27, 2015 · Viewed 12.6k times · Source

I would like to add a custom font to a pdf I'm generating using TCPDF. I might be missing something but the docs seem to be out dated. They are referencing the addTTFfont() function but I think it's been deprecated and no longer exists in the latest version of TCPDF.

I read that I need to convert the ttf file and put it in the fonts folder so I ran:

php/tcpdf/tools/tcpdf_addfont.php -i php/font/rumpelstiltskin-webfont.ttf

and it generated these files which are now in the fonts folder:

rumpelstiltskinwebfont.ctg.z
rumpelstiltskinwebfont.z
rumpelstiltskinwebfont.php

Then I tried to add the font:

$pdf->addFont('rumpelstiltskin');
$pdf->SetFont('rumpelstiltskin', '', 14, '', false);

but I'm getting an error:

TCPDF ERROR: Could not include font definition file: rumpelstiltskin

Answer

Dev01 picture Dev01 · Apr 27, 2015

I figured out my issue, I was almost there.

Here is a step by step:

First convert your font using the tcpdf_addfont.php tool font in the TCPDF tools folder:

php/tcpdf/tools/tcpdf_addfont.php -i php/font/rumpelstiltskin-webfont.ttf

This will generate the required files and put them in the TCPDF fonts folder. Check the fonts folder and copy the name of the font, in my case it was rumpelstiltskinwebfont.

In your code set the font using the font file's name and write a line of text:

$pdf->SetFont('rumpelstiltskinwebfont', '', 14, '', false);
$pdf->Write(0, 'Fill text', '', 0, '', true, 0, false, false, 0);

That's it. Hope this helps someone. :)