Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPDF - Is there a way to adjust single table row height?

Tags:

html

css

php

tcpdf

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?

like image 204
Adam Baranyai Avatar asked Oct 10 '13 11:10

Adam Baranyai


People also ask

How do I increase the height of a row in a table?

Change row height To set the row height to a specific measurement, click a cell in the row that you want to resize. On the Layout tab, in the Cell Size group, click in the Table Row Height box, and then specify the height you want. To use the ruler, select a cell in the table, and then drag the markers on the ruler.

How do you fix the height of a row?

Set a row to a specific height Select the row or rows that you want to change. On the Home tab, in the Cells group, click Format. Under Cell Size, click Row Height. In the Row height box, type the value that you want, and then click OK.

How do you set a fixed height table?

The height of rows 'tr' in a table can be fixed very easily. This can be done by adding the height attribute in the tr tag. If the height is not specified, the height of the row changes according to the content. The height can be specified either in pixels, or percentage.

How do I adjust row height in Confluence table?

Row height in Confluence tables is responsive. Or in other words, it automatically adjusts to the content you add to it. So if you paste or insert a logo in a row, the row height should automatically adjust to that. You can resize the image afterwards to your liking as well and the row height will follow.


1 Answers

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: Visual example of set line-heights.

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.

like image 137
EPB Avatar answered Oct 16 '22 08:10

EPB