How to Add Custom TTF Font to TCPDF
TCPDF is one of a popular library, which helps to get the PDF designed from the php code. This is helps to prepare mainly the invoices, receipts, etc. But most of the time, you might need to have custom font to make it look good with it. Here are some steps to make the Custom TTF font to work in TCPDF.
- Upload the TTF font file to the TCPDF/fonts directory on your server.
- Generate a font definition file for your custom TTF font using the TCPDF font generator tool. This file should be placed in the TCPDF/fonts directory and should have the same name as the font file.
- Set the font path and font name in your TCPDF code using the setFontPath() and setFont() methods. The font path should be set to the TCPDF/fonts directory, and the font name should be the name of the font definition file without the .php extension.
Here is an example of how to add a custom TTF font to TCPDF:
// Set font path
// Add custom TTF font
$pdf->addTTFfont('/path/to/tcpdf/fonts/customfont.ttf', 'TrueTypeUnicode', '', 32);
// Set font
$pdf->SetFont('customfont', '', 14);
In this example, ‘customfont.ttf’ is the name of the TTF font file. The addTTFfont() method is used to add the font to TCPDF. The first parameter is the path to the font file, the second parameter is the encoding (in this case, TrueTypeUnicode), the third parameter is the subset (in this case, an empty string), and the fourth parameter is the em size (in this case, 32).
Make sure that the font name matches the name of the font definition file in the TCPDF/fonts directory.
That’s it! You should now be able to use your custom TTF font in TCPDF.