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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19294288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:12:25  来源:igfitidea点击:

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

phphtmlcsstcpdf

提问by Adam Baranyai

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?

我已经尝试设置 td 填充、td 边距、td 高度、tr 高度,但没有成功。我也从 CSS 和 HTML 中尝试过这些。我设法实现的唯一一件事是看到一行的高度大于原始值,但我想让它更短。我尝试在 TCPDF 的文档中搜索,但我发现的唯一一件事是 TCPDF 不支持填充和边距。你们中有人知道某种“黑客”来达到我想要的结果吗?

回答by EPB

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 tdcell is fontsize * cellheightratio + any cellpadding proscribed

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

cellpadding can come from the cellpaddingattribute, so I set it to 0 for this example. I believe at least some of the padding dimensions can also be set with setCellPaddingsbefore writing the HTML.

cellpadding 可以来自cellpadding属性,所以我在这个例子中将它设置为 0。我相信在setCellPaddings编写 HTML 之前,至少也可以设置一些填充尺寸。

You can set the cell height ratio by using a line-heightCSS declaration to make rows smaller. (You can also, of course, just reduce the font size as well.)

您可以通过使用line-heightCSS 声明来设置单元格高度比率以使行更小。(当然,您也可以只减小字体大小。)

<?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.

我的 5.9.206 安装上的上述代码产生了这个: 设置行高的可视化示例。

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%.

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

*Note that if your text wraps, it'll look terrible at very reduced line-heights.

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