32

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();
2
  • 1
    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. Commented Feb 25, 2019 at 16:01
  • 1
    Did you tried mPDF? mpdf.github.io It bases on FPDF but is a bit more easy to use. You could also set it there in the constructor using 'mode' => 'utf-8'. May take a look Commented Aug 27, 2021 at 14:34

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What's in the DejaVuSansCondensed.php you're referring to when adding the font?
This way: $this->pdf->AddFont('Roboto', '', 'Roboto-Regular.php', public_path('fonts/'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.