dompdf character encoding UTF-8

2019-01-12 05:52发布

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?

10条回答
趁早两清
2楼-- · 2019-01-12 06:26

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:

$body = 'test 简化字 彝語/彝语 test číslo € černý Češký';

$cjk_scripts = 'Bopomofo|Han|Hiragana|Katakana';
$cjk_scripts = preg_replace('/[a-zA-Z_]+/', '\\p{$0}', $cjk_scripts);

// wrap the CJK characters into a span with it's own font
$body = preg_replace("/($cjk_scripts)+/isu", '<span class="cjk">$0</span>', $body);

// a font that supports CJK characters
$cjk_font_path = APP_PATH.'/fonts/DroidSansFallbackFull.ttf';

$html = <<<HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style type="text/css">
@font-face {
    font-family: 'DroidSansFallbackFull';
    font-style: normal;
    font-weight: 400;
    src: url('$cjk_font_path') format('truetype');
}
body {
    font-family: DejaVu Sans, sans-serif;;
}
.cjk {
    font-family: DroidSansFallbackFull, sans-serif;
}
</style>
</head>
<body>$body</body>
</html>
HTML;

$dompdf = new \DOMPDF();
$dompdf->set_paper('A4');
$dompdf->load_html($html);
$dompdf->render();

$dompdf->stream('test.pdf', ['Attachment'=>0]);

Related: https://github.com/dompdf/dompdf/issues/1508

查看更多
来,给爷笑一个
3楼-- · 2019-01-12 06:28

utf8_decode() did the trick for me with some German translations like ä and ü.

echo utf8_decode('X Ponuka číslo € černý Češký <br>');
查看更多
我想做一个坏孩纸
4楼-- · 2019-01-12 06:33

Only Add

  <style>
    *{ font-family: DejaVu Sans !important;}
  </style>

before </head> It is working for me.

查看更多
倾城 Initia
5楼-- · 2019-01-12 06:39

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...

@import url('https://fonts.googleapis.com/css?family=Roboto:400,700&subset=latin-ext');
body {font-family: 'Roboto', sans-serif;}
查看更多
登录 后发表回答