TCPDF and Vertical Alignment of Table Cells

If you landed here, you already know what is the issue. You want a table cell aligned to the middle in TCPDF but the TD is not taking any of the parameters and keeps as it is: aligned to the top. I've seen this topic from 2008 on in the TCPDF forums and did not find a solution so far.

In the last days I had some time to work on it. The only thing I got working is a combination of counting table rows and an theoretically not but practically empty span with modified font size:

if ($rows!=0) {
        $innertable .= "\n".'<tr><td rowspan="'.$rows.'">';
        $innertable .= '<span style="font-size: 25px;">'.str_repeat('&nbsp;<br/>', $rows-1).'</span>';
        $innertable .= $cat['Category']['name'].'</td>';

That is just an example of how I am doing it at my current project. The trick is to add blank lines and to prevent them from beeing dropped, when TCPDF parses the table cells. The important part is only the middle line of my example. I cant really tell why (and I have played around a lot with it), but my "two lines" is exactly 50 pixels high. My font size defined in TCPDF is 16.


So: if you have a font of 16, 25 is the font size of the span you add. Then, add a no breaking space plus a html newline for each but one row of your tablecell. If you want to center a td with a rowspan of 4, write something like this:

$innertable .= '<td rowspan="4"><span style="font-size: 25px;">'.str_repeat('&nbsp;<br/>', 3).'</span></td>';

For me, it looks like this now:

Alignment of table cells in TCPDF

I hope this will help some of you.

Cheers, Guido