php wkhtmltopdf - 编码问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11859872/
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
wkhtmltopdf - encoding issue
提问by Michal S
I'm using wkhtmltopdf to export html pages to pdf, but it seems it has a problem with Czech characters... I load whole html into variable, then I change encoding and run wkhtmltopdf like this:
我正在使用 wkhtmltopdf 将 html 页面导出为 pdf,但似乎捷克字符有问题......我将整个 html 加载到变量中,然后更改编码并像这样运行 wkhtmltopdf:
$html = ob_get_clean();
$html = iconv("UTF-8","Windows-1250", $html);
file_put_contents('../export.php', $html);
$commandString = WKHTML_LIB.'http://www.estiroad.com/export.php sestava.pdf';
exec($commandString);
The .html file has the right encoding, but even when I set --encoding windows-1250 parameter to command string, its just not working... Thanks for any ideas...
.html 文件具有正确的编码,但即使我将 --encoding windows-1250 参数设置为命令字符串,它也无法正常工作...感谢您的任何想法...
EDIT: I solved the issue! The catch was in constant WKHTML_LIB, which I defined on the beginning of the page:
编辑:我解决了这个问题!问题是在我在页面开头定义的常量 WKHTML_LIB 中:
define('WKHTML_LIB', "../wkhtmltopdf/wkhtmltopdf-amd64");
I just wrote the path directly to the exec(); command and now it works even with the flags. Sorry for bothering you with such a triviality... Now the $commandString line looks like this:
我只是直接写了路径到exec(); 命令,现在它甚至可以与标志一起使用。很抱歉用这么琐碎的事情打扰你......现在 $commandString 行看起来像这样:
$commandString = '"../wkhtmltopdf/wkhtmltopdf-amd64" --print-media-type --page-size A4 -R 50 --encoding windows-1250 --header-html header.html --margin-top 10mm --margin-bottom 10mm --margin-left 10mm --margin-right 10mm http://www.estiroad.com/export.php sestava.pdf';
采纳答案by Michal S
I solved the issue! The catch was in constant WKHTML_LIB, which I defined on the beginning of the page:
我解决了这个问题!问题是在我在页面开头定义的常量 WKHTML_LIB 中:
define('WKHTML_LIB', "../wkhtmltopdf/wkhtmltopdf-amd64");
I just wrote the path directly to the exec(); command and now it works even with the flags. Sorry for bothering you with such a triviality... Now the $commandString line looks like this:
我只是直接写了路径到exec(); 命令,现在它甚至可以与标志一起使用。很抱歉用这么琐碎的事情打扰你......现在 $commandString 行看起来像这样:
$commandString = '"../wkhtmltopdf/wkhtmltopdf-amd64" --print-media-type --page-size A4 -R 50 --encoding windows-1250 --header-html header.html --margin-top 10mm --margin-bottom 10mm --margin-left 10mm --margin-right 10mm http://www.estiroad.com/export.php sestava.pdf';
回答by Marcel Burkhard
For future reference:
备查:
I had the same problem with german umlauts.
我对德国元音变音也有同样的问题。
As soon as I added
我一加
<meta charset="UTF-8" />
to the html page the problem was solved.
到 html 页面问题就解决了。
That of course presupposes your page is served as utf-8.
当然,前提是您的页面是 utf-8。
回答by Leon Chung
I had a similar problem with wkhtmltopdf before where the Chinese characters were not rendered properly. I have solved the problem by installing the appropriate Chinese fonts.
我在 wkhtmltopdf 之前遇到了类似的问题,之前中文字符没有正确呈现。我通过安装合适的中文字体解决了这个问题。
By any chance do you have to install any Czech related fonts for the characters to render properly? As I am not familiar with Czech characters / fonts, I am not sure whether this applies. Hope this helps.
您是否必须安装任何与捷克语相关的字体才能正确呈现字符?由于我不熟悉捷克字符/字体,我不确定这是否适用。希望这可以帮助。
回答by Nenotlep
Just now I made a test with those characters you provided and they work fine for me. Win7, wkhtmltopdf 0.11.0 rc2. Works on cover, toc, bookmarks, content and headers. Also tested with korean and chinese characters and even they work.
刚才我用你提供的那些字符进行了测试,它们对我来说很好用。Win7,wkhtmltopdf 0.11.0 rc2。适用于封面、目录、书签、内容和标题。还用韩文和中文字符进行了测试,甚至它们也能工作。
PDF Generated using (file locations removed)
--print-media-type --page-size A5 --header-html header.html --footer-html footer.html --margin-bottom 10mm --margin-top 10mm --margin-left 10mm --margin-right 10mm cover cover.html toc --xsl-style-sheet tocfile.xsl temp.html temp.pdf
PDF 生成使用(文件位置已删除)
--print-media-type --page-size A5 --header-html header.html --footer-html footer.html --margin-bottom 10mm --margin-top 10mm --margin-left 10mm --margin-right 10mm cover cover.html toc --xsl-style-sheet tocfile.xsl temp.html temp.pdf
temp.html is Extremely invalid XHTML, first line says <?xml version="1.0" encoding="iso-8859-1"?>. The temp.html file was written to disk with C# using UTF-8 and it works. I really suggest using UTF-8 wherever possible.
temp.html 是极其无效的 XHTML,第一行说<?xml version="1.0" encoding="iso-8859-1"?>。temp.html 文件是用 C# 使用 UTF-8 写入磁盘的,它可以工作。我真的建议尽可能使用 UTF-8。



