Hi,
I need to generate a PDF report from my MySql. The problem appears when we want to write in Persian. The PDFlib appearently doesn't support UTF-8. I also checed UFPDF. It neither work well.
Any advice?
I use:
PHP Version 4.3.7
mysql: Client API version 3.23.49

Dani AI

Generated

Short summary for and : the practical, low-effort paths today are (1) use a PDF library with built‑in Unicode/RTL support (TCPDF or mPDF), or (2) keep an FPDF-style workflow but add UTF‑8 support and Arabic shaping (tFPDF + Ar‑PHP). TCPDF and mPDF handle UTF‑8 and RTL out of the box; tFPDF adds UTF‑8 to FPDF and Ar‑PHP provides the Arabic/Persian glyph-joining you need. (tcpdf.org)

TCPDF minimal flow (install TCPDF, add a Unicode TTF, then write UTF‑8 text):

require 'tcpdf_include.php';
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->AddPage();
$fontname = $pdf->addTTFfont('/path/to/NotoNaskhArabic-Regular.ttf', 'TrueTypeUnicode', '', 32);
$pdf->SetFont($fontname, '', 12);
$persian = $row['persian_text']; // ensure this is UTF-8
$pdf->Write(0, $persian);
$pdf->Output('report.pdf', 'F');

See TCPDF docs for addTTFfont/Unicode font handling. (tcpdf.org)

If you prefer staying with FPDF-style code: use tFPDF (UTF‑8 version of FPDF) and pre-process Persian/Arabic text with Ar‑PHP’s utf8Glyphs() so characters join correctly, then AddFont() with a Persian-capable TTF. tFPDF requires mbstring; Ar‑PHP exposes utf8Glyphs() for glyph joining. (fpdf.org)

Other options and troubleshooting: if your layout is HTML/CSS heavy, render with mPDF (good RTL/shape support) or generate a PDF via headless Chromium (Puppeteer) — Chrome’s text engine handles shaping/RTL well when the right fonts are installed. Always confirm the whole pipeline is UTF‑8: MySQL connection/tables and PHP connection must use utf8mb4 (or set charset), otherwise saved text will be garbled. (mpdf.github.io)

Quick checklist: use a Unicode TTF covering Persian (Noto Naskh, Vazirmatn/Vazir, Amiri, etc.), embed the font in the PDF, enable mbstring, ensure PDO/mysqli is set to utf8mb4, and if mixing LTR+RTL test copy/paste and search in the resulting PDF.

Recommended Answers

All 3 Replies

I am also interested in UTF-8 support PDFlib Lite since I need to write Cyrillic text into a PDF.

Any suggestions please?

Member Avatar for Member #120589

For Cyrillic - have you tried FPDF?

http://www.phpclasses.org/browse/package/421.html

I haven't tried it with Cyrillic, but I think it should work.


For Persian, does the PDF_load_font() function help in any way?

Many thanks for that - I will have a look at FPDF

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.