4

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?

caoglish
  • 1,343
  • 3
  • 19
  • 29

6 Answers6

4

Try this deletePage function

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

Instead Delete use deletePage

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Usman Saeed
  • 78
  • 10
4

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

}
Kanti
  • 627
  • 4
  • 18
3

You should check your $html variable.

1) If it could contains any <html />, <head />, <title />, <body /> tag then please remove them and just take html contents after and before <body />.

2) You should avoid any css, js link file within $html content.

3) Finally you should use $html=utf8_encode($html); just before $this->writeHTML($html, true, false, true, false, '');.

4) You may need to adjust your MARGIN_LEFT, MARGIN_TOP, MARGIN_RIGHT and MARGIN_BOTTOM to solve such problems. Please check $this->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); and $this->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);.

Hopefully it can solve your problem.

Santanu Brahma
  • 121
  • 1
  • 4
  • Check whether you have added any pagebreak at end of the HTML content, if it is, either remove it or add some condition around it as per requirement – Aditya Oct 12 '21 at 05:48
3

My answer is similar to @kanti. I think we can set the default to false even before the Output generation.

Background. The extra page we see is basically

"If true print TCPDF meta link".

so by default the TCPDF::$tcpdflink = true , is set true. All we need is

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

call your public function later when you need it. ...

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

Good Luck.

user5249203
  • 4,436
  • 1
  • 19
  • 45
0

Check also the height of your enclosing div. It should not be 100%. Try to remove any height property from the CSS style of the enclosing div ( I mean the div which encloses all the content).

gdm
  • 7,647
  • 3
  • 41
  • 71
-1

The problem is the 4th parameter (unicode = true) in your create_pdf.php file. This parameter is passed into tcpdf.php on line 1838

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

change it to false.

Shonubi Korede
  • 541
  • 6
  • 4