8

我正在使用 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

我怎样才能解决这个问题?

4

1 回答 1

1

好吧,我想我有一个解决你的问题的方法。我可以制作一个看起来像我认为您正在寻找的pdf。这是它的截图

http://i.imgur.com/UBdkNDx.png

为此,您必须使用与 dompdf 不同的方式来制作 pdf:wkhtmltox-php。

wkhtmltox-php 是一个从源代码编译的自定义 php 命令,它使用 libwkhtmltox 来制作 pdf。安装它需要一些努力,但它会像上面一样呈现您的波斯文本,并且比 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 开发包。注意:如果您在 configure 或 make install 时遇到错误,您将需要安装 c 编译工具,如“make”和“gcc”

通过阅读您的输出,make install您将知道模块位于哪个目录。通常是:

 /usr/lib64/php/modules/

第三:在您的 php.ini 文件中设置 php 以了解该模块,在“动态扩展”部分标题下添加以下行

extension=phpwkhtmltox.so

第四:运行ldconfig

$ ldconfig

第五:重启apache(或者你正在使用的任何httpd)

最后:像这样使用它:对于我的示例,我只是使用来自维基百科的国际象棋打开页面,因为我没有指向您的示例 html 的 url。

<?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 做得对。

于 2015-08-02T22:22:37.273 回答