Wrapping text in FPDF table cells

This article aims to help those who like myself wondered how to make text wrap within a table cell using FPDF to generate a the PDF. A few posts around the web seem to have missed the point that wrapping comes out of the box in FPDF using the MultiCell function. However, it’s just a tiny bit tricky but not prohibitively so. So, this is what we are aiming for – an invoice.

FPDF Invoice

There are 2 examples of wrapping here. Firstly the client address is wrapped, and secondly the first row of the line item data has a wrapped description.

In both cases the MultiCell in-built function is used. Before I had this solution working I was using the Cell function. In that case, long text simply continued through to the next cell ‘Quantity’. In the case of Cell, you can simply output a series of calls to the function with a cell width and so fourth and the cells will line up against each other on the same line.

When I switched to MultiCell for the first cell of each row only, I found that it would cause all subsequent cells in the same row to wrap to the next line. Therefore my solution was to use the SetXY function to reposition the ‘cursor’ back to where the 2nd cell of the current row would normally be, and then proceed to use Cell calls.

In order to do that it’s a case of marking the current X and Y coordinates of the cursor prior to entering the line item loop and calling SetXY just after the 1st cell MultiCell function call. That solved the line wrapping issue and my text wrapped inside the first cell – great.

The second issue however is that the MultiCell is arbitrarily high depending on the amount of text wrapping going on. I had been provided a static height to my Cell calls as as such they were now out of line with the first column, that is, the text and border lines were all out of sync with the 1st column.

To solve that problem it was a case of grabbing the Y coordinate of the cursor before and immediately after the MultiCell call, finding the difference to ascertain the height of the cell, and then using this to size the height of the remaining Cell calls on the row.

See it in action

Source Code

Please feel free to download the source code for this article which builds the invoice PDFs as described.

The source code is provided as a ZIP file with index.php for the mixed example and index-all-mc.php for all Multi-cell with the 2 classes that build the PDFs. You will need to obtain the FPDF library yourself and modify the FPDF_FONTPATH and fpdf.php require. By default the code looks for fpdf17 in the same folder as the index files.

Best of luck!

19 thoughts on “Wrapping text in FPDF table cells”

  1. Hi can you please make this example applicable where all the columns are MultiCell i tried adapting your code but couldnt make it work, Please if you can do this, I am so stuck at this point.

      1. Hi , I am having problem to display text in multiple line with in a column in pdf report table , How can I display this according to column width and text will go to next line if width exceed .

  2. Hi can you please make this example applicable where all the columns are
    MultiCell i tried adapting your code but couldnt make it work, Please
    if you can do this…….

  3. if i use above code an not getting proper output, it is displaying  
    Output(); ?> ..what i want to do to get output

      1. There is nothing wrong with the code Arun. I’ve just installed it from the download I provide here: http://www.adcworks.com/code/fpdf/invoice-pdf/

  4. Hi can you please make this example applicable where the description cell is on middle column, not on the first column? I need to put line border on the cell. And The height of the columns before the description cell are shorter and the after the description cell and longer so it looks ugly.

    1. Download the new code and look at index-all-mc.php. You just need to set $col_widths, $headers and $rows in the order you want. 

  5. Hi, thanks for your sharing. However, I found out in your ‘InvoiceAllMCPDF.class.php’ the call of MultiCell does not pass in the $rowHeight but $row_line_heights[$r] and caused the next MultiCell print at incorrect XY. Please have a look, thanks.

  6. Hi, it works fine. But here only first column of the table have data wrapping. what if i want data wrapping in column 3 or 4? any suggestions?

  7. awesome tutorial, for 3 or 4 columns only select the longest string before do calculation for the height.

  8. Great and useful tutorial, but as far as I can see, your all multi-cell table example assumes that you have the same number of rows per cell for each row of the table (=2 in your example).

    Also as far as I can see the mixed multi-cell and cell table assumes that the first column is the one with multi-cell or at least assumes that the first column is the one with the tallest cells.

    Is it possible to modify this for the general case, where I don’t know beforehand how much text I have?

Comments are closed.