Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how TCPDF prevent the extra blank page

Tags:

php

pdf

tcpdf

I have create class to make page by using TCPDF.

I need to convert HTML to pdf, so I using writeHTML and AcceptPageBreak().

The $html is Dynamically changed, could be very long.

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

The problem is I genenrate PDF, but alway has a extra blank page in the end of the PDF.

I tried use $this->delete($this->getPage()) ,but it only remove last page which has content and the extra blank page remain. this seems writeHTML will create a page break after it.

how to prevent this extra blank page?

like image 981
caoglish Avatar asked Dec 19 '22 16:12

caoglish


1 Answers

I had the same Problem: I fixed it with:

class TCPDFextended extends \TCPDF {

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

}
like image 54
Kanti Avatar answered Jan 07 '23 07:01

Kanti