Converter Guide Font Preview new
🎨 Palettize
✦ TCPDF · Complete guide

How to Use Custom Fonts in TCPDF

A step-by-step guide to converting any TrueType font and embedding it in your PHP-generated PDFs — including Unicode, bold/italic variants, and common fixes.

📄 ~8 min read 🛠 PHP · TCPDF 🔤 Unicode supported

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

1
Convert your TTF with our online tool
Upload your .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.
2
Extract the ZIP into your fonts folder
Unzip and copy all generated files into your TCPDF fonts directory. The default path is tcpdf/fonts/ but it depends on your installation.
3
Call setFont() in your PHP code
Use the font name returned by the converter (shown after conversion). Pass it to $pdf->setFont() before adding any text.
4
Set iconv or output encoding if needed
For Unicode fonts, make sure your strings are UTF-8 encoded. TCPDF handles the rest automatically when using TrueType Unicode fonts.
💡
The font name used in 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:

Output files
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:

PHP
$pdf->setFont($family, $style = '', $size = 10, $fontfile = '', $subset = 'default', $out = true);

A typical usage looks like this:

PHP — basic usage
// 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');
Make sure you pass '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():

PHP — custom font path
// 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);
💡
The 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.

PHP — separate variant fonts
// 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.

PHP — style parameter
$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

PHP — Unicode example
$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);
💡
For Arabic and Hebrew (right-to-left scripts) remember to call $pdf->setRTL(true) before writing the text, and setRTL(false) to switch back.

Recommended Unicode fonts

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.

⚡ Convert a font now

Frequently asked questions

How do I add a custom font to TCPDF?
Convert your .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.
Why is my TCPDF font not rendering correctly?
The most common causes are: the font name does not match the generated filename, one or more font files are missing from the fonts folder, or you are using a non-Unicode font with multibyte characters. Check all three files (.php, .z, .ctg.z) are present.
Can I use Google Fonts with TCPDF?
Yes. Download the .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.
What is the difference between TrueType and TrueType Unicode in TCPDF?
TrueType Unicode fonts support the full UTF-8 character set including multibyte characters (Arabic, Chinese, Cyrillic, etc.). Standard TrueType is limited to single-byte encodings. Unless you only need basic Latin characters, always use Unicode mode (-b flag).
How do I use bold and italic variants of a custom font?
Convert each variant separately (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').
How do I set a custom font directory in TCPDF?
Define the K_PATH_FONTS constant with the absolute path to your fonts folder, before including tcpdf.php: define('K_PATH_FONTS', __DIR__ . '/fonts/');
Are converted fonts safe to use commercially?
The conversion process itself does not change the font's license. Whether you can embed a font in a PDF depends on the font's license — check for the "embedding" permission. Fonts under OFL, Apache 2.0, or similar open licenses are generally safe. Always verify the original font license before distributing PDFs.
🎨
Palettize
Extract colour palettes from any image
Try it →