Php 使用存储在数据库中的unicode字符生成PDF文档

Php 使用存储在数据库中的unicode字符生成PDF文档,php,html,mysql,unicode,dompdf,Php,Html,Mysql,Unicode,Dompdf,我想生成带有unicode字符的PDF文档。 我使用utf8\u unicode\u 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));

我想生成带有unicode字符的PDF文档。 我使用
utf8\u unicode\u 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"); 
?>

有人能告诉我这有什么问题吗?我怎样才能纠正它?

首先是一些一般性的建议

除了将数据存储为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。你应该仔细阅读(它有点过时,但仍然有有用的信息)。然后退房


现在有一些具体的建议

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问题跟踪器上关于显示泰米尔字符的这篇文章:

考虑到上述内容,您的代码应该更像这样:

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));
include_once'common/dompdf/dompdf_config.inc.php';
$date=日期(“Y/m/d”);
$value=mysql\u fetch\u assoc($result);
$html='1
@字体{
字体系列:latha;
字体风格:普通;
字体大小:400;
src:url(http://yourfontprovider.com/latha.ttf)格式(“真实类型”);
}
单词详细信息
英文单词:
塔姆利语: '; $dompdf=新的dompdf(); $dompdf->load_html($html,'UTF-8'); $dompdf->render(); $dompdf->stream(“dompdf_out.pdf”,数组(“附件”=>false));
Unrelated,但您应该避免使用不推荐使用的mysql函数,而是使用mysqli。非常感谢您,这非常有用:)
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));