【问题标题】:Generating PDF document with unicode characters stored in the database使用存储在数据库中的 unicode 字符生成 PDF 文档
【发布时间】:2014-07-09 07:01:03
【问题描述】:

我想生成带有 unicode 字符的 PDF 文档。 我将 using utf8_unicode_ci 存储在数据库中。

这是我的桌子:

language(word_id,english,sinhala,tamil)

这是我生成 pdf 的代码。但是僧伽罗语和泰米尔语单词没有出现。

<?php
$word_id=  '2';
require_once '../model/language.php';
$obj=new Word();
$result=($obj->getWord($word_id));

include_once 'common/dompdf/dompdf_config.inc.php';
$date=date("Y/m/d");
$html="Word Details<br/>";
$value=  mysql_fetch_assoc($result);
$html.='<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<div style="float:left;width:96%">
     <table border="0" width="100%">

        <tr>
            <th>English Word : </th>
            <td><input type="text" name="enhlish" value="'.$value['english'].'"/></td>
        </tr>
        <tr>

            <td colspan="2"><hr/></td>
        </tr>
        <tr>
            <th>Sinhala Word : </th>
            <td><input type="text" name="sinhala" value="'.$value['sinhala'].'"/></td>
        </tr>
        <tr>
            <th>Tamli Word : </th>
            <td><input type="text" name="tamli" value="'.$value['tamil'].'"/></td>
        </tr>

       </table>';


$dompdf = new DOMPDF();
$dompdf = new DOMPDF(); $html = iconv('UTF-8','Windows-1250',$html);
$dompdf->load_html($html,'UTF-8');
$dompdf->render();
$dompdf->stream("dompdf_out.pdf",
array("Attachment" => false));
exit(0);


 include("foot.inc"); 
?>

这是模型中的代码:

require_once 'connection.php';
class Word{
function getWord($word_id){
        $conn = new Connection();
        $sql = "select * from language where word_id='$word_id'";
        $result = $conn->query($sql);
        return $result;
        }
    }  

谁能告诉我这有什么问题,我该如何纠正?

【问题讨论】:

  • 不相关,但你应该避免使用已弃用的 mysql 函数并使用 mysqli。

标签: php html mysql unicode dompdf


【解决方案1】:

首先是一些一般性的建议...

除了将数据存储为 UTF8 之外,您还需要确保数据库连接采用 UTF8 格式。如何执行此操作取决于您的数据访问库。我不太清楚您使用的是什么数据访问库,但我看到了一些经典的 mysql 函数。如果这是您使用的内容,您将在连接到数据库后使用以下内容:

mysql_query("SET NAMES 'utf8'");

您还应该确保 PHP 在 UTF8 中本机工作。这里有两件事你需要。首先,dompdf 需要 MBString 扩展来正确处理多字节字符。其次,您可能想使用以下代码告诉 PHP 将您的字符数据视为 UTF8:

mb_internal_encoding('UTF-8');

最后,为了在 PDF 中显示 Windows ANSI 字符集之外的字符,您需要一种支持这些字符的字体。 dompdf v0.6.x 默认包含 DejaVu 字体,但不支持泰米尔语,因此您必须将字体加载到 dompdf 中。最简单的方法是使用@font-face。您应该阅读dompdf Unicode How-To(它有点过时,但仍有有用的信息)。然后查看this answer to the question "dompdf and set different font-family"


现在有一些具体的建议......

1) 一直使用 UTF8。您正在使用基于 UTF8 的字符集,应该将其保留在该字符集中。旧版本的 dompdf(0.5.x 和更早版本)仅支持 Windows-ANSI。较新的版本在 UTF8 中本机工作,即使您不使用任何“特殊”字符,UTF8 也是首选的文档编码。

2) 不要从 UTF8 转换为较小的编码。通过较少的编码,我的意思是从支持大字符集的包容性编码(如 UTF8)转换为有限编码(如 iso-8859-x 或 Windows-12XX)。同样,如果目标编码不支持您的字符,您将丢失信息。您将文档字符串从 UTF8 转换为 Windows-1250。该编码是否支持您正在使用的字符?

3) 您的文档应始终指定正确的编码。您在文档元标记中指定文档以 UTF8 编码,因此 dompdf 将假定这是要使用的适当编码。如果您转换为另一种编码,您的字符可能无法正确表示。

4) 如上所述,您需要一种支持文档中使用的字符的字体。您根本没有指定任何字体,因此将使用 PDF 核心字体。这些字体仅支持使用 Windows ANSI 编码的文本。在 dompdf 问题跟踪器上阅读这篇关于显示泰米尔语字符的帖子:https://github.com/dompdf/dompdf/issues/838#issuecomment-47415806

考虑到上述情况,您的代码应该看起来更像这样:

include_once 'common/dompdf/dompdf_config.inc.php';
$date=date("Y/m/d");
$value=  mysql_fetch_assoc($result);

$html = '
  <html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <style>
    @font-face {
      font-family: latha;
      font-style: normal;
      font-weight: 400;
      src: url(http://yourfontprovider.com/latha.ttf) format("true-type");
    }
    </style>
  </head>
  <body>
    Word Details<br/>
    <div style="float:left;width:96%">
    <table border="0" width="100%">
      <tr>
        <th>English Word : </th>
        <td><input type="text" name="enhlish" value="'.$value['english'].'"/></td>
      </tr>
      <tr>
        <td colspan="2"><hr/></td>
      </tr>
      <tr>
        <th>Tamli Word : </th>
        <td><input type="text" name="tamli" value="'.$value['tamil'].'" style="font-family: latha, sans-serif;" /></td>
      </tr>
    </table>
  </body>
  </html>
';

$dompdf = new DOMPDF();
$dompdf->load_html($html,'UTF-8');
$dompdf->render();
$dompdf->stream("dompdf_out.pdf", array("Attachment" => false));

【讨论】:

  • 非常感谢,这很有用.. :)
猜你喜欢
  • 2016-09-02
  • 2018-03-07
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 2017-11-23
  • 2014-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多