FPDF MultiCell prints \n as text, shows <br> tags or pushes the next cell down
Fix it fast
Most common cause, 1 of 3The \n is written in single quotes or stored as two characters
Write the literal in double quotes, or turn stored backslash-n pairs into real newlines before printing.
<?php
$pdf->MultiCell(0, 6, "Line 1\nLine 2");
// text that arrived with backslash-n pairs
$txt = str_replace('\n', "\n", $txt);
$pdf->MultiCell(0, 6, $txt);Short answer
FPDF's MultiCell breaks lines only at a real newline or at the cell's right edge. A \n inside single quotes is two characters, HTML <br> tags print as text, and after MultiCell the cursor returns to the left margin, so the next Cell drops a line. Use "\n", convert <br>, and restore the position with SetXY. Applies to FPDF 1.86-1.9.
Which cause do you have?
Count what the text really contains just before it reaches MultiCell. A 0 for real newlines with a non-zero count of backslash-n pairs is cause 1. A br match is cause 2. If the breaks print correctly but the row falls apart, it is cause 3.
var_dump(
substr_count($txt, "\n"), // real newline characters
substr_count($txt, '\n'), // a backslash followed by n
preg_match('/<br\s*\/?>/i', $txt) // HTML line breaks
);Then find the line you see in this list and open its fix:
- 1The \n is written in single quotes or stored as two characters
The PDF shows the letters \n between the lines, and the first step counts 0 real newlines - 2The text carries HTML line breaks
Tags such as <br>, <br /> or <p> are printed in the PDF; the text came from a rich-text editor or went through nl2br() - 3MultiCell moves the cursor to the left margin of the next line
Line breaks look right inside the block, but the cell meant to sit beside it starts below it at the left margin
What does the error look like?
MultiCell prints Line 1\nLine 2 on one line<br /> appears as text in the PDFThe next Cell after MultiCell starts on a new line instead of beside it
Cause 1 of 3
The \n is written in single quotes or stored as two characters
You seeThe PDF shows the letters \n between the lines, and the first step counts 0 real newlines
Check
MultiCell ignores a \n that sits inside single quotes, because PHP does not expand escape sequences there: the string holds a backslash and the letter n. The FPDF manual says MultiCell breaks lines explicitly only at the \n character itself. Text saved by an API or a form as backslash-n pairs fails in the same way.
grep -rnF '\n' src/ | grep "'"Fix
Write the literal in double quotes, or turn stored backslash-n pairs into real newlines before printing.
<?php
$pdf->MultiCell(0, 6, "Line 1\nLine 2");
// text that arrived with backslash-n pairs
$txt = str_replace('\n', "\n", $txt);
$pdf->MultiCell(0, 6, $txt);Verify
The first step now reports at least one real newline, and the PDF shows two separate lines.
var_dump(substr_count($txt, "\n"));Confirmed by php.net
Cause 2 of 3
The text carries HTML line breaks
You seeTags such as <br>, <br /> or <p> are printed in the PDF; the text came from a rich-text editor or went through nl2br()
Check
MultiCell has no HTML support, so a <br> from an editor or from nl2br() is printed as plain text. nl2br() inserts <br /> in front of each newline and keeps the newline, so its output breaks the line and also prints the tag next to it.
grep -rn "nl2br(" src/Fix
Pass the text from before nl2br() if you have it. Otherwise turn each br into a newline and strip the rest of the markup.
<?php
// \R also removes the newline that nl2br() left after each tag
$plain = strip_tags(preg_replace('/<br\s*\/?>\R?/i', "\n", $html));
$pdf->MultiCell(0, 6, $plain);Verify
No tag text is visible in the PDF, and each former <br> starts a new line.
var_dump(preg_match('/<br\s*\/?>/i', $plain)); // int(0)Confirmed by php.net
Cause 3 of 3
MultiCell moves the cursor to the left margin of the next line
You seeLine breaks look right inside the block, but the cell meant to sit beside it starts below it at the left margin
Check
A table row breaks after MultiCell because FPDF draws each line of the block as a cell that moves below, then sets the position back to the left margin. That differs from Cell, whose ln parameter lets the position stay on the right. A Cell call that follows therefore starts under the block.
grep -rn -A2 "MultiCell(" src/Fix
Save the position before MultiCell, measure the height it used, then put the cursor back to the right of the block and draw the other cells with that height.
<?php
$x = $pdf->GetX();
$y = $pdf->GetY();
$pdf->MultiCell(80, 6, $description, 1);
$rowHeight = $pdf->GetY() - $y;
$pdf->SetXY($x + 80, $y);
$pdf->Cell(30, $rowHeight, $quantity, 1, 0, 'C');
$pdf->Cell(30, $rowHeight, $price, 1, 1, 'R');Verify
The quantity and price cells sit beside the description, their borders match its height, and the next row starts below all three.
var_dump($rowHeight);Confirmed by raw.githubusercontent.com
How do I stop it coming back?
- Normalise text once, where it enters the PDF code: strip HTML, convert br to newline, and turn escaped pairs into real newlines.
- Wrap the MultiCell-plus-cells pattern in one helper that returns the row height, so every table row uses the same SetXY logic.
- Keep a small sample PDF in the test suite with a multi-line description, and compare its page count or a text dump after FPDF upgrades.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: Spotless Laundry, EZ Estimater.