Dompdf prints Cyrillic (or other Unicode) text as question marks or empty boxes
Cyrillic text in the PDF appears as ??? or as empty boxesShort 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.
Fix it fast
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());2Match what you see, then jump to its fix
- 1
Empty boxes where the letters should beThe font used in the PDF has no Cyrillic glyphsFix - 2
Strange multi-character sequences instead of letters, not boxesThe HTML does not declare UTF-8, or the file is saved in another encodingFix - 3
Your font-family names a font, but the output uses the fallback or shows boxesA custom font was never installed in DompdfFix
- 1
What does the error look like?
Cyrillic text in the PDF appears as ??? or as empty boxesAccented or non-Latin letters are replaced by squaresText 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/ | head2Fix
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.phpConfirmed by github.com
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.html2Fix
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.phpConfirmed by github.com
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" | head2Fix
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.phpConfirmed by github.com
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix