Adding a custom font to TCPDF

Adding a custom font to TCPDF

Last updated:

This is what you need to do when you try to use a font but it's not available; this is most likely the case when you get the following error message on TCPDF:

error message

(using, for example, arial font)

Old way

  • use TCPDF's addTTFfont and send as parameter a TTF (possibly another type too):

    $font = $this->addTTFfont("filename.ttf");
    
  • use the fontname when you need it...

    $this->SetFont($font,'',10);
    

New way

Since a few versions back, there's a new file in tools/tcpdf_addfont.php, which can be used for this very purpose. It's easier to use this tool rather than the old method (from a quick look at it, I think it's a wrapper script for calling addTTFfont behind the scenes).

Either way, from now on you just need to call it like this (replace /path/to/tcpdf with the location where you've installed this library and /path/to/fonts with the location where you keep your custom fonts)

For example, to add an Arial TTF source file:

$ /path/to/tcpdf/tools/tcpdf_addfont.php -i /path/to/fonts/arial.ttf

If everything works as expected, you'll see this:

>>> Converting fonts for TCPDF:
*** Output dir set to /path/to/tcpdf/fonts/
+++ OK   : /path/to/fonts/arial.ttf added as arial
>>> Process successfully completed!

To add Multiple Font Files:

$ /path/to/tcpdf/tools/tcpdf_addfont.php -i /path/to/fonts/arialb.ttf,/path/to/fonts/arialbi.ttf,/path/to/fonts/ariali.ttf

On success, you'll see this:

>>> Converting fonts for TCPDF:
*** Output dir set to /path/to/tcpdf/fonts/
+++ OK   : /path/to/fonts/arialb.ttf added as arialb
+++ OK   : /path/to/fonts/arialbi.ttf added as arialbi
+++ OK   : /path/to/fonts/ariali.ttf added as ariali
>>> Process successfully completed!

References

official TCPDF docs on fonts

Dialogue & Discussion