php dompdf 字符编码 UTF-8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16384517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dompdf character encoding UTF-8
提问by lostika
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
我试图用正确的字符创建pdf,但有“?” 字符。我创建了一个测试 php 文件,我试图在其中找到最佳解决方案。如果我在浏览器中打开 html 我看起来没问题
UTF-8 --> UTF-8 : X Ponuka ?íslo ?erny ?e?ky
But when I look into the pdf I see this
但是当我查看pdf时,我看到了这个
UTF-8 --> UTF-8 : X Ponuka ?íslo ?erny ?e?ky
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 ?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>
What Im doing wrong? I tried many many options which I found :( Any idea?
我做错了什么?我尝试了很多我发现的选项:(知道吗?
回答by BrianS
You should read over the Unicode How-toagain. 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.
您应该再次阅读Unicode How-to。主要问题是您没有指定支持您的字符的字体。看起来您已经阅读了操作指南,因为您使用的是该文档中的字体示例。然而,该示例并不打算全局应用于任何文档,dompdf 默认不包含萤火虫(一种汉字字体)或 Verdana。
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 将退回到仅支持 Windows ANSI 编码的核心字体之一(Helvetica、Times Roman、Courier)。因此,请务必使用支持 Unicode 编码并具有您需要显示的字符的字体来设置文本样式。
With dompdf 0.6.0 you can use the included Deja Vu fonts. So the following should work (just the HTML):
使用 dompdf 0.6.0,您可以使用包含的 Deja Vu 字体。所以以下应该工作(只是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>
回答by Frantisek
I got UTF-8 characters working with this combination. Before you pass html to DOMpdf, make encoding covert with this:
我得到了使用这种组合的 UTF-8 字符。在将 html 传递给 DOMpdf 之前,请使用以下代码进行编码:
$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
Use DejaVu font in your css
在你的 css 中使用 DejaVu 字体
*{ font-family: DejaVu Sans; font-size: 12px;}
Make sure you have set utf-8 encoding in HTML <head>
tag
确保在 HTML<head>
标签中设置了 utf-8 编码
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Now all special characters are working "? ? ? ? ? y á í é"
现在所有特殊字符都可以使用“? ? ? ? ? y á í é”
回答by Prasant Kumar
Only Add
只添加
<style>
*{ font-family: DejaVu Sans !important;}
</style>
before </head>
It is working for me.
在</head>
它为我工作之前。
回答by Dirk de Boer
utf8_decode() did the trick for me with some German translations like ? and ü.
utf8_decode() 为我做了一些德语翻译,比如 ? 和你。
echo utf8_decode('X Ponuka ?íslo ?erny ?e?ky <br>');
回答by Fusion
Nothing out of mentioned answers helped me. After hours of struggle I switched to niklasravnsborg/laravel-pdfhas nearly exactly the same syntax and usage, and everything is working allright.
提到的答案中没有任何帮助我。经过数小时的努力,我切换到niklasravnsborg/laravel-pdf具有几乎完全相同的语法和用法,并且一切正常。
回答by David ?karda
If you don't mind having only one charset you can change every charset in dompdf_font_family_cache.dist.php
如果您不介意只有一个字符集,则可以更改其中的每个字符集 dompdf_font_family_cache.dist.php
just like
就像
<?php
$distFontDir = $rootDir . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'fonts' . DIRECTORY_SEPARATOR;
return array(
'sans-serif' =>
array(
'bold' => $distFontDir . 'DejaVuSans-Bold',
'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
'italic' => $distFontDir . 'DejaVuSans-Oblique',
'normal' => $distFontDir . 'DejaVuSans'
),
'times' =>
array(
'bold' => $distFontDir . 'DejaVuSans-Bold',
'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
'italic' => $distFontDir . 'DejaVuSans-Oblique',
'normal' => $distFontDir . 'DejaVuSans'
),
'times-roman' =>
array(
'bold' => $distFontDir . 'DejaVuSans-Bold',
'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
'italic' => $distFontDir . 'DejaVuSans-Oblique',
'normal' => $distFontDir . 'DejaVuSans'
),
'courier' =>
array(
'bold' => $distFontDir . 'DejaVuSans-Bold',
'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
'italic' => $distFontDir . 'DejaVuSans-Oblique',
'normal' => $distFontDir . 'DejaVuSans'
),
'helvetica' =>
array(
'bold' => $distFontDir . 'DejaVuSans-Bold',
'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
'italic' => $distFontDir . 'DejaVuSans-Oblique',
'normal' => $distFontDir . 'DejaVuSans'
),
'zapfdingbats' =>
array(
'bold' => $distFontDir . 'DejaVuSans-Bold',
'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
'italic' => $distFontDir . 'DejaVuSans-Oblique',
'normal' => $distFontDir . 'DejaVuSans'
),
'symbol' =>
array(
'bold' => $distFontDir . 'DejaVuSans-Bold',
'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
'italic' => $distFontDir . 'DejaVuSans-Oblique',
'normal' => $distFontDir . 'DejaVuSans'
),
'serif' =>
array(
'bold' => $distFontDir . 'DejaVuSans-Bold',
'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
'italic' => $distFontDir . 'DejaVuSans-Oblique',
'normal' => $distFontDir . 'DejaVuSans'
),
'monospace' =>
array(
'bold' => $distFontDir . 'DejaVuSans-Bold',
'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
'italic' => $distFontDir . 'DejaVuSans-Oblique',
'normal' => $distFontDir . 'DejaVuSans'
),
'fixed' =>
array(
'bold' => $distFontDir . 'DejaVuSans-Bold',
'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
'italic' => $distFontDir . 'DejaVuSans-Oblique',
'normal' => $distFontDir . 'DejaVuSans'
),
'dejavu sans' =>
array(
'bold' => $distFontDir . 'DejaVuSans-Bold',
'bold_italic' => $distFontDir . 'DejaVuSans-BoldOblique',
'italic' => $distFontDir . 'DejaVuSans-Oblique',
'normal' => $distFontDir . 'DejaVuSans'
),
'dejavu sans mono' =>
array(
'bold' => $distFontDir . 'DejaVuSansMono-Bold',
'bold_italic' => $distFontDir . 'DejaVuSansMono-BoldOblique',
'italic' => $distFontDir . 'DejaVuSansMono-Oblique',
'normal' => $distFontDir . 'DejaVuSansMono'
),
'dejavu serif' =>
array(
'bold' => $distFontDir . 'DejaVuSerif-Bold',
'bold_italic' => $distFontDir . 'DejaVuSerif-BoldItalic',
'italic' => $distFontDir . 'DejaVuSerif-Italic',
'normal' => $distFontDir . 'DejaVuSerif'
)
)
?>
I know it's not the best way, but it saves lot of time
我知道这不是最好的方法,但它节省了很多时间
回答by Timo Huovinen
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
.
Dompdf 不支持后备字体,因此如果它不支持您的字符,您就不能使用您喜欢的字体,并且您也不能将其他字体设置为这些字符的后备字体,例如droid sans fallback
.
What you can do instead is take advantage of regex unicode script ranges: https://www.regular-expressions.info/unicode.htmlto wrap those blocks of text into spans and give them the fallback font.
您可以做的是利用正则表达式 unicode 脚本范围:https: //www.regular-expressions.info/unicode.html将这些文本块包装到跨度中并为它们提供后备字体。
Example:
例子:
$body = 'test 简化字 彝語/彝语 test ?íslo ?erny ?e?ky';
$cjk_scripts = 'Bopomofo|Han|Hiragana|Katakana';
$cjk_scripts = preg_replace('/[a-zA-Z_]+/', '\p{http://192.168.10.10/fonts/pdf/wts11.ttf
}', $cjk_scripts);
// wrap the CJK characters into a span with it's own font
$body = preg_replace("/($cjk_scripts)+/isu", '<span class="cjk">$html = <<<EOT
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
@font-face {
font-family: chinese;
src: url('http://192.168.10.10/fonts/pdf/wts11.ttf') format('truetype');
}
.chineseLanguage { font-family: chinese; }
body {font-family: DejaVu Sans, sans-serif;}
</style>
</head>
<body>
Chinese
<div class='chineseLanguage'>
忠烈祠
中文 - 这工作<br>
</div>
hello world <br>
Russian - русский текст <br>
Greek - α,β,γ,δ,ε <br>
chars - !@#$%^&* -=- <br><br>
<br>
Hebrew (iw)<br><br>
?? ???? ?? ??? ?????? ????? ??? ?? ???? ??? ??????<br>
<br>
</body>
</html>
EOT;
</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]);
回答by Woody Hayday
Lots of answers here, struggled to get any to provide cross-language support reliably. I believe that for those of us making distributed software, there is also server-setting blocks which stop some functionality such as @import
and src:url()
in pdfdom automatically working to embed a font.
这里有很多答案,很难找到可靠地提供跨语言支持的答案。我认为,对于我们这些做分布式软件,也有服务器设定块,其阻止一些功能,比如@import
和src:url()
在pdfdom自动工作嵌入字体。
The following solution has worked across many servers & locally hosted sites, and requires no command line access:
以下解决方案适用于许多服务器和本地托管站点,并且不需要命令行访问:
- Retrieve font you want to use as a .ttf (for language support including Cyrillic, Greek, Devanagari, Latin, and Vietnamese, we used Noto Sanswith all optional languages checked)
- Run/build-in the following script and fire PDFBuilder_install_font_family() ONCE only (singular install)
- 检索要用作 .ttf 的字体(为了支持包括西里尔文、希腊文、梵文、拉丁文和越南文在内的语言,我们使用了Noto Sans,并选中了所有可选语言)
- 运行/内置以下脚本并仅触发 PDFBuilder_install_font_family() 一次(单一安装)
Gist for PDFBuilder_install_font_family()
: https://gist.github.com/woodyhayday/f8dc36cc7ec922bc1894f33eb2b0e928
要点PDFBuilder_install_font_family()
:https: //gist.github.com/woodyhayday/f8dc36cc7ec922bc1894f33eb2b0e928
回答by Yevgeniy Afanasyev
Chinese characters are causing problems sometimes. The important part is to have good font hereis a list you can download.
中文字符有时会引起问题。重要的部分是要有好的字体,这里有一个你可以下载的列表。
I chose first named "Kai Bold Font" here is a download page
我选择了第一个命名为“Kai Bold Font” 这里是一个下载页面
Then put it on your hosting service in a public folder. I put it into
然后将它放在您的托管服务上的公共文件夹中。我把它放进
ini_set("allow_url_fopen", true);
and here is my html example
这是我的 html 示例
@import url('https://fonts.googleapis.com/css?family=Roboto:400,700&subset=latin-ext');
body {font-family: 'Roboto', sans-serif;}
PS. there is a little chance you might need this set:
附注。您可能需要此套装的可能性很小:
##代码##回答by general666
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...
我遇到了同样的问题,我很简单地解决了它。只需在生成 HTML 时使用的 CSS 文件中导入具有所需语言子集的谷歌字体。在您的 HTML 文件中指定 utf-8 并且它正在工作...
##代码##