Im trying to create pdf with correct characters, but there are "?" chars. I created a test php file, where Im trying to fing the best solution. If Im open in the browser the html I looks like ok
UTF-8 --> UTF-8 : X Ponuka číslo € černý Češký
But when I look into the pdf I see this
UTF-8 --> UTF-8 : X Ponuka ?íslo € ?erný ?ešký
Here is my all code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>č s š Š</title>
</head>
<body>
<?php
require_once("dompdf/dompdf_config.inc.php");
$tab = array("UTF-8", "ASCII", "Windows-1250", "ISO-8859-2", "ISO-8859-1", "ISO-8859-6", "CP1256");
$chain = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <style></style><title>č s š Š</title></head><body>';
foreach ($tab as $i)
{
foreach ($tab as $j)
{
$chain .= "<br> $i --> $j : ".iconv($i, $j, 'X Ponuka číslo € černý Češký <br>');
}
}
$chain .= '<p style="font-family: firefly, verdana, sans-serif;">??????X Ponuka číslo € černý Češký <br></p></body></html>';
echo $chain;
echo 'X Ponuka číslo € černý Češký <br>';
$filename = 'pdf/_1.pdf';
$dompdf = new DOMPDF();
$dompdf->load_html($chain, 'UTF-8');
$dompdf->set_paper('a4', 'portrait'); // change these if you need to
$dompdf->render();
file_put_contents($filename, $dompdf->output());
?>
</body>
</html>
What Im doing wrong? I tried many many options which I found :( Any idea?
Dompdf does not support fallback fonts, so you can't use your favorite font if it does not support your characters, and you also can't set another font to be the fallback font for those characters like
droid sans fallback
.What you can do instead is take advantage of regex unicode script ranges: https://www.regular-expressions.info/unicode.html to wrap those blocks of text into spans and give them the fallback font.
Example:
Related: https://github.com/dompdf/dompdf/issues/1508
utf8_decode() did the trick for me with some German translations like ä and ü.
Only Add
before
</head>
It is working for me.I had the same problem and I solved it very simple. Just import google fonts with required language subset in your CSS file which is used when generating HTML. Specify utf-8 in your HTML file and it's working...