Wrap Text in Fpdf in Php

Wrap Text in Fpdf in Php

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

Stefan Bogdanescu

Founder & Senior Architect

Controlling Text Wrapping in FPDF Cells

When working with legacy PDF generation libraries like FPDF, you often encounter a specific behavioral quirk: the distinction between Cell and MultiCell. In your original code, you used Cell to output strings like 'Hi5(xtra)'. Because Cell does not check for line breaks or character width limits internally, it simply prints the string until it hits the right margin. If the text is too long for the cell width (20 units in your case), the overflow gets pushed to the next page or renders outside the intended area, depending on the configuration.

To force the text 'Hi5(xtra)' onto a second line within the same 20-unit width, you must switch to MultiCell. However, as your observations show, simply calling MultiCell changes the layout entirely. The first cell in a sequence of MultiCell calls behaves differently than subsequent ones because MultiCell automatically advances the cursor down when text overflows.

The core issue in your attempt was likely the order of operations and the state of the y-coordinate. When you call $pdf->MultiCell(20, 7, 'Hi5(xtra)', 1), FPDF calculates if the string fits in the current x,y position. If it doesn't fit, it moves down one line height (7 units) and continues writing the remaining text. This is the desired behavior for wrapping, but it disrupts your grid-like layout established by the previous Cell calls which all had a border (1).

The MultiCell Behavior vs. Cell

The fundamental difference lies in how these methods handle the y-coordinate. A standard Cell(w, h, txt) draws text and returns immediately. If you pass a string longer than the width, FPDF ignores the overflow for that specific call; it does not wrap it automatically unless you provide an explicit line break character (\n).

Conversely, MultiCell(w, h, txt, border, align, fill) is designed specifically to handle text wrapping. It calculates the remaining space. If the text overflows, it creates a new line at y + h and continues drawing. This means you cannot use MultiCell if you strictly require every cell to start on a fresh line without any automatic breaking logic interfering with your specific grid alignment.

Implementing Manual Wrapping

If you need to keep the strict 20-unit width for every row but allow one specific string to wrap, you have two primary options. The first is to manually insert a newline character within the string itself. FPDF treats \n as a command to move to the next line and start writing at the current x-position (unless MultiCell logic is interfering).

To achieve the look where 'Hi5' stays on the first line and (xtra) drops to the second, you should modify the string content or use MultiCell for that specific item. Here is how you would adjust the code:

require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',16);

// Standard cells remain unchanged as they fit or overflow to next page
$pdf->Cell(20,7,'Hi1',1);
$pdf->Cell(20,7,'Hi2',1);
$pdf->Cell(20,7,'Hi3',1);

$pdf->Ln();

// For the problematic item, we have two choices:
// Option A: Manually insert a newline in the string if the font supports it.
// This often requires adjusting the font size or using a smaller font for the second line.
// 
// Option B (Recommended): Use MultiCell specifically for this cell to handle overflow.
// Note that MultiCell will advance the y-coordinate after this call.
$pdf->MultiCell(20, 7, 'Hi5\n(xtra)', 1);

// If you want to continue with a standard Cell immediately after, 
// you must account for the fact that MultiCell has already moved the cursor down.
$pdf->Cell(20,7,'Hi5',1); // This will likely print on the line below (xtra) or push further down

Handling Layout Consistency

The tricky part with MultiCell is that it consumes vertical space dynamically. If you alternate between Cell and MultiCell, your grid alignment breaks because MultiCell might span two lines while a simple Cell spans one. To maintain the strict 20x7 grid shown in your first image, you generally cannot mix them seamlessly without calculating the exact height used by the wrapped text.

A more robust approach for maintaining a fixed grid is to manually break the string using a newline character and ensure the font size allows both lines to fit within the visual space, or accept that the row height will increase. If you strictly need the (xtra) part on the second line within the same logical "row" height, you might need to reduce the font size for the overflow text or use MultiCell and then manually calculate the remaining vertical offset before drawing subsequent cells.

For modern applications, switching to a library like Laravel often simplifies this problem by providing built-in PDF generators that handle complex layouts and wrapping more gracefully than raw FPDF. However, if you are constrained to FPDF, manual string manipulation with \n inside MultiCell is the standard solution for forcing a wrap at a specific point.

Final Implementation Note

In your specific case, replacing 'Hi5(xtra)' with 'Hi5\n(xtra)' inside the MultiCell call will force the break. However, be aware that MultiCell adds border logic to every line it draws unless you specify otherwise in the border parameter. If your layout requires a single border around the whole content block rather than borders around each line of text, you must pass an empty string or 'LTRB' depending on your desired aesthetic.

Remember that FPDF is a stateful stream processor. Once MultiCell advances the y-coordinate, all subsequent drawing commands start from that new position. Always verify the resulting PDF coordinates if you are building complex tables to ensure no data is pushed off-page unintentionally.

Our network