PHP TCPDF – How to vertical align text in a table cell

PHPtcpdfvertical-alignment

By default, the values within tables cells (within tables generated by TCPDF) are vertically aligned right at the top of the cell.

Has anyone found a simple method for vertically aligning text to the middle of the cell?

I have found a couple of proposed solutions online but these don't strike me as ideal.

For example one suggestion (http://sourceforge.net/projects/tcpdf/forums/forum/435311/topic/4385696) is to set the contents of each cell using TCPDF's MultiCell() method, but this is a pain when you simply want to write out your HTML code and then generate the PDF.

Another suggestion (http://bytethinker.com/blog/tcpdf-and-vertical-alignment-of-table-cells) is to place spans within each cell and create blank lines with line breaks in these spans in order to force your text down (and thus towards the vertical centre), but this is a bit of a hack.

Has anyone found a better / cleaner way to achieve this? Surely a library this popular would cater for such a common requirement?

Best Answer

If its a static table... you can add invisible text above the contents to push the text down:

<td width="12%" style="text-align:center">
  <div style="font-size:10pt">&nbsp;</div>
  <b>The contents of my cell...</b>
</td>

Adjust the font-size of the &nbsp; to raise or lower the text... Its a hack, but it does work. <br />'s work too... but they will be less accurate...