Monday, July 5, 2010
We are using open source php class FPDF for creating pdf file for exporting reports such as employees timesheet details in tabular format.
The below sample code provided by fpdf.org is working fine if the the table cell is having content with small length. Otherwise the content of one cell will overlap with other cell content.
By reading this article I came to know that we can use Multicell for wrapping the text either implicitly reaching at right border or explicitly by using '\n'.
But, still I faced some issues as our requirement is somewhat different from the sample code mentioned in this article.
I have tried different ways to make the height of the multicell uniform for all columns in the table.
But it didn't help. I came to know that we can specify height only for the specific cell but not for the set of multiple cells used for creating one row of a table.
So, the border were not displaying correctly even when the text wrap was working correctly when using MultiCell.
So, I decided to use combination of both Cell and MultiCell.
i-e Use Cell for designing the table structure without putting any actual data. And, use MultiCell to put data with wrap feature.
In other words, combining both Cell and MultiCell to get benefit of both good alignment and word wrap feature.
First I called Cell without specifying any data for creating a table structure with borders.
Than Called SetXY for bringing back the cursor to the original position. And then called MultiCell for putting the data.
The code will look like this.
$yH=30; //height of the row
$this->Cell($w, $yH, "", 'LRB',0,'',$fill);
Find below the final code which can be used to create tabular pdf reports with proper alignment and with word wrap feature.
Limitation of this code is, we need to specify a fixed height ($yH) for the table row in this code.
It is not possible to specify this height value if you don't have any clue about length of the dynamically generated text.
In this case we need to add a piece of code to set the $yH value dynamically.
i-e The code should measure the height of the cell for each column using GetY() method. i-e we need to find the difference by calling the GetY() before and after calling the MultiCell method.
Then we need to find the largest $yH among all columns. This largest value should be used as $yH when actually creating the table.
i-e We need to use the code two times, one time for finding the appropriate height and another time for creating table using the found height.
If you know any simple way to achieve all these things you can share it thro' the comments.
The above sample code will create issue if the pdf content grows more than one page.
In this case we need to call addPage() for every page of the content.
In our settings, each page hold 7 rows of data. So I have updated the code to call addPage() on every 7th row using modulo operator. And, we need to call at end of the all rows also in addition to call it at every 7th row.
Now the code will look like this.
You can bookmark this blog for further reading, or you can subscribe to our blog feed.