Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
757 views
in Technique[技术] by (71.8m points)

pdf - How to set encoding in PHP FPDI library

How to set UTF-8 encoding in php library named FPDI? Here's library: https://www.setasign.com/products/fpdi/manual/

The code:

$pdf = new Fpdi();
$pdf->AddPage();
$pdf->setSourceFile('PdfDocument.pdf');
$tplIdx = $pdf->importPage(1);
 
$pdf->useTemplate($tplIdx, 10, 10, 100);

$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, 'Za?ó?ci? g??l? ja?ń');

$pdf->Output();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could add new font with proper letters

$pdf->AddFont('DejaVu','','DejaVuSansCondensed.php');
$pdf->SetFont('DejaVu', '', 10, '', false);

Then in regard to FPDF library that is used by FPDI:

There possible encodings are:

cp1250 (Central Europe)
cp1251 (Cyrillic)
cp1252 (Western Europe)
cp1253 (Greek)
cp1254 (Turkish)
cp1255 (Hebrew)
cp1257 (Baltic)
cp1258 (Vietnamese)
cp874 (Thai)
ISO-8859-1 (Western Europe)
ISO-8859-2 (Central Europe)
ISO-8859-4 (Baltic)
ISO-8859-5 (Cyrillic)
ISO-8859-7 (Greek)
ISO-8859-9 (Turkish)
ISO-8859-11 (Thai)
ISO-8859-15 (Western Europe)
ISO-8859-16 (Central Europe)
KOI8-R (Russian)
KOI8-U (Ukrainian)

The string that was sent to pdf was in UTF-8 (it was checked by mb_detect_encoding function) and there was a need to convert it with cp1250.

$str = iconv('UTF-8', 'cp1250', 'zazó?ci? g??l? ja?ń');

Another solution would be to try to use:

$pdf->SetFont('freeserif', '', 14, '', true);

UPDATE PRO TIP:

In case of problems with fonts - check out first if the font is installed on your linux server.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...