TCPDF如何防止额外的空白页

4
我创建了一个类来使用TCPDF生成页面。我需要将HTML转换为PDF,所以我使用了writeHTMLAcceptPageBreak()$html是动态变化的,可能非常长。
class MY_TCPDF extends TCPDF{
    public function makePage($html){
        $head_image="header.jpg";
        $this->SetMargins(PDF_MARGIN_LEFT, 70, PDF_MARGIN_RIGHT);
        $this->setPrintHeader(false);
        $this->AddPage();
        // get the current page break margin
        $bMargin = $this->getBreakMargin();
        // get current auto-page-break mode
        $auto_page_break = $this->getAutoPageBreak();
        // disable auto-page-break
        $this->SetAutoPageBreak(false, 0);
        // set bacground image
        $img_file = $head_image;
        $this->Image($img_file, 0, 0, 210, 68, '', '', '', false, 300, '', false, false, 0);
        // restore auto-page-break status
        //$this->SetAutoPageBreak($auto_page_break, PDF_MARGIN_BOTTOM);
        // set the starting point for the page content
        $this->setPageMark();
        $this->writeHTML($html, true, false, true, false, '');
        $this->lastPage();


        ob_start();
        //Close and output PDF document
        $this->Output('my.pdf', 'I');
        ob_end_flush();
    }

    public function AcceptPageBreak() {
        $this->SetMargins(PDF_MARGIN_LEFT, 10, PDF_MARGIN_RIGHT);
        $this->AddPage();   
        return false;
    }
}

问题在于我生成PDF时,总是会在PDF的末尾有一个额外的空白页。

我尝试使用 $this->delete($this->getPage()) ,但它只删除了最后一页有内容的页面,而额外的空白页还留在那里。这似乎是因为writeHTML会在其后创建一个分页符。

如何防止这个额外的空白页呢?

6个回答

4

我遇到了同样的问题: 我通过以下方法解决了它:

class TCPDFextended extends \TCPDF {

    public function Output($name = 'doc.pdf', $dest = 'I')
    {
        $this->tcpdflink = false;
        return parent::Output($name, $dest);
    }

}

1
这应该是被接受的答案。对我来说完美地运作了。 - Maciej Pyszyński
1
请问您能解释一下吗? - Linga

4
尝试使用这个 deletePage 函数。
$lastPage = $this->getPage();
$this->deletePage($lastPage);

不要使用Delete,使用deletePage


3

请检查你的 $html 变量。

1)如果它包含任何<html />, <head />, <title />, <body />标签,请删除它们并只获取<body />前后的html内容。

2)应避免在$html内容中包含任何css、js链接文件。

3)最后,在$this->writeHTML($html, true, false, true, false, '');之前,您应该使用$html=utf8_encode($html);

4)你可能需要调整你的MARGIN_LEFT、MARGIN_TOP、MARGIN_RIGHT和MARGIN_BOTTOM来解决这些问题。请检查$this->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);$this->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

希望这能解决你的问题。


检查您是否在HTML内容的末尾添加了任何分页符,如果有,请根据要求删除它或在其周围添加一些条件。 - Aditya

3

我的答案与@kanti类似。我认为在输出生成之前我们可以将默认设置为false。

背景。我们看到的额外页面基本上是

"如果为真打印TCPDF meta链接"。

因此默认情况下,TCPDF::$tcpdflink = true被设置为true。 我们所需要的就是

 class My_PDF extends TCPDF {
     public function changeTheDefault($tcpdflink) {
            $this->tcpdflink = $tcpdflink;
          }
}

在需要时稍后调用您的公共函数。

$get_pdf = new My_PDF (your_parameters);
$get_pdf->changeTheDefault(false); # changes the default to false

祝你好运。


0

还要检查您的包含 div 的高度。它不应该是 100%。尝试从包含所有内容的 CSS 样式的 div(我的意思是)中删除任何高度属性。


-1
问题出在您的create_pdf.php文件中的第四个参数(unicode = true)。该参数传递到tcpdf.php的1838行。
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'ISO-8859-1', false);

将其更改为false。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接