TCPDF UTF-8 symbols not showing up

TCPDF UTF-8 symbols not showing up

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

TCPDF UTF-8 Symbols Not Showing Up

When generating PDFs with TCPDF, encountering replacement characters like ? or `` for specific Lithuanian glyphs (čęėįšųūž) is a classic encoding mismatch issue. This happens because the default fonts bundled with TCPDF—specifically Helvetica and Times—are not fully Unicode-compliant. They lack glyph definitions for many extended Latin character sets, including the Lithuanian characters you need.

To render these symbols correctly, you must switch to a TrueType font that supports the full Unicode range or use a CIDFont approach. The default "default times" font mentioned in your setup refers to an internal mapping of Times New Roman, but TCPDF does not include the necessary glyph data for non-ASCII characters in the standard TrueType versions it ships by default.

Enabling UTF-8 with a Unicode-Supporting Font

The most straightforward solution is to explicitly define a font that supports UTF-8 and assign it as the default before adding any text. TCPDF allows you to add external TrueType fonts (.ttf) or use OpenType fonts if they are available.

You can register a standard system font like Arial Unicode MS, DejaVu Sans, or Roboto by passing the addTTFfont method with the uni2 encoding. This encoding tells TCPDF to map UTF-8 code points directly to the font's internal character set.

Here is how you configure it in your PHP script:

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

// Set default font (must be a TrueType font with uni2 encoding)
$fontDir = __DIR__ . '/fonts/'; // Adjust path to where your .ttf files live
$pdf->AddFont('DejaVuSans', '', $fontDir . 'dejavusans.ttf'); 
$pdf->SetFont('DejaVuSans', '', 12);

// Now you can print Lithuanian text without replacement characters
$pdf->Cell(0, 10, 'Lithuanian text: čęėįšųūž', 1, 1, 'C');

If you are using TCPDF version 5.9 or higher, the AddFont method supports adding fonts with UTF-8 support directly from TTF files found in your project directory. Ensure that the font file actually contains the glyphs for Lithuanian characters; DejaVu Sans is a popular choice because it is open-source and covers a wide range of European languages out of the box.

Understanding Font Encodings

The core issue lies in how TCPDF maps characters to byte values. The default encoding used by the bundled fonts is typically cp1252 (Western European) or iso-8859-1. These legacy encodings do not have slots for Lithuanian characters beyond a certain point, or they map them incorrectly.

When you switch to a font with uni2 encoding, TCPDF expects the input string to be in UTF-8 and looks up the corresponding glyph using Unicode code points. For example, the character `` has the Unicode code point U+0105. With the correct font loaded, TCPDF will find the glyph for this code point and render it.

The CIDFont Alternative

If you cannot use external TrueType fonts due to licensing or distribution constraints, TCPDF provides a way to generate PDFs using CIDFonts (Character ID-based Fonts), which are designed for Asian languages but can handle any Unicode set if configured correctly. However, this requires generating a specific font file from a base 14-point font using the TCPDF_FONTS directory utilities or a third-party tool like ttf2cid.

For most web applications, simply adding a TTF font with the uni2 encoding is the preferred path. It avoids complex font mapping tables and leverages standard system fonts that developers are already familiar with.

Verifying the Result

After generating the PDF, open it in any viewer (Preview on macOS or Adobe Reader) to ensure the characters render correctly. If you still see question marks, verify that:

  1. The font file path is correct and readable by the web server.
  2. The SetFont method is called before any text output methods like Cell, WriteHTML, or Output.
  3. The input string being passed to the PDF generation is actually UTF-8 encoded in your PHP source code (use utf8_encode only if you are passing raw bytes, but modern PHP strings are typically UTF-8 by default).

By explicitly defining a Unicode-capable font and setting its encoding to uni2, you resolve the character gap between the legacy default fonts and the extended Latin requirements of Lithuanian text.

Our network