TCPDF - 有没有一种方法可以调整单个表格行的高度?

9

我现在已经尝试了两天,但是没有成功调整表格中单独一行的最小高度。

我使用以下方法创建表格:

<?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);
?>

我已经尝试过设置td padding、td margin、td height、tr height,但都没有成功。我尝试了从CSS和HTML中调整这些属性,唯一成功的是让行高变得比原来更高,而我想要的是缩短行高。我在TCPDF文档中搜索,但唯一找到的是TCPDF不支持padding和margin。你们有人知道任何实现我想要结果的“hack”吗?

1个回答

32
你遇到的问题可能是文本行实际高度。在内部,TCPDF使用单元格高度比率来控制呈现的行高。当你有一个只有一行文本的TD时,你可以将它缩小到线条的总高度。因此,td单元格的最小尺寸为fontsize * cellheightratio + any cellpadding proscribedcellpadding可以来自cellpadding属性,所以我在这个例子中将其设置为0。我相信在编写HTML之前,至少可以通过setCellPaddings设置一些填充维度。
你可以使用line-height CSS声明来设置单元格高度比率,使行更小。(当然,你也可以减小字体大小。)
<?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);

在我的5.9.206安装中,上述代码会产生以下结果:Visual example of set line-heights. 这意味着第一行的字体大小是两倍大。第二行将行高设置为字体大小的100%。第三行是80%。第四行是50%。
请注意,如果文本换行,非常缩小的行高会使其看起来很糟糕。

这是正确的。它也适用于空表格单元格。 - 321zeno
2
我也遇到了这个问题,但我没有使用HTML单元格,只是使用了标准的Cell方法。我需要一个精确的高度才能使页面分页边框正常工作,我花了几个小时摸不着头脑,不知道FontSize正在改变我的大小,从而扰乱了分页。保存原始字体大小,然后在使用Ln()写入单元格之前调用SetFontSize(0),然后重新设置字体大小即可解决问题。感谢您! - gregthegeek
如何添加多个 $this->setCellHeightRatio(2);? - wahmal
运行得很棒。谢谢。 - Amit Dwivedi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接