【问题标题】:DomPDF output is broken when I use persian text当我使用波斯文本时,DomPDF 输出被破坏
【发布时间】:2015-08-03 06:22:37
【问题描述】:

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

我该如何解决这个问题?

【问题讨论】:

  • “输出损坏” ..您是在说 RTL 输出损坏吗?
  • domdpf 当前不支持 RTL。您可以破解库以获得伪支持 (see here)。您也可以尝试TCPDFmPDF,两者似乎都至少有一定程度的 RTL 支持。
  • 这是您要找的吗? i.imgur.com/UBdkNDx.png 如果是这样,我可以给你我认为的解决方案...
  • 我认为波斯语是从右到左书写的。 Dompdf 还不允许这样做
  • 我想这会对你有所帮助 - stackoverflow.com/questions/21201257/…

标签: php dompdf


【解决方案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知道这个模块 在您的 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 做得对。

【讨论】:

    猜你喜欢
    • 2012-03-28
    • 1970-01-01
    • 2013-07-03
    • 2011-12-28
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    相关资源
    最近更新 更多