前端之家收集整理的这篇文章主要介绍了
php – 当我使用波斯文本时,DomPDF输出被破坏了,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_
404_0@
我正在使用DomPDF和
PHP来创建PDF
文件.当文本是英文时,一切正常,但是当我想转换波斯文本时,
输出就会被破坏
这是包含波斯文和英文文本的示例文件:
<html><head>
<Meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body {
font-family: 'dejavu sans';
direction;rtl;
}
p {
font-size: 2em;
background: #eee;
padding: 1em;
}
h2 {
color: #999;
}
</style>
<style type="text/css"></style></head>
<body marginwidth="0" marginheight="0">
<div style="text-align:right">
<h2>Give You Glory</h2>
<br/>
Hadi
</div>
<br/>
هادی
</body></html>
这是输出PDF文件:
http://i.stack.imgur.com/HOyMO.png
我怎样才能解决这个问题?
好吧,我想我有一个
解决你的问题的
方法.我可以制作一个看起来像我认为你正在寻找的PDF.这是它的截图
http://i.imgur.com/UBdkNDx.png
要做到这一点,你必须使用不同的方式制作pdf而不是dompdf:wkhtmltox-PHP.
wkhtmltox-PHP是一个从源代码编译的自定义PHP命令,它使用libwkhtmltox制作pdfs.安装它需要花费一些精力,但它会像上面那样呈现你的波斯文本并且比dompdf快得多.
这些说明假设linux或类似你的操作系统:
第一:安装wkhtmltopdf.
这里有大多数操作系统的预编译二进制文件:
http://wkhtmltopdf.org/downloads.html
第二:获取并编译并安装PHP-wkhtmltox.
cd /tmp/
wget https://github.com/mreiferson/PHP-wkhtmltox/archive/master.zip
unzip master.zip
cd PHP-wkhtmltox-master/
PHPize
./configure
sudo make install
注意:如果您的计算机上没有安装PHPize,则需要安装PHP dev软件包.
注意:如果你在configure或make install上遇到错误,你需要安装c编译工具,比如’make’和’gcc’
通过阅读make install的输出,您将知道该模块所在的目录.通常是:
/usr/lib64/PHP/modules/
第三:设置PHP以了解这个模块
在您的PHP.ini文件中,在“动态扩展”标题下添加以下行
extension=PHPwkhtmltox.so
第四:运行ldconfig
$ldconfig
第五:重启apache(或者你正在使用的httpd)
最后:像这样使用它:
对于我的例子,我只是使用维基百科的国际象棋开放页面,因为我没有你的示例HTML的网址.
<?PHP
/**
* the config_array has lots of options but the two most important are:
* "out" this is the full path to where you want your pdf made
* "imageQuality" basically the same as jpg image quality. lower quality is slower,higher quality is a bigger file
*/
$config_array = array( "out" => "/tmp/pdfdocument.pdf","imageQuality" => 95);
/**
* the array of urls that are the input html for making your pdf. note that these are not files,but urls
* also note that this is an array of arrays keyed by "page"
*/
$htmls_array = array(array("page"=>"http://en.wikipedia.org/wiki/Queen's_Gambit_Declined"));
/**
* run the conver like so and your pdf file should be on disk
*/
wkhtmltox_convert('pdf',$config_array,$htmls_array);
?>
如果你看看我上面发布的截图,看起来PHPwkhtmltox可以正常工作.