我想生成带有unicode字符的PDF文档。
我将使用utf8_unicode_ci
存储在db。
这是我的表:
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;
}
}
任何人都可以告诉我这是错的吗?我怎么能纠正这个?
答案 0 :(得分: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字体,但那些不支持tamil,所以你必须将字体加载到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));