FPDF - Create Table with Multiline (Wrap Text in a Cell)


In this example, we will learn how to creates a table to display user information with support for multiline text in the address column.

Demo:Create a Multiline Text in Table Create a Table  with Multiline Text

The following example shows, how to create a multiline text in the tables in FPDF.

  • Include the FPDF library, PDF class extends the FPDF class, enabling custom methods for PDF generation.
  • Defines the width for each column: Name, Address, Email, and Age.
  • Loops through the data array to generate table rows.
  • Calls the string_2_multiline method to split the address into multiple lines if it exceeds the specified width (35 characters).
  • Adds additional lines for multiline addresses by leaving other columns blank.
  • Draws a horizontal line after each row.
  • Calls the body method to create the table with the provided data.
  • Sends the generated PDF to the browser for download or display.

Example
<?php
  require('fpdf/fpdf.php');

  class PDF extends FPDF
  {
    function body($data){
     
        $this->SetFont('Arial','B',12);
     
      #Each column width
      $width=[40,80,50,20];
      
      #Display Table Header Cell
      $this->Cell($width[0],7,"NAME",1,0);
      $this->Cell($width[1],7,"ADDRESS",1,0);
      $this->Cell($width[2],7,"EMAIL",1,0);
      $this->Cell($width[3],7,"AGE",1,0);
     
      $this->Ln();
     
      $this->SetFont('Arial','',12);
    
      foreach($data as $row){
        
        $this->Cell($width[0],7,$row["name"],"LR",0);
        
        #split address string into multi line
        $multi_line_text=$this->string_2_multiline($row["address"],35);
        
        #show first line of address
        $this->Cell($width[1],7,$multi_line_text[0],"R",0);        
        
        $this->Cell($width[2],7,$row["email"],"R",0);
        $this->Cell($width[3],7,$row["age"],"R",0);
        $this->Ln();
        
        
        #if address lines more then 1
        for($x=1;$x<count($multi_line_text);$x++){
            
            $this->Cell($width[0],7,"","LR",0);
            $this->Cell($width[1],7,$multi_line_text[$x],"R",0);        
            $this->Cell($width[2],7,"","R",0);
            $this->Cell($width[3],7,"","R",0);
            $this->Ln();
        }
        $this->Line(10, $this->GetY(), 200, $this->GetY());  
      }  
        
    }
    # Multiline Handling function
    function string_2_multiline($text,$maxStringLength){
        
        $words=explode(" ",$text);
        
        $rows=[];
        $i=0;
        $len=0;
        foreach($words as $row)
        {
            $len+=strlen($row);
            if($len<=$maxStringLength)
            {
                if(!isset($rows[$i])){
                    $rows[$i]="";
                }
                
                $rows[$i].=$row." ";
                $len++;
            }else
            {
                $i++;
                $len=strlen($row);
                $rows[$i]=$row." ";
            }
        }
        return $rows;
    }

  }

  //create an FPDF object
  $pdf=new PDF();
  
  //Add Blank Page
  $pdf->AddPage();
  
 //Table Rows
  $data=[
    [
        "name"=>"Ram",
        "address"=>"No 12,Car street 2nd cross,New Bus stand,salem-8. ",
        "email"=>"ram@gmail.com",
        "age"=>25
    ],
    [
        "name"=>"saravanan",
        "address"=>"New Bus stand Backside,salem-8. ",
        "email"=>"ram@gmail.com",
        "age"=>22
    ],
    [
        "name"=>"Sam",
        "address"=>"No 62,Karmegam street,Near Ammapet Police station,salem-4.",
        "email"=>"ram@gmail.com",
        "age"=>24
    ],
    [
        "name"=>"Kumar",
        "address"=>"Omalur main road,salem.",
        "email"=>"kumar@gmail.com",
        "age"=>30
    ],[
        "name"=>"Raj",
        "address"=>"No 90,Siddhar kovil Theru,Illampillai main road,Sivathapuram.salem-12",
        "email"=>"ram@gmail.com",
        "age"=>28
    ],
  ];
  
  // get datas
  $pdf->body($data);
  
  // Close document and sent to the browser
  $pdf->Output();
?>