Programming, tutorials, mechatronics, operating systems, and other tech stuff

PHP FPDF Use text wrapping in table with MultiCell Table

17 comments
When we make a table in pdf using PHP FPDF library, basically we build it individually cell by cell. It is of course not as convenience as making a table using xml language such as HTML table. And the major drawbacks of using Cells is it's not able to wrap or break text as it overflow the defined cell width.

MultiCell

However, there is a method in FPDF library called MultiCell which allows us to make a Cell that wraps it's content, in short, it will break the text and make a new line when the text exceed it's width, thus multiplies it's height accordingly. But it doesn't works the same way with Cell, especially the positioning as the height varies according to it's content.

To make a table using MultiCell, we need to find out how much lines needed in a row as every cell in a row should have same height. And then we need to define where to put next cell, we can do this by calculating current cell position and offset it with it's dimension.

Don't want the math? FPDF MultiCell Table Class to the rescue.

FPDF Founder, Olivier Plathey have provided a FPDF extension class to make table from MultiCell named PDF_MC_Table. You can copy it from here :
https://github.com/gemul/fpdf-multicell-table/blob/master/pdf_mc_table.php

To use it, change the main fpdf.php file path to your FPDF library directory.
<?php
//call main fpdf file
require('fpdf18/fpdf.php');
//create new class extending fpdf class
class PDF_MC_Table extends FPDF {
...

Then in your script, include the pdf_mc_table.php file, not fpdf.php file.

<?php
//include pdf_mc_table.php, not fpdf17/fpdf.php
include('pdf_mc_table.php');
...

Next, create a new object from PDF_MC_Table Class.

...
//make new object
$pdf = new PDF_MC_Table();
...

From this point onward, you can use $pdf to build your pdf structure as usual. It doesn't change any functionaly of FPDF but instead it does add new ones.

Build the pdf table with MultiCell.

Like usual, you can add pages, use cell to build your content, and finally call Output() to render your pdf. Please read Generate Printable Invoice in PHP using FPDF library for more information.
And when it comes to make a multicell table, you need to:
  1. Set Font

    This is important to calculate length of the text.
    ...
    //add page, set font
    $pdf->AddPage();
    $pdf->SetFont('Arial','',14);
    ...
    
  2. Define the columns width.

    The column width is defined in array manner using SetWidths() method. The number of element should match the number of column.
    ...
    //set width for each column (6 columns)
    $pdf->SetWidths(Array(20,40,40,30,20,40));
    ...
    
  3. Set the line height.

    Use SetLineHeight() to define line height. This height will be used globally in our table. What we need to define here is height of line, not the row height. For example, if we define the line height to 5, and the cell with greatest line number in a row require 3 lines, then the height of the entire row will be 5 * 3 = 15.
    ...
    //set line height. This is the height of each lines, not rows.
    $pdf->SetLineHeight(5);
    ...
    
  4. Set the columns alignment (optional).

    This is optional. The default alginment of the cell is Left. You can define column alignment using SetAligns() method in array manner, with 'L' or empty string for Left (default), 'R' for Right, 'C' for Center, 'J' for justified. Also note that even if you only want to change only one column, you need to define all of them because number of array elements should match the number of columns.
    For example, when we want to make second column right aligned and third column centered, we use:
    ...
    //set alignment
    $pdf->SetAligns(Array('','R','C','','',''));
    ...
    
  5. Make the table header with standard Cell.

    To make it look like a proper table, add a table heading using standard cell with same number of columns and same widths for each cell respectively. Make the font bold if you like, or change the fill color to distinct it from table body. In this example, i will only make it bold.
    ...
    
    //add table heading using standard cells
    //set font to bold
    $pdf->SetFont('Arial','B',14);
    $pdf->Cell(20,5,"ID",1,0);
    $pdf->Cell(40,5,"First Name",1,0);
    $pdf->Cell(40,5,"Last Name",1,0);
    $pdf->Cell(30,5,"Email",1,0);
    $pdf->Cell(20,5,"Gender",1,0);
    $pdf->Cell(40,5,"Address",1,0);
    //add a new line
    $pdf->Ln();
    //reset font
    $pdf->SetFont('Arial','',14);
    ...
    
    Add a new line after that and do not forget to change the font back to normal.
  6. Write each row with Row() method.

    Then write the data row by row with Row() method also in array manners. The class will take care of the math.
    ...
    //loop the data
    foreach($data as $item){
     //write data using Row() method containing array of values.
     $pdf->Row(Array(
      $item['id'],
      $item['first_name'],
      $item['last_name'],
      $item['email'],
      $item['gender'],
      $item['address'],
     ));
     
    }
    ...
    
That's it. And don't forget to call $pdf->Output(); method to see the result.

And always keep in mind to never output anything in your script, so no empty line before "<?php", no echo, var_dump(), print() whatsoever. Even an error report or php warning will break your pdf file thus make it a corrupted pdf.

Except when you want to debug your code, you can freely output anything but comment out $pdf->Output(); line to prevent pdf render.

Known limitations.

However, as far as i know, there are some limitation in this method which is:
  1. Borders.
    Because the border is drawn using Rect() behind the scene, you may need some tweaking if you wish to change its style, color or size.

Further explanation and tutorial video.

For further explanation on how this class works behind the scene, please watch my tutorial video on youtube here :
https://youtu.be/pELrw9P5ywM
Also check out my PHP PDF Video Tutorial playlist here:
https://www.youtube.com/playlist?list=PLHCU7DCP4jhxnxV4Ub4YDqWiZ061t183c

17 comments :


  1. Howdy, would you mind letting me know which web host you’re utilizing? I’ve loaded your blog in 3 completely different web browsers, and I must say this blog loads a lot quicker than most. Can you suggest a good internet hosting provider at a reasonable price?
    Best AWS Amazon Web Services Training in Chennai | Best AWS Training and Certification for Solution Architect in Chennai
    Amazon Web Services Training in Tambaram, Chennai|Best AWS Training in Tambaram, Chennai

    ReplyDelete
  2. This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
    Data science training in Bangalore | Data Science Training institute in Bangalore

    ReplyDelete
  3. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Selenium Training in Electronic City

    ReplyDelete
  4. Post is very useful. Thank you, this useful information.

    Learn Best Cognos Training in Bangalore from Experts. Softgen Infotech offers the Best Cognos Training in Bangalore.100% Placement Assistance, Live Classroom Sessions, Only Technical Profiles, 24x7 Lab Infrastructure Support.

    ReplyDelete
  5. Data Science is the future of Artificial Intelligence. Therefore, it is very important to understand what is Data Science and how can it add value to your business.
    Best Institute for Data Science in Mumbai which includes classroom and online training. Along with Classroom training, we also conduct online training using state-of-the-art technologies to ensure the wonderful experience of online interactive learning. Best Institute for Data Science

    ReplyDelete
  6. Data Science is the future of Artificial Intelligence. Therefore, it is very important to understand what is Data Science and how can it add value to your business.
    Data Science institute in Mumbai which includes classroom and online training. Along with Classroom training, we also conduct online training using state-of-the-art technologies to ensure the wonderful experience of online interactive learning. Data Science institute in Mumbai

    ReplyDelete
  7. Data Science is the future of Artificial Intelligence. Therefore, it is very important to understand what is Data Science and how can it add value to your business.
    Best institute for Data Science in Bangalore which includes classroom and online training. Along with Classroom training, we also conduct online training using state-of-the-art technologies to ensure the wonderful experience of online interactive learning. Best institute for Data Science in Bangalore

    ReplyDelete
  8. Nice blog,I understood the topic very clearly,And want to study more like this.
    Data Scientist Course

    ReplyDelete
  9. I really appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thx again!

    Selenium Training in Electronic City

    ReplyDelete
  10. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
    Microsoft Azure Training in Electronic City

    ReplyDelete