Line height in fpdf multicell

Line height in fpdf multicell

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

Stefan Bogdanescu

Founder & Senior Architect

Understanding Line Spacing in FPDF MultiCell

When generating PDF documents with FPDF, one of the most common frustrations involves controlling vertical spacing when using MultiCell. You might expect that simply calling the function multiple times or passing a newline character would result in lines appearing directly underneath each other. Instead, you often find yourself dealing with excessive whitespace between text blocks, making the layout look unprofessional or cluttered.

The core of this issue lies in understanding how FPDF calculates cell height. The MultiCell method does not just insert a newline character; it calculates a default line height based on the font size and the current settings. By default, if you set your font size to 12 points, FPDF will add significant padding above and below each line to ensure readability. This behavior is hardcoded to prevent text from running into itself when wrapping occurs.

To adjust this spacing, you need to look at the optional parameters of the MultiCell function. The signature for this method allows you to specify a custom line height as the third argument. When you define this value, FPDF uses it to determine the vertical distance between the baseline of one line and the next.

Consider a scenario where you are formatting an address block. You want the lines to be tightly packed without overlapping, yet distinct enough to remain legible. If your font size is set to 10 points, you might try setting the third parameter to something like 4 or 5. However, this value represents the total height of the cell in points. If you set it too low relative to the font size, text wrapping can cause characters to overlap vertically, ruining the layout.

Here is a practical implementation example where we initialize an FPDF instance and configure the address:

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Helvetica', '', 10); // Set font size to 10pt

// Define the address lines
$address = [
    '102 South Avenue',
    'Suite 107',
    'Scottsdale AZ 85260',
    '111-000-1111'
];

$x = 10; // Starting x position
$y = 10; // Starting y position

foreach ($address as $line) {
    // The third argument (4.5) sets the custom line height in points.
    // It must be greater than the font height to prevent overlap.
    $pdf->MultiCell(80, 4.5, $line, '', '', false, $x, $y);
}

In this example, we pass 4.5 as the third argument to MultiCell. This forces FPDF to use a cell height of 4.5 points for every line. Since the font size is 10 points, you must ensure that this custom height is slightly larger than the actual font height plus any necessary padding. If you set it to exactly the font size, lines might overlap when text wraps. A safe heuristic is often to use a value roughly equal to the font size or slightly less if you are certain no wrapping will occur within the column width.

One critical pitfall to avoid is confusing the SetLineHeight method (which exists in some forks like TCPDF but not standard FPDF) with the parameter passed directly to MultiCell. Standard FPDF does not have a standalone SetLineHeight function that persists globally for all cells. You must pass the height every time you call MultiCell, or wrap the logic in a helper class to maintain consistency across your document generation scripts.

Another consideration is text wrapping. If you define a narrow width (the first argument of MultiCell) and a short line height, long strings will wrap onto the next line immediately. If your custom line height is smaller than the font height, the wrapped text will sit on top of the previous line's characters, causing illegibility. Therefore, when experimenting with tighter spacing, always test with long strings to ensure the vertical gap accommodates the full font metrics.

For complex PDF generation needs involving dynamic layouts or extensive form fields, you might find that FPDF's limitations become a bottleneck. In such cases, exploring modern alternatives like Spatie's PDF generator or integrating libraries that build on top of FPDF can offer more granular control over spacing and layout without sacrificing the ease of use that makes FPDF popular in the PHP ecosystem. If you are building a larger application stack, resources like https://laravelcompany.com often cover integration patterns for generating invoices and reports, which might provide alternative approaches to text layout using frameworks designed for Laravel applications.

Our network