FPDF: Change text color while inside a Cell?

FPDF: Change text color while inside a Cell?

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

Stefan Bogdanescu

Founder & Senior Architect

FPDF: Change text color while inside a Cell?

The core limitation of FPDF's Cell method lies in how it treats content. When you pass a string to SetFont or implicitly via Cell, the library renders that entire block as a single, continuous glyph stream. It does not support inline styling like bolding or color changes within that specific cell without breaking the flow of execution.

In your scenario, calling $pdf->SetTextColor(255,255,255) followed by Cell with the string 'WHITE ORANGE ORANGE WHITE' results in all characters inheriting the current font state. The PDF specification itself does not support "run-length encoding" of color within a single text object generated by FPDF. Consequently, the orange text will render white because the global color setting applies to the whole cell content until changed again for subsequent cells.

To achieve distinct colors for specific words like 'ORANGE' while keeping 'WHITE', you must fragment the string into multiple Cell calls. This is not a workaround; it is the architectural requirement of the FPDF library. You need to treat each word as an independent rendering unit, resetting the font color between them.

Here is the implementation pattern required:

$pdf->SetTextColor(255, 255, 255); // Set white for 'WHITE'
$pdf->Cell(10, 0, 'WHITE', 0, 0, 'C');
$pdf->SetTextColor(255, 165, 0);   // Set orange for 'ORANGE'
$pdf->Cell(10, 0, 'ORANGE', 0, 0, 'C');
$pdf->SetTextColor(255, 165, 0);   // Keep orange for second 'ORANGE'
$pdf->Cell(10, 0, 'ORANGE', 0, 0, 'C');
$pdf->SetTextColor(255, 255, 255); // Set white again for final 'WHITE'
$pdf->Cell(10, 0, 'WHITE', 0, 1, 'C');

Notice the critical changes: the Cell width is reduced to accommodate individual words, and the $ln parameter (the fourth argument) must be set to 0. In your original snippet, you used 1, which forces a line break after every cell. To keep everything on one line, you must manually manage the positioning or accept that the library will not auto-wrap the fragmented text back into a single visual block unless you explicitly calculate the remaining width and call Cell with $ln=0.

This approach introduces specific trade-offs. The most significant is horizontal spacing. Every time you call SetTextColor, FPDF creates a new font string internally, but more importantly, each Cell call adds the standard cell border logic if not disabled, and crucially, it advances the x-coordinate by the width of that word plus padding.

If you simply sum the widths of "WHITE", "ORANGE", etc., without accounting for the internal margins FPDF applies between cells (typically 1 unit or less depending on configuration), your layout will shift unexpectedly. The Cell method includes a margin to prevent text overlap, so chaining them horizontally requires careful calculation or setting the border style to 0 (no borders) and accepting that the library handles the spacing automatically between the disjointed segments.

Another pitfall involves font consistency. If you switch fonts during this process—say, making "ORANGE" bold while "WHITE" is normal—you must ensure the font family remains compatible. FPDF stores the current font state in memory; changing the color does not reset the font family, but changing the font family resets the color to the default black unless explicitly set again. Therefore, if you toggle between bold and regular styles for these words, you must re-apply $pdf->SetTextColor() immediately after every SetFont call that changes the style or family.

For complex documents where this fragmentation becomes tedious, consider switching to TCPDF or mPDF. These libraries support UTF-8 out of the box and offer more robust handling of text formatting, including inline CSS-like properties if using their HTML rendering capabilities. However, for standard FPDF workflows, fragmenting the string is the only reliable path to multi-colored text within a single logical row.

If you are building a Laravel application that relies heavily on PDF generation, libraries like barryvdh/laravel-dompdf or spatie/pdf-to-image might offer different rendering engines under the hood that handle these styling nuances differently. However, sticking to pure FPDF requires adhering to its strict imperative model: set state, render one unit, update state, render next unit.

Ultimately, the solution is not about finding a hidden API flag within SetTextColor or Cell. It is about respecting the procedural nature of the library. By breaking the string into atomic units and managing the color state explicitly between each unit, you regain full control over the visual output without violating the library's constraints.

Our network