dompdf character encoding UTF-8

dompdf character encoding UTF-8

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

Stefan Bogdanescu

Founder & Senior Architect

dompdf character encoding UTF-8

Rendering text with special characters in PDFs generated by dompdf is a classic pain point for developers working on invoicing or reporting systems. You might see perfect rendering in your browser, only to hit a wall of replacement characters (like ?) when the same content hits the PDF engine. The issue isn't usually that you lack Unicode support; it's almost always about how the data is fed into the library versus what the library actually expects at specific stages of the rendering pipeline.

The source material provided illustrates this perfectly. The user observes:

UTF-8 --> UTF-8 : X Ponuka číslo € černý Češký 

rendering correctly in HTML, but becoming:

UTF-8 --> UTF-8 : X Ponuka ?íslo € ?erný ?ešký 

in the resulting PDF.

The culprit is rarely the meta tag inside the generated string or the iconv calls used to test compatibility matrices. The real issue lies in how dompdf interprets its input and what fonts are actually being loaded.

Understanding the Input Stream

In the provided code, the HTML string is explicitly constructed with <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>. While this is good practice for web browsers, dompdf operates differently. When you instantiate a DOMPDF object and call $dompdf->load_html($html, 'UTF-8'), the second argument tells the library that your input string is already encoded in UTF-8.

However, if the font files loaded by the PDF engine do not contain glyphs for the specific characters (like č, ř, or ý), the renderer cannot draw them. Instead of throwing an error, it defaults to a replacement character (?). This is a silent failure common in many rendering engines.

The test code using iconv attempts to force conversions between various legacy encodings (Windows-1250, ISO-8859-2) and UTF-8. While this might help if the source data were actually stored in a legacy encoding, it does not fix the issue if the source is already valid UTF-8 but the font cannot display it.

The Font Configuration Mismatch

The most common reason for seeing ? characters in a PDF generated by dompdf is that the default font (helvetica) being used does not support the full range of Central European characters required by your specific text.

Helvetica, while ubiquitous on web pages due to its fallback behavior, often lacks diacritics or non-English letters in its standard glyph set when embedded as a system font in PDFs. When dompdf loads a document, it attempts to map the input characters to the available fonts. If a character like č is requested but the current font (Helvetica) doesn't have that glyph, dompdf replaces it with ?.

To resolve this, you must ensure your document uses a font that actually contains the necessary glyphs and that you explicitly load it.

Implementing a Proper Solution

Instead of relying on the default font stack or trying to force legacy encodings via iconv, the robust solution is to load a Unicode-compatible font and configure dompdf to use it globally. Fonts like DejaVu Sans, FreeSans, or any commercial font with full Unicode coverage will handle characters like č, ř, ý, and € without issue.

Here is how you should structure your initialization:

require_once 'vendor/autoload.php'; // Or dompdf_config.inc.php depending on version

use Dompdf\Dompdf;

// Create a new instance
$dompdf = new Dompdf();

// Load the HTML string (ensure it is valid UTF-8)
$html_content = '<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<p>X Ponuka číslo € černý Češký</p>
</body>
</html>';

$dompdf->loadHtml($html_content);

// This is the critical step: Load a font that supports your characters
$fontDir = __DIR__ . '/fonts'; // Path to your local fonts directory
$dompdf->addFont('DejaVuSans.ttf', 'normal', $fontDir . '/DejaVuSans.ttf');

// Apply the loaded font globally so all elements use it
$dompdf->set_default_font_size(10);
// Optionally set specific styles in CSS to ensure the font is applied
// style = 'font-family: DejaVu Sans;'

// Render and save
$dompdf->render();
file_put_contents('output.pdf', $dompdf->output());

Key Considerations

  1. Font Availability: The path to the font file must be correct relative to where dompdf runs. If you use a framework like Laravel, ensure the font files are copied into the deployment directory and not left in vendor/, which might get wiped during updates.
  2. Encoding Consistency: Ensure your PHP source file and the HTML content string are both saved and treated as UTF-8. Mixing UTF-8 strings with legacy encodings without explicit conversion will lead to the exact corruption seen in the example (?íslo instead of číslo).
  3. CSS vs. Code: While you can specify fonts in CSS, it is safer to set the default font in the PHP configuration or ensure your <style> block explicitly targets all elements (like body, p, div) to use the new font. This prevents inheritance from a system font that might lack the glyphs.

When to Use External Resources

If you are building a SaaS application where font management is complex, consider using a service like https://laravelcompany.com if they provide managed PDF generation services with pre-configured Unicode-safe fonts. However, for standard dompdf usage, loading the font locally remains the most reliable and performant approach.

The goal is to ensure that every character in your source HTML has a corresponding glyph in the loaded font map. Once that link is established, the replacement characters will disappear, and your PDF output will match the browser preview.

Our network