FPDF MultiCell() in PHP for Wrapped Multi-line Text

FPDF MultiCell() displays text across one or more lines inside a fixed width. When the text reaches the available width, FPDF automatically wraps the remaining text onto the next line.

FPDF MultiCell width height border alignment and fill options

This makes MultiCell useful when the length of the text is not known in advance. A normal Cell() does not automatically wrap long text.

$pdf->MultiCell(width,line_height,text,border,align,fill);

FPDF MultiCell() Syntax and Parameters Top ↑

$pdf->MultiCell(w,h,txt,border,align,fill);
w
Width of the MultiCell.
h
Height of each line, not the total rendered height.
txt
Text displayed inside the MultiCell.
border
Border around all or selected sides.
align
Text alignment such as 'L', 'C', 'R' or 'J'.
fill
Use true to apply the current fill color.
Important: the h parameter is the height of each text line. If the text wraps to three lines with h=10, the rendered MultiCell will normally consume about 30 units vertically.

Difference between FPDF Cell() and MultiCell() Top ↑

Difference between FPDF Cell and MultiCell text wrapping

The following example displays similar text first with Cell() and then with MultiCell().

<?php
require 'fpdf.php';

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

$pdf->SetX(50);
$pdf->Cell(
    60,
    10,
    'This is Cell - Welcome to plus2net.com',
    1,
    1,
    'L',
    false
);

$pdf->Ln(20);
$pdf->SetX(50);

$pdf->MultiCell(
    60,
    10,
    'This is MultiCell - Welcome to plus2net.com',
    'LRTB',
    'L',
    false
);

$pdf->Output('I','my_file.pdf');
View Difference between MultiCell() and Cell()

Understanding the Height of MultiCell() Top ↑

The second parameter supplied to MultiCell() controls the height of each line.

$pdf->MultiCell(
    60,
    10,
    'Long text that may wrap to several lines',
    1,
    'L'
);

If the text occupies one line, the vertical space is approximately 10 units. If it wraps to two lines, the MultiCell consumes approximately 20 units. With three lines it consumes approximately 30 units.

Total vertical space = number of rendered lines x line height

This behavior is different from Cell(), where the text remains on a single line even when it is longer than the Cell width.

Add Borders to MultiCell() Top ↑

The border parameter can draw all sides or selected sides of the MultiCell.

  • 1 - border on all sides.
  • 0 - no border.
  • 'L' - left border only.
  • 'RT' - right and top borders.
  • 'BTL' - bottom, top and left borders.
  • 'LRTB' - all four sides.
$pdf->MultiCell(
    100,
    10,
    'Text with selected borders',
    'LRT',
    'L'
);
View MultiCell Border Options

FPDF MultiCell Text Alignment Top ↑

MultiCell supports the following common text alignment values:

  • 'L' - left alignment.
  • 'C' - center alignment.
  • 'R' - right alignment.
  • 'J' - justified text.
<?php
require 'fpdf.php';

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

$pdf->SetX(50);
$pdf->MultiCell(100,10,'Alignment = L',1,'L',false);

$pdf->Ln(10);
$pdf->SetX(50);
$pdf->MultiCell(100,10,'Alignment = R',1,'R',false);

$pdf->Ln(10);
$pdf->SetX(50);
$pdf->MultiCell(100,10,'Alignment = C',1,'C',false);

$pdf->Ln(10);
$pdf->SetX(50);
$pdf->MultiCell(
    100,
    10,
    'Longer text is useful when demonstrating justified alignment inside an FPDF MultiCell.',
    1,
    'J',
    false
);

$pdf->Output('I','my_file.pdf');
View MultiCell Alignment Options

Add Background Fill Color to MultiCell() Top ↑

Use SetFillColor() to define the RGB background color and pass true as the final MultiCell parameter.

<?php
require 'fpdf.php';

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

$pdf->SetX(50);
$pdf->MultiCell(
    100,
    10,
    'Fill value is false',
    1,
    'C',
    false
);

$pdf->Ln(10);
$pdf->SetFillColor(1,255,255);
$pdf->SetX(50);
$pdf->MultiCell(
    100,
    10,
    'SetFillColor(1,255,255)',
    1,
    'C',
    true
);

$pdf->Ln(10);
$pdf->SetFillColor(255,255,1);
$pdf->SetX(50);
$pdf->MultiCell(
    100,
    10,
    'SetFillColor(255,255,1)',
    1,
    'C',
    true
);

$pdf->Ln(10);
$pdf->SetFillColor(255,1,255);
$pdf->SetX(50);
$pdf->MultiCell(
    100,
    10,
    'SetFillColor(255,1,255)',
    1,
    'C',
    true
);

$pdf->Output('I','my_file.pdf');
View MultiCell Fill Colors

X and Y Position after MultiCell() Top ↑

MultiCell changes the current PDF cursor position differently from Cell.

  • The Y coordinate moves down according to the number of lines rendered.
  • After MultiCell finishes, the X coordinate returns to the current left margin.
  • Use SetX(), SetXY() or margins when you need to position the next block manually.
FPDF MultiCell X and Y cursor positions

The following example reads the current coordinates using GetX() and GetY().

<?php
require 'fpdf.php';

$pdf=new FPDF();

$pdf->SetLeftMargin(50);
$pdf->SetTopMargin(30);
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);

$line_height=15;
$width=100;

for($i=1;$i<=3;$i++){
    $x=$pdf->GetX();
    $y=$pdf->GetY();

    $pdf->MultiCell(
        $width,
        $line_height,
        'Before MultiCell: X='.round($x).', Y='.round($y),
        'LRTB',
        'L',
        false
    );
}

$x=$pdf->GetX();
$y=$pdf->GetY();

$pdf->MultiCell(
    $width,
    $line_height,
    'X='.round($x).', Y='.round($y).
    ' - Extra text is added here so the content wraps onto another line and increases the final Y position.',
    'LRTB',
    'L',
    false
);

$pdf->Output('I','my_file.pdf');

We use the Plus2net PHP round() tutorial here to display shorter coordinate values.

View MultiCell X and Y Position Demo

Set Font Size relative to MultiCell Line Height Top ↑

FPDF does not automatically calculate the font size from the MultiCell height. However, for a controlled layout we can calculate a font size from the chosen line height.

$line_height=15;
$font_size=$line_height*0.75;

$pdf->SetFont(
    'Arial',
    'B',
    $font_size
);
This is a custom sizing rule used by the example. The value 0.75 is not an FPDF requirement and may need adjustment for different fonts, widths and content.

A complete demonstration:

<?php
require 'fpdf.php';

$pdf=new FPDF();
$pdf->SetLeftMargin(50);
$pdf->SetTopMargin(30);
$pdf->AddPage();

$width=100;

$line_height=35;
$font_size=0.75*$line_height;
$pdf->SetFont('Arial','B',$font_size);
$pdf->MultiCell(
    $width,
    $line_height,
    'line height='.$line_height.', font size='.$font_size,
    'LRTB',
    'L',
    false
);

$line_height=25;
$font_size=0.75*$line_height;
$pdf->SetFont('Arial','B',$font_size);
$pdf->MultiCell(
    $width,
    $line_height,
    'line height='.$line_height.', font size='.$font_size,
    'LRTB',
    'L',
    false
);

$line_height=10;
$font_size=0.75*$line_height;
$pdf->SetFont('Arial','B',$font_size);
$pdf->MultiCell(
    $width,
    $line_height,
    'line height='.$line_height.', font size='.$font_size,
    'LRTB',
    'L',
    false
);

$pdf->Output('I','my_file.pdf');
Demo: Font Size based on MultiCell Line Height

Estimate Font Size from Text Length Top ↑

The existing Plus2net example also demonstrates a simple calculation based on text length, width and line height:

$cell_data="Welcome to plus2net, PHP section,
read more on how to generate PDF documents here";

$chars_no=strlen($cell_data);

$font_size=0.75*($width/$chars_no)*($line_height/5);

$pdf->SetFont('Arial','B',$font_size);

$pdf->MultiCell(
    $width,
    $line_height,
    $cell_data,
    'LRTB',
    'L',
    false
);
Character count is only a rough sizing method because different characters have different printed widths. For precise layouts, the actual rendered text width and selected font should be considered rather than relying only on strlen().
Demo: Font Size based on Text Length

FPDF MultiCell() Video Tutorial Top ↑

MultiCell in FPDF to add multi-line text with automatic word wrapping

Common Problems with FPDF MultiCell() Top ↑

Text still appears on only one line Top ↑

The text will wrap only when it requires more width than the MultiCell provides. Reduce the width or use a longer string to demonstrate wrapping.

The next element starts at the left margin Top ↑

This is normal MultiCell behavior. After MultiCell finishes, X returns to the current left margin. Use SetX() or SetXY() before drawing the next element if another horizontal position is required.

The MultiCell is taller than expected Top ↑

The height argument is the height of each line. When text wraps onto several lines, the total vertical space increases accordingly.

The background color is not displayed Top ↑

Call SetFillColor() and set the MultiCell fill argument to true.

Justified alignment is difficult to see Top ↑

Justification is most visible when the text wraps across multiple lines. A short single-line string does not demonstrate it well.

Font size calculated from strlen() is inaccurate Top ↑

strlen() counts characters but does not measure their rendered width. Characters such as W and i occupy different amounts of space, so character-count formulas should be treated as approximations.

Cell() Draw Lines Add Images Create PDF Tables

Database Records to PDF Table

Frequently Asked Questions Top ↑

Q1: What is MultiCell() in FPDF?

MultiCell() displays text inside a fixed width and automatically wraps long content onto additional lines.

Q2: What is the difference between Cell() and MultiCell()?

Cell() is mainly used for single-line content, while MultiCell() can automatically wrap longer text over multiple lines.

Q3: Does the height parameter represent the total MultiCell height?

No. The height parameter specifies the height of each rendered line. The total vertical space increases when the text wraps onto additional lines.

Q4: How do I add borders to an FPDF MultiCell?

Use 1 for a border on all sides or use letters such as L, R, T and B to select individual sides.

Q5: How do I change the alignment of text in MultiCell()?

Use L for left, C for center, R for right or J for justified alignment.

Q6: How do I add a background color to MultiCell()?

Set the color with SetFillColor() and pass true as the fill parameter of MultiCell().

Q7: What happens to the X and Y coordinates after MultiCell()?

Y moves down according to the number of rendered lines, and X returns to the current left margin after the MultiCell finishes.


Cell() Drawing Line in PDF Adding Image to PDF


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer