Manual Page Break in TCPDF

ぃ、小莉子 提交于 2019-11-29 23:40:49
AmdY

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

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

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.

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.

igor.scabini

With version 5.9.142 from 2011-12-23 we could use the page-break-before, page-break-inside css properties, like this:

<div style="page-break-inside:avoid;">
some non breakable text
</div>

According to http://www.tcpdf.org/examples/example_049.phps you can use something like this

$html .= '<tcpdf method="AddPage" /><h2>Graphic Functions</h2>';

You need to verify that parameter K_TCPDF_CALLS_IN_HTML in TCPDF configuration file is true.

Mohsin

You can also follow this method to accomplish your needs:

$htmlcontent1="CERTIFICATE NUMBER 1 IMAGE HERE";

// output the HTML content
$pdf->writeHTML($htmlcontent1, true, 0, true, 0);

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

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Print a table

// add a page
$pdf->AddPage();

$htmlcontent1="CERTIFICATE NUMBER 1 IMAGE HERE";

// output the HTML content
$pdf->writeHTML($htmlcontent1, true, 0, true, 0);
// reset pointer to the last page
$pdf->lastPage();

// ---------------------------------------------------------

//Close and output PDF document
$pdf->Output('textcertificate.pdf', 'D');

Hopes it helps someone :)

Thanks

Giving your element the page-break-after, page-break-before or page-break-inside property via CSS will apply the attribute pagebreak or pagebreakafter to the html tag during TCPDF runtime.

// page-break-inside
if (isset($dom[$key]['style']['page-break-inside']) AND ($dom[$key]['style']['page-break-inside'] == 'avoid')) {
    $dom[$key]['attribute']['nobr'] = 'true';
}
// page-break-before
if (isset($dom[$key]['style']['page-break-before'])) {
    if ($dom[$key]['style']['page-break-before'] == 'always') {
        $dom[$key]['attribute']['pagebreak'] = 'true';
    } elseif ($dom[$key]['style']['page-break-before'] == 'left') {
        $dom[$key]['attribute']['pagebreak'] = 'left';
    } elseif ($dom[$key]['style']['page-break-before'] == 'right') {
        $dom[$key]['attribute']['pagebreak'] = 'right';
    }
}
// page-break-after
if (isset($dom[$key]['style']['page-break-after'])) {
    if ($dom[$key]['style']['page-break-after'] == 'always') {
        $dom[$key]['attribute']['pagebreakafter'] = 'true';
    } elseif ($dom[$key]['style']['page-break-after'] == 'left') {
        $dom[$key]['attribute']['pagebreakafter'] = 'left';
    } elseif ($dom[$key]['style']['page-break-after'] == 'right') {
        $dom[$key]['attribute']['pagebreakafter'] = 'right';
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!