Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Dompdf prints Cyrillic (or other Unicode) text as question marks or empty boxes

ErrorCyrillic text in the PDF appears as ??? or as empty boxes

Short answer

Dompdf draws each character with the font you give it and does not switch fonts for missing glyphs. If the font has no Cyrillic glyphs you get empty boxes. Use a Unicode TrueType font such as DejaVu Sans in your CSS, declare UTF-8 in the HTML head, and load custom fonts with load_font.php or @font-face.

  • Applies to dompdf 3, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Make a PDF from one line of Cyrillic text with an explicit Unicode font and a UTF-8 charset. If that renders, the problem is the font or charset in your real template, not Dompdf itself.

    <?php
    use Dompdf\Dompdf;
    $html = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head>'
          . '<body style="font-family: DejaVu Sans, sans-serif">Привет, мир</body></html>';
    $dompdf = new Dompdf();
    $dompdf->loadHtml($html);
    $dompdf->render();
    file_put_contents('test.pdf', $dompdf->output());
  2. 2Match what you see, then jump to its fix

    1. 1Empty boxes where the letters should beThe font used in the PDF has no Cyrillic glyphsFix
    2. 2Strange multi-character sequences instead of letters, not boxesThe HTML does not declare UTF-8, or the file is saved in another encodingFix
    3. 3Your font-family names a font, but the output uses the fallback or shows boxesA custom font was never installed in DompdfFix

What does the error look like?

  • Cyrillic text in the PDF appears as ??? or as empty boxes
  • Accented or non-Latin letters are replaced by squares
  • Text looks like garbled characters after loadHtml()

Cause 1 of 3

The font used in the PDF has no Cyrillic glyphs

1Check

Dompdf does not replace a font for characters it lacks, so each character must exist in the font that styles that text. Look at the font-family in your CSS and at any default font set in the options.

grep -rn "font-family" templates/ | head

2Fix

Put a font with Cyrillic coverage first in font-family, with a fallback after it. The DejaVu fonts that ship with Dompdf cover a wide range of characters; check that yours includes the letters you need.

<p style="font-family: DejaVu Sans, sans-serif;">Привет</p>

3Verify

Render the one-line test again and open the PDF.

php test.php

Confirmed by github.com

Did this fix it?

Cause 2 of 3

The HTML does not declare UTF-8, or the file is saved in another encoding

1Check

If you pass no encoding to loadHtml(), Dompdf tries to detect one itself. A template saved in one encoding but declared as another garbles the text.

file -I templates/invoice.html

2Fix

Declare UTF-8 in the head of the document and save the file as UTF-8. You can also pass the encoding as the second argument to loadHtml().

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

// PHP
$dompdf->loadHtml($html, 'UTF-8');

3Verify

Regenerate the PDF and check the letters.

php test.php

Confirmed by github.com

Did this fix it?

Cause 3 of 3

A custom font was never installed in Dompdf

1Check

Only TrueType (.ttf) files work, and the font must be registered with Dompdf, either once from the command line or at run time with @font-face.

find vendor/dompdf -name "*.ttf" | head

2Fix

Register the font once with the load_font.php script, run from the command line and not through the web server, or declare it with @font-face in the HTML.

php load_font.php MyFont /path/to/myfont.ttf

@font-face {
  font-family: 'MyFont';
  font-style: normal;
  font-weight: normal;
  src: url(/path/to/myfont.ttf) format('truetype');
}

3Verify

Use the font name in font-family and render the test PDF again.

php test.php

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Keep one Unicode font as the default for every template and test it with Cyrillic text before release.
  • Save all templates as UTF-8 and always declare the charset in the head.
  • Register custom fonts in your deploy step, so the server has the same fonts as your laptop.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.