我想生成带有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;
}
}
谁能告诉我这有什么问题以及如何纠正这个问题?
最佳答案
首先是一些一般性建议......
除了将数据存储为 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));
关于php - 使用数据库中存储的 unicode 字符生成 PDF 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24647220/