2013-10-10 83 views
6

我想了两天,现在没有结果,调整表中的单行最小高度,但没有成功。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填充,TD保证金,TD身高,TR的高度,没有成功。我也尝试从CSS和HTML这些。我设法实现的唯一目标就是看到一行的高度大于原始值,但我希望缩短它的长度。我尝试在TCPDF的文档中搜索,但唯一发现的是TCPDF不支持填充和边距。你们有没有知道某种“黑客”来达到我想要的结果?

回答

23

你可能碰到的是文本行的实际高度。在内部,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%。

*请注意,如果您的文字包装,它会看起来非常糟糕的线条高度。

+0

这是正确的。它也适用于空表格单元格。 – 321zeno

+2

我遇到了这个问题,除了我没有使用HTML单元格,只是标准的Cell方法。我需要一个确切的高度才能使页面打破边界正常工作,并且我花了几个小时来挠挠头,不知道FontSize正在改变我的尺寸,抛弃了休息时间。保存我的原始字体大小,然后在用Ln()写入单元格之前调用SetFontSize(0),然后重置字体大小以修复它。感谢你! – gregthegeek

+0

如何添加多个$ this-> setCellHeightRatio(2); ? – wahmal

相关问题