TCPDF - Is there a way to adjust single table row height?
What you're probably running into is the actual height of lines of text. Internally, TCPDF uses the cell height ratio to control the rendered line height. When you have a TD with a single line of text, the smallest you can make it is the line's total height. So the minimum size of a td
cell is fontsize * cellheightratio + any cellpadding proscribed
cellpadding can come from the cellpadding
attribute, so I set it to 0 for this example. I believe at least some of the padding dimensions can also be set with setCellPaddings
before writing the HTML.
You can set the cell height ratio by using a line-height
CSS declaration to make rows smaller. (You can also, of course, just reduce the font size as well.)
<?php
//For demonstration purposes, set line-height to be double the font size.
//You probably DON'T want to include this line unless you need really spaced
//out lines.
$this->setCellHeightRatio(2);
//Note that TCPDF will display whitespace from the beginning and ending
//of TD cells, at least as of version 5.9.206, so I removed it.
$html = <<<EOD
<table style="border:1px solid black;" border="1" cellpadding="0">
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr style="line-height: 100%;">
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
<tr style="line-height: 80%;">
<td>Row 3, Cell 1</td>
<td>Row 3, Cell 2</td>
</tr>
<tr style="line-height: 50%;">
<td>Row 4, Cell 1</td>
<td>Row 4, Cell 2</td>
</tr>
</table>
EOD;
$this->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
The above code on my 5.9.206 installation produces this:
This works out to row 1 being big, twice the font size. Row 2 sets the line-height to be 100% of the font size. Row 3 is 80%. Row 4 there is 50%.
*Note that if your text wraps, it'll look terrible at very reduced line-heights.
Adam Baranyai
Updated on July 24, 2022Comments
-
Adam Baranyai almost 2 years
I am trying for two days now, with no result, to adjust a single rows min-height in a table, with no success.
I am using the following method to create my table:
<?php $html = <<<EOD <table style="border:1px solid black;"> <tr> <td> Text 1 </td> <td> Text 2 </td> </tr> </table> EOD; $this->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true); ?>
I already tried setting td padding, td margin, td height, tr height, with no success. I tried these from CSS and HTML too. The only thing I managed to achieve, is to see a row's height larger then the original value, but I want to make it shorter. I tried searching in the documentation of TCPDF, but the only thing I found is that TCPDF is not supporting padding and margin. Do any of you know some kind of "hack" to achieve my desired result?
-
321zeno over 10 yearsThis is correct. And it applies to empty table cells as well.
-
gregthegeek about 10 yearsI ran into this as well except I was not using HTML cells, just the standard Cell method. I needed an exact height to get page breaking borders to work correctly and I spent hours scratching my head not knowing FontSize was changing my sizes, throwing off the breaks. Saving my original font size, then calling SetFontSize(0) before writing the Cell with a Ln(), then resetting font size back fixed it. Thanks for this!
-
wahmal almost 7 yearshow to add multiple $this->setCellHeightRatio(2); ?
-
amit_game almost 7 yearsWorking Awsome. Thanks