views:

1297

answers:

5

I am using TCPDF to generate the PDF in one of my project. I simply create a HTML and give it to the TCPDF to handle the PDF generation. But now I have certain HTML where multiple certificates are added one after the other and I want to have a page break into it. Page Break should be decide 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.

Kindly let me know if anyone has an idea how to do it.

+1  A: 

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');
Marc Ermshaus
A: 

Hi,

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

Mohsin
+1  A: 

I'm use <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); } }

AmdY
Brilliant - great work - great catch in the code!
JasonMichael
A: 

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.

A: 

Hi AmdY, I am using tcpdf_5_8_034 and I see that the code you are suggesting is already incorporated but I see the same problem with generation of multipage Document i.e. content is overwritten in the footer.

    Can you kindly suggest what could be wrong :- Here is a snippet of the smarty template which is used to grab the data ( basically I intend to output 18 rows on a page but if the data is greater then i set the pagebreak=true 

{if $rows<18 }
{assign var="rows" value=`$rows+1`}
  <tr>
{else}
 {assign var="rows" value=0}
   <tr pagebreak="true">

Please advise.

Girish Agarwal