How to Center Text in FPDF?

How to Center Text in FPDF?

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

Stefan Bogdanescu

Founder & Senior Architect

Centering Text in FPDF Using MultiCell

When generating PDFs with FPDF, a common requirement is to center text within a cell or across the page width. If you are working with dynamic content—such as text passed via $_POST where the length is unpredictable—you cannot rely on fixed coordinates for left alignment. Instead, you need a method that calculates positioning automatically based on the available space and the content itself.

The most straightforward approach within FPDF is utilizing the third parameter of the MultiCell() method. By passing 'C' as the alignment flag, FPDF handles the centering logic internally without requiring manual coordinate calculations.

// $x, $y are current cursor coordinates (optional if not specified)
// $height defines the cell height
// $text contains your dynamic content from $_POST
// '0' disables borders
// 'C' centers the text horizontally within the cell

$this->MultiCell(0, $height, $text, '', 'C');

In this example, the first argument 0 tells FPDF to extend the cell across the full width of the page. If you omit the x-coordinate or set it to 0, FPDF assumes the current cursor position. However, for true centering relative to the page margins, you often need to manage the x-coordinate explicitly if you are chaining multiple operations.

Handling Page Width and Margins

A critical detail often overlooked is the difference between centering within a cell versus centering relative to the entire page. The MultiCell() method with 'C' aligns text to the center of the cell, not necessarily the center of the page. If your cell spans the full width (argument 0), the visual result is effectively centered on the page, assuming the default margins are symmetrical.

If you have custom margins defined via SetLeftMargin, SetRightMargin, or by using SetMargins(), FPDF respects these settings when calculating the cell width. When you pass 0 as the first argument to MultiCell, it calculates the available width based on current margins. Consequently, 'C' aligns the text to the midpoint of that calculated width.

// Ensure margins are set if you want content centered relative to page edges
$this->SetMargins(10, 10); 

// This will center the text within the available margin space
$this->MultiCell(0, 20, $dynamicText, '', 'C');

Dealing with Multi-line Content

When the input text contains newlines or is broken into multiple paragraphs using MultiCell, FPDF continues the cell on the next line automatically. The 'C' alignment applies to each line individually. This means every line in the generated block will be centered within the column width, regardless of whether the content wraps naturally or is manually broken with \n.

This behavior is particularly useful for forms where users input varying amounts of text. You do not need to pre-calculate line breaks or measure string widths manually. FPDF handles the wrapping and alignment iteratively as it writes the cell.

However, be aware that MultiCell always starts a new paragraph when it wraps. If you are using a single continuous string with embedded \n, FPDF treats each segment between newlines as a separate paragraph but keeps them within the same column flow. The centering applies to each of these segments independently.

Alternative: Using SetXY and Manual Calculation

While 'C' is convenient, there are scenarios where you might prefer absolute control over positioning. For instance, if you need to place text in the exact geometric center of a specific page regardless of margins, or if you are drawing custom shapes around the text, manual calculation using SetXY() combined with Cell() or MultiCell() with 'L' (left alignment) offers precision.

To achieve this manually:

  1. Calculate the total available width for the cell.
  2. Determine the length of your text string in points. Note that getting an exact width usually requires a trial-and-error loop or using GetStringWidth().
  3. Subtract the text width from the available width to find the left offset.
  4. Move the cursor using SetXY($x + offset, $y).
// Example of manual centering calculation
$width = $this->getPageWidth() - $this->getLeftMargin() - $this->getRightMargin();
$textWidth = $this->GetStringWidth($text); // Measure text width
$offset = ($width - $textWidth) / 2;

$this->SetXY($this->getLeftMargin() + $offset, $y);
$this->MultiCell(0, $height, $text, '', 'L');

This approach is more verbose but necessary when dynamic alignment flags like 'C' do not produce the exact visual result required, such as when dealing with complex layouts or non-standard margin configurations. In most standard use cases involving $_POST data, however, the built-in 'C' flag remains the most robust and efficient solution.

Our network