首页 文章

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

提问于
浏览
3

我已经使用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 回答

  • 3

    我有同样的问题:我修复它:

    class TCPDFextended extends \TCPDF {
    
        public function Output($name = 'doc.pdf', $dest = 'I')
        {
            $this->tcpdflink = false;
            return parent::Output($name, $dest);
        }
    
    }
    
  • 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
    

    祝好运 .

  • 1

    你应该检查你的 $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); .

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

  • 0

    试试这个deletePage函数

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

    而是删除使用deletePage

  • 0

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

  • 2

    问题是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);
    

    将其更改为false .

相关问题