TCPDF保持在一起的功能将内容保存在1页(PDF生成)

时间:2010-12-18 02:25:30

标签: php pdf tcpdf

我想知道TCPDF是否有一个保持在一起的功能。我有一个FPDF,但我不能让它在TCPDF中工作。

以下是我在PDF生成代码中的工作原理:

// ... PDF code/stuff

// while not kept together
    // add PDF stuff that should be kept together

// .. more PDF code/stuff

我认为如果添加了新页面,该函数将返回false,回滚然后再次执行while循环。

我确实有以下工作,但我宁愿它是在TCPDF的函数/方法中,所以它更可重用:

$pdf->startTransaction();
$block_page = $pdf->getPage();
$print_block = 2; // max 2 tries

while ($print_block > 0) {
    // do PDF stuff

    if ($pdf->getPage() == $block_page) {
        $print_block = 0;
    } else {
        // rollback
        $pdf = $pdf->rollbackTransaction();
        $pdf->AddPage();
        $block_page = $pdf->getPage();
        -- $print_block;
    }
}

如果它不依赖于内置的事务功能也很酷,因此可以在循环中使用事务,因为像writeHTML()这样的事情使用事务。

1 个答案:

答案 0 :(得分:5)

我想要类似的功能并决定使用交易。这在TCPDF版本5.9.125上。

我从TCPDF继承了我自己的PDF类并添加了我自己的方法:

public function writeHTMLTogether($html, $ln=true, $fill=false, $reseth=false, $cell=false, $align='') {
    $cp =  $this->getPage();
    $this->startTransaction();

    $this->writeHTML($html, $ln, $fill, $reseth, $cell, $align);

    if ($this->getPage() > $cp) {
         $this->rollbackTransaction(true);//true is very important
         $this->AddPage();
         $this->writeHTML($html, $ln, $fill, $reseth, $cell, $align);           
    } else {            
         $this->commitTransaction();            
    }
}

似乎工作正常。由于writeHTML似乎在某处存储了大量属性,因此如果没有真正的回滚,它就会崩溃。

可能不需要为当前页面($ cp)创建局部变量,因为我认为它已存储。但是嘿。

如果您继承自己编写自己的页眉和页脚功能,那么就不需要额外的工作了。

相关问题