How to implement custom fonts in TCPDF

How to implement custom fonts in TCPDF

Not every company uses the same font for everything. Usually, there are fonts for every single thing that requires characters in something e.g Logos, Slogans, Flyers, Visit Cards and Invoices. TCPDF is widely used to generate PDF on the fly from HTML in PHP due to its simplicity. One of the most awesome features of TCPDF is that you can embed almost any kind of Font (TrueType format), however not every developer knows how to do it easily. In this article, we want to show you how easily is to add a new custom font to TCPDF.

Important before proceed

You can import only TTF or OTF fonts into TCPDF, which means that you will work with the UTF-8 encoding, so be sure that your PDF is instantiated with unicode format set to true and UTF-8:

<?php

$unicode = true;
$format = "UTF-8";

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, "letter", $unicode, $format, false);

Otherwise, when you generate the PDF you will see weird characters instead of that what you need. Having said that, let's proceed !

1. Download the source files of the Font that you want

To make this example so pretty easy to understand and show how easy is to add a custom font to TCPDF, we are going to use a pretty weird custom font namely Just Beautiful Simplicity. It's worth to say, that maybe no one wants this kind of font on their PDFs:

Font TCPDF Custom Font

However as mentioned, this is just as a demo. You are free to include the kind of Font that you want as Roboto, Arial etc. From the source files of the font, you will need the file with the TTF extension as these are easy to find on every font that you can find. The source files of a font can be easily found if you search on google for it, you will usually find zip files with a couple of files inside that represents the font for different font types.

Note

Is important to verify before using a font, that it supports special characters as well (TrueType unicode).

2. Generating font files used by TCPDF

TCPDF can't use directly the font files to embed TrueType fonts (.TTF) files, but instead, you need to extract the font metrics and build the required tables using the provided utility on the TCPDF library. There are 2 options to generate the 3 required files from the font for TCPDF, you can either use a free online service that works for any TCPDF version or using the command line tool available on newer versions of TCPDF:

A. Using a free online service

For any version of TCPDF, you can rely on a free online service to generate the required files for that version. If you check the fonts folder of the TCPDF library, you will see 3 types of files:

  • Font file compressed ([fontname].z)
  • CIDToGIDMap created and compressed ([fontname].ctg.z)
  • Font definition file generated ([fontname].php)

For example, for our font you would need 3 files:

  • justbeatifulsimplicity.ctg.z
  • justbeatifulsimplicity.z
  • justbeatifulsimplicity.php

You can use this online tool to generate the mentioned files from a TTF file. After uploading the file and downloading the files that service generates, store them in the tcpdf/fonts folder. Following any of the mentioned steps above and with any version, you will find some new files in the tcpdf/fonts folder. They represent the font in the formats that TCPDF needs. Now you can simply type the name of the font in the SetFont method of the PDF and you are ready.

B. Using the command line tool in newer versions of TCPDF

With the newer versions of TCPDF, there are 2 ways to achieve your goal:

B.A Using the Command Line Utility

Inside the tcpdf/tools folder you will find the tcpdf_addfont.php file. This can be used in the command line in the following way:

php tcpdf_addfont.php i- [your-font.ttf]

The command will automatically create the required files inside the tcpdf/fonts folder of your project and displays the name of the font that can be used in your code. For example, with other font (Alice And the Wicked Monster) the command line shows the following output:

Command Line Font Generation

B.B Manualy writing a script

If you are not willing to create the files using the command line, you can use manually the class that the command line uses. The TCPDF_FONTS class offers the static method addTTFfont to create the files. You can use the following code, store it into a PHP file (build-fonts.php) and then run it with php build-fonts.php. Note that you need to replace the font paths:

<?php 

// Add more files if the font offers for
// Bold styles, Oblique etc.
$pathTTFFiles = array(
    'justbeautifulsimplicity.ttf'
);

foreach($pathTTFFiles as $ttfFile){
    $fontname = TCPDF_FONTS::addTTFfont($ttfFile, 'TrueTypeUnicode', '', 96);
    
    echo "The processed font '$ttfFile' can be used with the name: $fontname";
}

Note

As you usually don't use a single format of the font but with more presentations like Bold, Oblique etc. The pathTTFFiles is an array that contains the path to all the fonts that you want to import.

Example

To generate our PDF with the previous font, we would just need only to set the name of the generated font in the SetFont method of the PDF:

<?php 

// Create new PDF
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// Set Font, not the TTF file but the php filename inside tcpdf/fonts
$pdf->SetFont("justbeautifulsimplicity", '', 10);

With some HTML printed, after generating the PDF with the previous code you would have a PDF with the custom font:

TCPDF Custom Font Example

Happy coding !

This could interest you

Become a more social person