FPDF PHP PDF Generator: How to Wrap Long Lines of Text in Tables

I have mentioned the useful FPDF PHP PDF generating library before, but today I’m quickly going to point out how you can solve the problem of inserting extra long lines of text into a table and forcing the table to automatically wrap or linebreak all of your text without simply chopping it off at the end when it reaches the right hand border of the cell.

The solution to this is pretty simple and it involves dropping the use of the Cell() function and instead incorporating the MultiCell() call.

The MultiCell basically allows for the printing of text with line breaks. These line breaks can be automatic, in other words when the text hits the right border of the cell, or explicit, i.e. via the \n control character.

The function cleverly automatically lays down as many cells as possible, one below the other and all without any input or direction from you. The cell block can then be frames and even have its background painted if you want.

Parameters are pretty self-explanatory. You can set the width of the cells, the height of the cells, the text to print, whether or not you want a border, how you want the text aligned and whether or not you want the background filled.

Putting this in code, you would get a call that looks something like this:

$pdf->MultiCell(275,7,’Insert your long string here’,1,’L',false);

Nifty.

Related Link: http://www.fpdf.org/en/doc/multicell.htm

You might also enjoy:

  • My tried and trusted method for inserting graphs into PDFs is to use the nifty PHP FPDF library and insert an image into it via a Google Chart URL. In ot ...
  • PHPExcel is a phenomenal Excel generating library for PHP which I have mentioned on this site numerous times before, and which is currently my defacto way f ...
  • The CodePlex project PHPExcel is an absolutely brilliant implementation around Microsoft's OpenXML standard, giving us a powerful PHP object driven engine t ...
  • Using jQuery to highlight a table row on mouse over is pretty simple to achieve, and today I'll quickly demonstrate how you can achieve this neat effect usi ...
  • Sometimes you need to RGB array that makes up a color when working in the wonderful world of PHP. Don't ask me why or when, though that said, I have require ...

About Craig Lotter

Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under the Funakoshi karate style and for the most part, simply enjoys life with his amazing wife and daughter. Oh, and he draws ever now and then too.
This entry was posted in Technology & Code, Tutorials and tagged , line break, multicell, , text wrap, wrap. Bookmark the permalink.
  • Donsevcik

    How we do a full row in a table with multiple cells which ALL have wrap text. It seems every example out there only allows the first cell in a row to be wrapped?

blog comments powered by Disqus