26

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();

[SOLUTION]

FIRST: I had to add new font with proper letters

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

SECOND: In regard to FPDF library that is user by FPDI: There possible encodings:

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 by me to pdf was in UTF-8 (I checked it by mb_detect_encoding function) and there was need to convert on cp1250.

$str = iconv('UTF-8', 'cp1250', 'zazółcić gęślą jaźń');

ANOTHER SOULUTION try to use:

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

UPDATE PRO TIP:

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

Adam Kozlowski
  • 3,978
  • 1
  • 23
  • 38
  • @Adam, where do you need UTF-8 encoding in this example? – Jan Slabon Oct 16 '17 at 18:27
  • @JanSlabon insdead $pdf->Write(0, 'This is just a simple text'); I want to use $pdf->Write(0, 'zazółcić gęślą jaźń'); There is a need to use polish letters. On pdf it is not written correctly. – Adam Kozlowski Oct 16 '17 at 18:42
  • 7
    This is not related to FPDI at all but to FPDF. You may check out tFPDF (and FPDI 1.x) for native UTF-8 support. Or create an indidivual encoding as described [here](http://www.fpdf.org/en/tutorial/tuto7.htm). – Jan Slabon Oct 17 '17 at 08:43
  • 2
    Possible duplicate of [FPDF utf-8 encoding (HOW-TO)](https://stackoverflow.com/questions/6334134/fpdf-utf-8-encoding-how-to) – Will B. Feb 25 '19 at 15:48
  • For clarification, the `FPDI` library uses either `FPDF`, `TCPDF`, or `tFPDF` in order to add PDF file importing support to them. FPDF does not support UTF-8 encoding. `tFPDF` is a replacement for `FPDF`, that adds UTF-8 support. However you will still need to add your UTF-8 fonts manually or to your `fonts/unifont` directory. – Will B. Feb 25 '19 at 16:01

0 Answers0