【问题标题】:TCPDF - Is there a way to adjust single table row height?TCPDF - 有没有办法调整单表行高?
【发布时间】:2013-10-18 03:01:17
【问题描述】:

我现在尝试了两天,没有结果,在表格中调整单行最小高度,但没有成功。

我正在使用以下方法来创建我的表:

<?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 不支持填充和边距。你们有谁知道某种“hack”来达到我想要的结果吗?

【问题讨论】:

    标签: php html css tcpdf


    【解决方案1】:

    您可能遇到的是文本行的实际高度。在内部,TCPDF 使用单元格高度比来控制渲染的行高。当你有一个单行文本的 TD 时,你可以使它的最小是该行的总高度。所以td 单元格的最小尺寸是fontsize * cellheightratio + any cellpadding proscribed

    cellpadding 可以来自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 安装上的上述代码会产生以下结果:

    这使得第 1 行变大,是字体大小的两倍。第 2 行将行高设置为字体大小的 100%。第 3 行是 80%。第 4 行有 50%。

    *请注意,如果您的文本换行,那么在非常减少行高时会看起来很糟糕。

    【讨论】:

    • 这是正确的。它也适用于空表格单元格。
    • 我也遇到了这个问题,只是我没有使用 HTML 单元格,只是使用标准的 Cell 方法。我需要一个准确的高度才能使分页边框正常工作,我花了好几个小时挠头,却不知道 FontSize 正在改变我的尺寸,摆脱了中断。保存我的原始字体大小,然后在使用 Ln() 写入单元之前调用 SetFontSize(0),然后重新设置字体大小来修复它。谢谢!
    • 如何添加多个$this->setCellHeightRatio(2); ?
    • 工作很棒。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-09
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 2023-02-11
    • 1970-01-01
    相关资源
    最近更新 更多