简体   繁体   English

FPDF 中的汉字问题

[英]Issues with Chinese characters in FPDF

I'm generating some order confirmations using FPDF library, which works great.我正在使用 FPDF 库生成一些订单确认,效果很好。 But now I need some Chinese characters in the document.但是现在我需要在文档中添加一些中文字符。 I have searching for many hours by now and have found no workable solution.我已经搜索了很多小时,但没有找到可行的解决方案。 Is it possible supporting Chinese characters using FPDF or is there an alternative to FPDF which will support it?是否可以使用 FPDF 支持中文字符,或者是否有支持它的 FPDF 替代品?

All the characters are mixed with regular text and are stored in a MySQL database.所有字符都与常规文本混合并存储在 MySQL 数据库中。 The PDF document only show unicode when printing the Chinese characters. PDF 文档在打印汉字时只显示 unicode。

There exist Chinese encoding formats like Big5 or GBK .存在中文编码格式,如Big5GBK However, more likely than not, the text you are trying to input is in Unicode.但是,您尝试输入的文本很可能是 Unicode。 There exists tFPDF , which provides Unicode support.存在tFPDF ,它提供 Unicode 支持。 I will test printing the following traditional hanzi: 中國.我将测试打印以下传统汉字:中国。 This means China, for those reading who do not know.这意味着中国,对于那些不知道的人来说。

//  Remember to copy msjh.ttf to [path to tFPDF]/font/unifont/ directory

//  Initialize tFPDF
require('tfpdf.php');
$pdf = new tFPDF();
$pdf->AddPage();

//  Add a Unicode font like MSJH
$pdf->AddFont('MSJH','','msjh.ttf',true);
$pdf->SetFont('MSJH','',42);

//  Output Chinese string to PDF
$pdf->Text(12,42,"中國");

//  Output PDF document
$pdf->Output();

Another alternative is the Code200365k TTF另一种选择是 Code200365k TTF

$pdf->AddFont('Code200365k','','Code200365k.ttf',true);

I use it for Chinese characters only as follows:我只将它用于汉字,如下所示:

if (preg_match("/[\x{4e00}-\x{9fa5}]+/u", $my_value)){
    $pdf->SetFont('Code200365k','',12);
}
$pdf->Cell(130,9,$my_value,1,1,'L');
$pdf->SetFont('Arial','',11);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM