問題描述
我正在嘗試使用正確的字符創(chuàng)建 pdf,但是有?"字符.我創(chuàng)建了一個測試 php 文件,我試圖在其中找到最佳解決方案.如果我在瀏覽器中打開 html,我看起來沒問題
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 € ?erny ?e?ky
但是當(dāng)我查看pdf時,我看到了這個
But when I look into the pdf I see this
UTF-8 --> UTF-8 : X Ponuka ?íslo € ?erny ?e?ky
這是我的所有代碼:
<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 € ?erny ?e?ky <br>');
}
}
$chain .= '<p style="font-family: firefly, verdana, sans-serif;">??????X Ponuka ?íslo € ?erny ?e?ky <br></p></body></html>';
echo $chain;
echo 'X Ponuka ?íslo € ?erny ?e?ky <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>
我做錯了什么?我嘗試了很多我發(fā)現(xiàn)的選項:(知道嗎?
What Im doing wrong? I tried many many options which I found :( Any idea?
推薦答案
您應(yīng)該閱讀Unicode How-to再次.主要問題是您沒有指定支持您的字符的字體.看起來您已經(jīng)閱讀了操作方法,因為您使用的是該文檔中的字體示例.然而,這個例子并不意味著全局適用于任何文檔,dompdf 默認(rèn)不包含 firefly(一種漢字字體)或 Verdana.
You should read over the Unicode How-to again. The main problem is that you don't specify a font that supports your characters. It looks like you've read the how-to, because you're using the font example from that document. However the example was not meant to apply globally to any document, dompdf doesn't include firefly (a Chinese character font) or Verdana by default.
如果您不指定字體,則 dompdf 會退回到僅支持 Windows ANSI 編碼的核心字體之一(Helvetica、Times Roman、Courier).因此,請務(wù)必使用支持 Unicode 編碼并具有您需要顯示的字符的字體來設(shè)置文本樣式.
If you do not specify a font then dompdf falls back to one of the core fonts (Helvetica, Times Roman, Courier) which only support Windows ANSI encoding. So always be sure to style your text with a font that supports Unicode encoding and has the characters you need to display.
在 dompdf 0.6.0 中,您可以使用包含的 Deja Vu 字體.所以以下應(yīng)該工作(只是 HTML):
With dompdf 0.6.0 you can use the included Deja Vu fonts. So the following should work (just the HTML):
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
body { font-family: DejaVu Sans, sans-serif; }
</style>
<title>? s ? ?</title>
</head>
<body>
<p>??????X Ponuka ?íslo € ?erny ?e?ky <br></p>
</body>
</html>
這篇關(guān)于dompdf 字符編碼 UTF-8的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!