TCPDF如何防止多余的空白页


how TCPDF prevent the extra blank page

我已经创建了使用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将创建一个分页符之后。

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

试试这个deletePage函数

$lastPage = $this->getPage();
$this->deletePage($lastPage);

代替Delete使用deletePage

我有同样的问题:我修改了:

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

你应该检查你的$html变量。

1)如果它可能包含任何<html />, <head />, <title />, <body />标签,那么请删除它们,只取<body />之后和之前的html内容。

2)你应该避免任何css, js链接文件在$html的内容。

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);

我的回答与@kanti类似。我认为我们甚至可以在Output生成之前将默认值设置为false。

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

"如果为真,打印TCPDF元链接"。

所以默认情况下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

好运。

检查div的高度。它不应该是100%。试着从封闭div(我指的是包含所有内容的div)的CSS样式中删除任何height属性。

问题是您的create_pdf.php文件中的第4个参数(unicode = true)。该参数在第1838行传递到tcpdf.php

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'ISO-8859-1', false);