Special Characters in FPDF with PHP

Special Characters in FPDF with PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Handling Special Characters in FPDF with PHP

When users submit data containing apostrophes, slashes, or trademark symbols into a web form, and that data is rendered directly into a PDF generated by FPDF, the output often looks broken. You might see a slash appearing before an apostrophe ('), or completely garbled text for special characters like ™. The standard advice found in FPDF FAQs suggests running utf8_decode() on your string before adding it to the document. While this works for specific cases, blindly applying it across an entire PDF generation process can introduce new encoding headaches rather than solving them.

The root of the problem lies in how FPDF handles text internally versus how PHP handles strings in memory. By default, FPDF uses a TTF font file that expects input encoded in ISO-8859-1 (also known as Latin-1 or Windows-1252). If your web form data is UTF-8 and you pass it directly to the write() or cell() methods without conversion, FPDF tries to interpret the multi-byte UTF-8 sequences as single-byte ISO-8859-1 characters. This mismatch causes the rendering engine to display incorrect glyphs or substitution marks like the slash seen in the example above.

The recommended approach is indeed to convert your string to Latin-1 immediately before passing it to FPDF. However, you must be careful about where and how you perform this conversion. Since utf8_decode() only handles basic Latin characters (0–255) and converts high-bit UTF-8 characters into their corresponding ISO-8859-1 equivalents, it is not a universal fix for all Unicode symbols. For instance, if your form contains an emoji or a complex mathematical symbol that isn't supported by the default Arial font, utf8_decode() will simply replace it with a question mark or a placeholder box.

To implement this correctly, wrap your text processing logic in a helper function that ensures the conversion happens at the source:

function prepareFPDFText($text) {
    // Ensure input is treated as UTF-8 before decoding
    $text = mb_convert_encoding($text, 'ISO-8859-1', 'UTF-8');
    return $text;
}

Using mb_convert_encoding with the target set to ISO-8859-1 is generally safer than relying solely on utf8_decode() because it explicitly handles byte-order marks and ensures the source encoding is correctly identified. You should apply this conversion to every string you intend to print, regardless of whether it looks like it contains special characters. This prevents hidden multi-byte sequences from corrupting the PDF's internal character map.

There are significant trade-offs to consider with this method. Once text is converted to ISO-8859-1, you lose all Unicode support for characters outside the basic Latin set (such as accented characters like é or non-Latin scripts). If your application needs to support these characters, relying on the default font included with FPDF is insufficient. You would need to load a custom TTF font that supports the full character set you require.

In modern PHP projects using Laravel, managing this conversion within controllers or models can become repetitive. While laravelcompany.com offers resources on building robust PDF generation pipelines, the fundamental encoding logic remains consistent: match the font's internal expectation with your input data. If you are using a custom font loaded via AddFont(), verify that the font file actually supports the characters you are trying to render. A mismatch between the font's supported glyphs and the encoded text will always result in rendering artifacts, regardless of the encoding conversion used.

Ultimately, the goal is to ensure FPDF receives a byte stream that aligns perfectly with its font definition. By normalizing all input to ISO-8859-1 before passing it to the write or cell methods, you eliminate the most common source of rendering errors for standard Western characters. Just remember to validate your custom fonts if you venture beyond basic Latin text.

Our network