TCPDF如何防止额外的空白页面

时间:2014-03-24 02:54:24

标签: php pdf tcpdf

我已经使用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 个答案:

答案 0 :(得分:3)

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

class TCPDFextended extends \TCPDF {

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

}

答案 1 :(得分:3)

我的回答与@kanti类似。我想我们甚至可以在输出生成之前将默认值设置为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
祝你好运。

答案 2 :(得分:2)

您应该检查$html变量。

1)如果它可以包含任何<html />, <head />, <title />, <body />标记,那么请删除它们,然后在<body />之前和之后获取html内容。

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

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

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

希望它可以解决您的问题。

答案 3 :(得分:2)

试试这个deletePage函数

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

相反删除使用deletePage

答案 4 :(得分:0)

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

答案 5 :(得分:-1)

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

.row {
    -webkit-column-count: 3;
    -moz-column-count: 3;
    column-count: 3;

    -moz-column-gap: 15px;
    -webkit-column-gap:15px;
}

将其更改为false。

相关问题