Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manual Page Break in TCPDF

I am using TCPDF to generate the PDF in one of my projects. I simply create a HTML file and give it to the TCPDF to handle the PDF generation. But now I have some HTML where multiple certificates are added one after the other and I want to have a page break in it. Page Break should be decided by HTML i.e. I want to know if there is any identifier in HTML which TCPDF understands and then accordingly adds a page break into the generated PDF.

How could I do this?

like image 381
swapnilsarwe Avatar asked Oct 22 '09 08:10

swapnilsarwe


4 Answers

I'm using <br pagebreak="true"/>.

Find method writeHTML and code

if ($dom[$key]['tag'] AND isset($dom[$key]['attribute']['pagebreak'])) {
    // check for pagebreak
    if (($dom[$key]['attribute']['pagebreak'] == 'true') OR ($dom[$key]['attribute']['pagebreak'] == 'left') OR ($dom[$key]['attribute']['pagebreak'] == 'right')) {
        // add a page (or trig AcceptPageBreak() for multicolumn mode)
        $this->checkPageBreak($this->PageBreakTrigger + 1);
    }
    if ((($dom[$key]['attribute']['pagebreak'] == 'left') AND (((!$this->rtl) AND (($this->page % 2) == 0)) OR (($this->rtl) AND (($this->page % 2) != 0))))
            OR (($dom[$key]['attribute']['pagebreak'] == 'right') AND (((!$this->rtl) AND (($this->page % 2) != 0)) OR (($this->rtl) AND (($this->page % 2) == 0))))) {
        // add a page (or trig AcceptPageBreak() for multicolumn mode)
        $this->checkPageBreak($this->PageBreakTrigger + 1);
    }
}
like image 142
AmdY Avatar answered Nov 19 '22 14:11

AmdY


You might use TCPDF's AddPage() method in combination with explode() and a suitable delimiter:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8',
                 false);

// TCPDF initialization code (...)

$delimiter = '<h1>';
$html      = file_get_contents('./test.html');
$chunks    = explode($delimiter, $html);
$cnt       = count($chunks);

for ($i = 0; $i < $cnt; $i++) {
    $pdf->writeHTML($delimiter . $chunks[$i], true, 0, true, 0);

    if ($i < $cnt - 1) {
        $pdf->AddPage();
    }
}

// Reset pointer to the last page
$pdf->lastPage();

// Close and output PDF document
$pdf->Output('test.pdf', 'I');
like image 42
mermshaus Avatar answered Nov 19 '22 13:11

mermshaus


I tried using

<br pagebreak="true" />

or

<tcpdf method="AddPage" />

each of them resulted not in starting new page at the top of the page but adding the full A4-page empty space in between HTML text. So if text ended in the middle of the page and then page break was inserted, the new text was written from the middle of the next page. Which I didn't want.

What worked was this (found it here TCPDF forcing a new page):

$pdf->writeHTML($content, true, 0, true, 0);

$pdf->AddPage();
$pdf->setPage($pdf->getPage());  

This now starts with writing text on top of the page.

like image 15
jazkat Avatar answered Nov 19 '22 13:11

jazkat


TCPDF support the 'pagebreak' attribute for HTML tags and CSS properties 'page-break-before' and 'page-break-after'. For example you can use <br pagebreak="true" />.

Check the official http://www.tcpdf.org website and forums for further information.

like image 9
user412934 Avatar answered Nov 19 '22 14:11

user412934