如何计算TCPDF中MultiCell/writeHTMLCell的高度?

mer*_*uro 14 php pdf tcpdf

我尝试创建具有多页的PDF和需要计算每个元素(多细胞)的高度在提前为分页符准备.根据该文件有一对夫妇的功能有像GetCharWidth/GetStringWidth支持我在做我自己,但除了一个潜在的性能丢了,我可能不会做正确的反正.建议以更优雅的方式实现我的目标?

参考:TCPDF

小智 26

我GOT它:D !!!!!

创建另一个pdf2对象

// pdf2 set x margin to pdf1's xmargin, but y margin to zero
// to make sure that pdf2 has identical settings, you can clone the object (after initializing the main pdf object)
$pdf2 = clone $pdf;
pdf2->addpage
pdf2->writeCell
$height = pdf2->getY()
pdf2->deletePage(pdf2->getPage())
pdf1->checkPageBreak($height);
pdf1->writeCell()
Run Code Online (Sandbox Code Playgroud)

W00tness:D


Car*_*ton 18

这是一个老问题,但TCPDF的当前版本(截至2011年12月7日)有一个函数调用getStringHeight,它允许您在实际调用MultiCell之前计算传递给MultiCell的字符串的结果高度.然后这个高度可以用于各种事情,原始问题中的计算,以及制作表格时设置行高等.工作得很好.

只是一些信息,以防其他人偶然发现这个问题寻找这个问题的解决方案,就像我做的那样.


Rob*_*est 7

虽然Carvell的答案很棒,但TCPDF提到了getStringHeight返回估计的高度.有用的文档提供了一个非常全面的技术,以获得确切的高度$height.至于为什么他们不使用这个本身就是一个谜......

// store current object
$pdf->startTransaction();
// store starting values
$start_y = $pdf->GetY();
$start_page = $pdf->getPage();
// call your printing functions with your parameters
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
$pdf->MultiCell($w=0, $h=0, $txt, $border=1, $align='L', $fill=false, $ln=1, $x='', $y='',     $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// get the new Y
$end_y = $pdf->GetY();
$end_page = $pdf->getPage();
// calculate height
$height = 0;
if ($end_page == $start_page) {
    $height = $end_y - $start_y;
} else {
    for ($page=$start_page; $page <= $end_page; ++$page) {
        $pdf->setPage($page);
        if ($page == $start_page) {
            // first page
            $height = $pdf->h - $start_y - $pdf->bMargin;
        } elseif ($page == $end_page) {
            // last page
            $height = $end_y - $pdf->tMargin;
        } else {
            $height = $pdf->h - $pdf->tMargin - $pdf->bMargin;
        }
    }
}
// restore previous object
$pdf = $pdf->rollbackTransaction();
Run Code Online (Sandbox Code Playgroud)

  • 注意!在最新版本中,一些属性受到保护,您必须更改以下内容:`$ pdf-> h`到`$ pdf-> getPageHeight()`,`$ pdf-> bMargin`到`$ pdf-> getBreakMargin()` ,和`$ pdf-> tMargin`到`$ pdf-> getMargins()['top']` (3认同)

Jas*_*ael 5

根据我的经验,提前计算出单元高度几乎是不可能的。使用 TCPDF 的分页符处理功能要容易得多,它会提前告诉您是否要进入分页符。这是示例代码:

$yy = $this->pdf->GetY();

$check_pagebreak = $this->pdf->checkPageBreak($height+$padding,$yy,false);
Run Code Online (Sandbox Code Playgroud)

将 false 更改为 true 以允许自动分页,否则,您可以自己处理分页逻辑,这就是我最终所做的。

另外,如果您可能需要这个,这里还有另一个小提示:考虑使用事务功能分两次创建文档。第一遍用于计算所有高度和单元格、分页符等。您还可以将所有行高和每页行数存储在数组中。在第二遍中,使用所有正确的信息创建文档,不需要分页逻辑(第二遍可以从单独的方法运行,以便使代码更易于阅读,并保持理智)。


小智 1

重访帖子:Tcpdf \xe2\x80\x93 Variable Height Table Rows With MultiCell有很多有用的信息。这是一个简短的摘录:

\n
\n

getNumLines()...实际上允许我们确定给定特定宽度的文本字符串将占用多少行。实际上,它允许我们执行我使用 MultiCell 返回的操作,而无需实际绘制任何内容。这让我们可以用一行代码确定最大单元高度:

\n
$linecount = max($pdf->getNumLines($row[\'cell1data\'], 80),$pdf->getNumLines($row[\'cell2data\'], 80\n
Run Code Online (Sandbox Code Playgroud)\n
\n