How do you add custom fonts in TCPDF?

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

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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. 🙂

Solution 2

Got this answer in another question and resolved for me. You just need to use the first parameter, the path of the font file. Worked with TTF and OTF fonts.

It generates a name string to use with $pdf->SetFont($fontname, '', $font_size);

Hope it helps.


The latest TCPDF version automatically convert fonts into TCPDF format using the addTTFfont() method. For example:

// convert TTF font to TCPDF format and store it on the fonts folder
$fontname = TCPDF_FONTS::addTTFfont('/path-to-font/FreeSerifItalic.ttf', 'TrueTypeUnicode', '', 96);

// use the font
$pdf->SetFont($fontname, '', 14, '', false);

For further information and examples, please check the TCPDF Fonts documentation page.

NOTE: Once the font has been converted, TCPDF no longer requires the TTF file or the above call to addTTFfont()!

Solution 3

You can convert ttf fonts with http://fonts.palettize.me and then put uncompressed result in fonts folder into tcpdf class. Then you can add it with $pdf->SetFont(‘rumpelstiltskinwebfont’, ”, 14); using the name of the file

Solution 4

Since TCPDF (version 6.2.6) you can use TCPDF_FONTS::addTTFfont directly instead of AddFont.

TCPDF makes three files out of the ttf and puts them into the directory "font" of TCPDF. Once these have been created, they can be used for all future PDFs.

Example:
First (for simplicity) I created a folder in the font folder of TCPDF with the name of the new font, in my example "arialuni" and copied my ttf of this font into it.

If I want to create my font files in a single separate file that does not use Composer autoload, I have to include TCPDF once and also specify the correct path to my ttf.

require_once('vendor/tecnickcom/tcpdf/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$fontname = TCPDF_FONTS::addTTFfont('vendor/tecnickcom/tcpdf/fonts/arialuni/arialuni.ttf', 'TrueTypeUnicode', '', 32);

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply