What is TCPDF?
TCPDF is one of the most widely used open-source PHP libraries for generating PDF documents server-side. It supports UTF-8 text, images, barcodes, HTML rendering, and — crucially — custom fonts in TrueType format.
Unlike FPDF, TCPDF includes built-in support for multibyte character sets, making it the go-to choice for PDFs that need to handle Arabic, Chinese, Cyrillic, or other non-Latin scripts.
TCPDF ships with a set of core fonts, but for any real-world project you will almost certainly need to embed a custom typeface — whether it's a brand font, a system font not included by default, or a Unicode-capable font for multilingual content.
Font types in TCPDF
TCPDF supports three font types. Understanding the difference is important before you start:
| Type | Description | When to use |
|---|---|---|
| Core | 14 built-in PDF fonts (Helvetica, Times, Courier…). No embedding needed. | Basic documents, no special characters |
| TrueType | Converted from a .ttf file. Embedded in the PDF. |
Custom fonts, brand typography |
| TrueType Unicode | Like TrueType but with full UTF-8 support and subsetting. | Multilingual content, special characters |
For most modern projects, you want TrueType Unicode — use the -b flag when converting (this is what our tool does by default).
Step-by-step: convert and use a TTF font
.ttf file at fonts.palettize.me. The converter runs tcpdf_addfont with the -b (Unicode) flag and returns a ZIP with all the files TCPDF needs.tcpdf/fonts/ but it depends on your installation.$pdf->setFont() before adding any text.setFont() must exactly match the filename of the .php definition file inside tcpdf/fonts/ — without the extension.What does the converter generate?
For a font named MyFont.ttf, the converter generates:
tcpdf/fonts/ ├── myfont.php # Font definition (metrics, encoding) ├── myfont.ctg.z # Character-to-glyph map (compressed) └── myfont.z # Font data (compressed)
All three files must be present in the same folder. The font name to use in setFont() is myfont (lowercase, stripped of special characters).
Using setFont() correctly
The setFont() method signature in TCPDF is:
$pdf->setFont($family, $style = '', $size = 10, $fontfile = '', $subset = 'default', $out = true);
A typical usage looks like this:
// Load TCPDF require_once('tcpdf/tcpdf.php'); $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false); $pdf->addPage(); // Set the custom font $pdf->setFont('myfont', '', 12); // Write text $pdf->Cell(0, 10, 'Hello, custom font!', 0, 1); $pdf->Output('document.pdf', 'I');
'UTF-8' as the fifth argument to the TCPDF constructor. Without it, Unicode characters will not render correctly even with a Unicode font.Using a custom font path
If your fonts are not in the default tcpdf/fonts/ directory, define the path before calling setFont():
// Define a custom fonts directory define('K_PATH_FONTS', __DIR__ . '/my_fonts/'); require_once('tcpdf/tcpdf.php'); $pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8'); $pdf->addPage(); $pdf->setFont('myfont', '', 12);
K_PATH_FONTS constant must be defined before including tcpdf.php, otherwise it will be ignored.Bold, italic and font variants
TCPDF handles font variants differently depending on whether you converted them separately or whether you are using the $style parameter of setFont().
Option A: convert each variant as a separate font
This is the most reliable approach. Convert MyFont-Bold.ttf, MyFont-Italic.ttf, and MyFont-Regular.ttf separately. The converter will name them myfontb, myfonti, and myfont respectively.
// Regular $pdf->setFont('myfont', '', 12); $pdf->Cell(0, 10, 'Regular text', 0, 1); // Bold (separate converted file) $pdf->setFont('myfontb', '', 12); $pdf->Cell(0, 10, 'Bold text', 0, 1); // Italic (separate converted file) $pdf->setFont('myfonti', '', 12); $pdf->Cell(0, 10, 'Italic text', 0, 1);
Option B: use the style parameter
You can also pass 'B', 'I', or 'BI' as the second argument to setFont(). TCPDF will look for a font file with the corresponding suffix. This only works if you have converted and named the variants accordingly.
$pdf->setFont('myfont', 'B', 12); // looks for myfontb.php $pdf->setFont('myfont', 'I', 12); // looks for myfonti.php $pdf->setFont('myfont', 'BI', 12); // looks for myfontbi.php
Unicode and multilingual fonts
TCPDF has excellent Unicode support, but you need to make sure both the font and the document are configured correctly.
Requirements for Unicode rendering
- Use a font that actually contains the required glyphs (e.g. Noto Sans for broad language coverage)
- Convert with the
-bflag (Unicode mode) — our tool does this automatically - Pass
'UTF-8'to the TCPDF constructor - Make sure your PHP strings are UTF-8 encoded
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false); $pdf->addPage(); $pdf->setFont('notosans', '', 12); // Arabic (RTL) $pdf->setRTL(true); $pdf->Cell(0, 10, 'مرحبا بالعالم', 0, 1); // Back to LTR for other languages $pdf->setRTL(false); $pdf->Cell(0, 10, '日本語テキスト', 0, 1);
$pdf->setRTL(true) before writing the text, and setRTL(false) to switch back.Recommended Unicode fonts
- Noto Sans / Noto Serif — broad language coverage, open source (Google)
- DejaVu Sans — good coverage, widely used in open-source projects
- FreeSans / FreeMono — GNU free fonts, solid Unicode support
Common errors and fixes
| Error | Cause | Fix |
|---|---|---|
| Font not found | Font definition file .php not in the fonts folder, or wrong font name. |
Check that myfont.php exists in K_PATH_FONTS. Font name must exactly match the filename (without .php). |
| Empty or blank PDF | Font files present but incomplete. Missing .z or .ctg.z. |
Re-convert the TTF. Make sure all three generated files are copied to the fonts folder. |
| Characters render as boxes | Font does not contain the required glyphs, or the font was not converted in Unicode mode. | Use a Unicode-capable font (e.g. Noto Sans). Reconvert with -b flag. |
| Garbled / wrong characters | String encoding mismatch. PHP file saved as Latin-1 but TCPDF expects UTF-8. | Make sure your PHP source file is saved as UTF-8 and that you pass 'UTF-8' to the TCPDF constructor. |
| K_PATH_FONTS not found | K_PATH_FONTS constant defined after require tcpdf.php. |
Always define the constant before including TCPDF. |
| setFont() triggers warning | Trying to set font before calling addPage(). |
Always call $pdf->addPage() before setFont(). |
Ready to convert your font?
Upload your .ttf file and get TCPDF-ready font files packaged in a ZIP, in seconds.
Frequently asked questions
.ttf file using tcpdf_addfont (or our online tool), place the generated files in your /tcpdf/fonts/ folder, and call $pdf->setFont('fontname', '', 12) in your PHP code..php, .z, .ctg.z) are present..ttf file from Google Fonts (or use the Google Fonts download page), convert it with our tool, and use it like any other TrueType font. Most Google Fonts are licensed under OFL, which permits embedding in PDF documents.-b flag).MyFont-Bold.ttf, MyFont-Italic.ttf). The converter will automatically strip the style suffix from the filename. You can then either call setFont('myfontb') directly, or use the style parameter: setFont('myfont', 'B').K_PATH_FONTS constant with the absolute path to your fonts folder, before including tcpdf.php: define('K_PATH_FONTS', __DIR__ . '/fonts/');