MultiCell to display text with line breaks

Syntax of MultiCell with different options to print the string with line breaks.
MultiCell options
Text string wrap into next line after reaching the border of the MultiCell. In case of Cell the text crosses and flows out of the Cell border. MultiCell is used when we are not sure about the length of the string to display.

Read More on Cell


MultiCell in FPDF PDF generator to add multiline text with auto ward wrap and increase the height

MultiCell & Cell difference

Here is the code to display difference between MultiCell and Cell
<?Php  
require('fpdf.php');  
$pdf = new FPDF();  
$pdf->AddPage();  
$pdf->SetFont('Arial','B',16);  

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

$pdf->Ln(40); // Line gap  
$pdf->SetX(50); // Set Horizontal position  

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

$pdf->Output('my_file.pdf','I'); // Send to browser and display  
?>

Difference between MultiCell and Cell in PDF

MultiCell Border

We can add borders to different sides of a MultiCell by specifying the sides.

  • 1: All borders
  • 0: No border
  • L: Only Left border
  • RT: Only Right and Top border
  • BTL: Only Bottom, Top, and Left border

You can continue with other combinations of the borders.

Check the border option value with different sides to add border.

MultiCell Text Alignment

Below are the different text alignment options available in MultiCell:

  • L: Left alignment
  • C: Center alignment
  • R: Right alignment
  • J: Justified alignment (Default value)
<?Php  
require('fpdf.php');  
$pdf = new FPDF();  
$pdf->AddPage();  
$pdf->SetFont('Arial','B',16);  

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

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

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

$pdf->Ln(20);  
$pdf->SetX(50);  
$pdf->MultiCell(100,10,'Demo About MultiCell Alignment = J',1,'J',false);  

$pdf->Output('my_file.pdf','I'); // Send to browser and display  
?>

Check the alignment option of text inside MultiCell with different values.

MultiCell and fill

We can fill the MultiCell by using background colour ( value = true ) or keep it transparent ( value= false ) . Default value is false.
<?Php  
require('fpdf.php');  
$pdf = new FPDF();  
$pdf->AddPage();  
$pdf->SetFont('Arial','B',16);  

$pdf->SetX(50);  
$pdf->MultiCell(100,10,'SetFillColor is not set, value = false',1,'J',false);  

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

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

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

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

fill option with different value of SetFillColor for MultiCell

Position of X and Y in MulitCell

Value of abscissa (Horizontal x position) does not change after MultiCell is displayed, but value of ordinate ( vertical y position ) by default increases by assigned height of the MultiCell. If the MultiCell height increases due to text wrap then Y position also increases.
MultiCell position
We can control the starting point of MultiCell by using top margin and left margin. In the example below we have displayed the X and Y position by reading the value using GetX and GetY functions.
We have used PHP Math function round() to get rounded value of X and Y coordinates.
<?Php  
require('fpdf.php');  
$pdf = new FPDF();  

$pdf->SetLeftMargin(50); // Set left margin before adding a page  
$pdf->SetTopMargin(30); // Set top margin before adding a page  
$pdf->AddPage();  
$pdf->SetFont('Arial','B',16);  

$h = 15; // Default height of each MultiCell  
$w = 100; // Width of each MultiCell  

$y = $pdf->GetY(); // Get current Y (vertical position)  
$x = $pdf->GetX(); // Get current X (horizontal position)  

$pdf->MultiCell($w,$h,  'X='.round($x).', Y='.round($y),  'LRTB','L',false);  

$y = $pdf->GetY();  
$x = $pdf->GetX();  
$pdf->MultiCell($w,$h,  'X='.round($x).', Y='.round($y),  'LRTB','L',false);  

$y = $pdf->GetY();  
$x = $pdf->GetX();  
$pdf->MultiCell($w,$h,  'X='.round($x).', Y='.round($y),  'LRTB','L',false);  

$y = $pdf->GetY();  
$x = $pdf->GetX();  
$pdf->MultiCell($w,$h,  'X='.round($x).', Y='.round($y).  ' ( Added more text inside MultiCell for text to Wrap around )',  'LRTB','L',false);  

$y = $pdf->GetY();  
$x = $pdf->GetX();  
$pdf->MultiCell($w,$h,  'X='.round($x).', Y='.round($y),  'LRTB','L',false);  

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

Position of X & Y Coordinates for MultiCell

Adjusting text size with height of the autoCell

In above code we have set the font size to a fixed value by using $pdf->SetFont('Arial','B',16); , we can change this fixed height 16 to make it 75% of the height of the autoCell by using the variable $h as parameter while using SetFont(). Here we have kept it as 0.75*$h , you can change this to any value. Now the font size will increase or decrese based on the default height of the multiCell.
$h = 15; // Default height of each MultiCell  
$w = 100; // Width of each MultiCell  
$f = $h * 0.75; // Auto text size based on height  

$pdf->SetFont('Arial', 'B', $f);  
Full code is below and demo for auto text size in PDF autoCell is here.
<?Php  
require('fpdf.php');  
$pdf = new FPDF();  

$pdf->SetLeftMargin(50); // Set left margin before adding a page  
$pdf->SetTopMargin(30); // Set top margin before adding a page  
$pdf->AddPage();  

$h = 35; // Default height of each MultiCell  
$w = 100; // Width of each MultiCell  
$f = 0.75 * $h; // Auto text size  

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

$y = $pdf->GetY(); // Get current Y (vertical position)  
$x = $pdf->GetX(); // Get current X (horizontal position)  

$pdf->MultiCell($w,$h, '$h='.$h.',$f='.$f", 'LRTB','L',false);  

$h = 25;  
$f = 0.75 * $h;  
$pdf->SetFont('Arial','B',$f);  
$pdf->MultiCell($w,$h, '$h='.$h.',$f='.$f", 'LRTB','L',false);  

$h = 10;  
$f = 0.75 * $h;  
$pdf->SetFont('Arial','B',$f);  
$pdf->MultiCell($w,$h,  '$h='.$h.',$f='.$f", 'LRTB','L',false);  

$pdf->Output('my_file.pdf','I');  
?>
Based on the length of the text inside the multiCell we can change the size of the font matching to width and height of the multiCell.
$cell_data = "Welcome to plus2net, PHP section,  
read more on how to generate pdf documents here";  

$chars_no = strlen($cell_data);  

$f = 0.75 * ($w / $chars_no) * ($h / 5);  
// Auto text size based on width, character count, and height  

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

$pdf->MultiCell($w, $h, $cell_data, 'LRTB', 'L', false);  

Questions



Cell demo for auto text size based on length of the string in PDF autoCell
Drawing Line in PDF
Adding Image to PDF File
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



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-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer