Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP TCPDF - How do I vertical align text in a table cell?

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?

like image 944
Kosta Kontos Avatar asked May 28 '12 07:05

Kosta Kontos


People also ask

How do I align text in a table?

Select the text and go to the Layout tab and the Alignment section of the ribbon. Choose “Align Center.” Your text will then be right in the middle of the cell. Centering the text in your Word table is a simple task whether horizontally, vertically, or both.

Which attribute is used for vertical alignment of text in cell of a table?

The HTML <td> valign Attribute is used to specify the vertical alignment of text content in a cell.

How do I align table contents in a cell?

To center align text in table cells, use the CSS property text-align. The <table> tag align attribute was used before, but HTML5 deprecated the attribute. Do not use it. So, use CSS to align text in table cells.

How do you vertically align text in a table cell?

To place an item at the top or bottom of its cell, insert the "VALIGN=" attribute within the code for that cell. To vertically align an entire row (e.g., placing all data in that row at the tops of the cells), insert the "VALIGN=" attribute within the code for that row.


2 Answers

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...

like image 63
Nawlbergs Avatar answered Oct 03 '22 08:10

Nawlbergs


Remember that TCPDF cannot handle full-blown HTML and CSS the way a browser can. Nicolas has done a good job of rendering the most-used HTML codes. CSS cannot be placed at the top of the document, it must be put inline.

Tables must be formatted completely manually--cells do not auto-size the way they do in a browser. Likewise, you must figure out how tall something is and add <BR>s or size an invisible image to push it down if you want.

Perhaps one day he will add more of this functionality. As it is, I find the class amazing.

like image 32
JayEdgar Avatar answered Oct 03 '22 06:10

JayEdgar