php - TCPDF - 有没有办法调整单表行高?

标签 php html css tcpdf

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

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

<?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 不支持填充和边距。你们中有人知道某种“黑客”来实现我想要的结果吗?

最佳答案

您可能遇到的是文本行的实际高度。在内部,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 安装上的上述代码产生了这个: Visual example of set line-heights.

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

*请注意,如果您的文本换行,在行高非常低的情况下看起来会很糟糕。

关于php - TCPDF - 有没有办法调整单表行高?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19294288/

相关文章:

php - 群组 , 订单 , 限额

PHP 从 URL 抓取数据并插入数据库不起作用 - 语法错误

php - 在 PHPUnit 异常上禁用堆栈跟踪

html - 导航栏元素悬停的问题

jquery - React 中的软滚动顶升

php - gmail登录后cookie被删除

html - 为什么这个动画不起作用?

javascript - javascript 突变后恢复内部 HTML

javascript - 如何显示某些内容正在加载,例如 : loading. 正在加载..正在加载

javascript - 多个元素可见和不可见取决于可观察的