0

We want to create dynamically generated PDFs via HTML - but we also want to use an existing template for each generated page.

Using a template via FPDI is straight forward enough:

// Generate the FPDI instance
$pdf = new \setasign\Fpdi\Tcpdf\Fpdi(…);

// Add the first page
$pdf->AddPage();

// Load the template
$pagecount = $pdf->setSourceFile('template.pdf');
$tpl = $pdf->importPage(1);
$pdf->useTemplate($tpl);

// Write the HTML
$pdf->writeHTML($html, true, 0, true, 0);

However, when the written HTML causes an automatic page wrap, the subsequent pages do not use the template.

Since I cannot know beforehand how many pages there will be generated due to the dynamically generated HTML - how can I instruct TCPDF/FPDI to use a given template for all pages automatically? Similar to mPDFs SetDocTemplate function, where you can define that the last page of the template should be repeated as needed.

  • tecnickcom/tcpdf version 6.2.26
  • setasign/fpdi version 2.2.0
fritzmg
  • 2,494
  • 3
  • 21
  • 51

1 Answers1

3

I was running into the same problem. In my case using the xFPDF package. (but should be the same for normal FPDI.)

I solved it by adding the template via the header function. http://www.fpdf.org/en/doc/header.htm As for me this was adding a logo and a page title to every generated page. So this function is used for every generated page. I just moved the template code to the start of the header function else , like in my case, the logo and text wouldn't show up in the header.

class PDF extends XFPDF_CORE
{

  function Header()
  {
    $pagesource = $this->setSourceFile('pdftemplate/preview-test.pdf');
    $pageimport = $this->importPage($pagesource);
    $this->useTemplate($pageimport);

    ...

   }

 ...

}


$pdf->new PDF();

...

$pdf->AddPage();

...

$pdf->writeHTML($multipagecoveringhtml);

...

Hope this is still helpful to your project.

marty90
  • 103
  • 1
  • 2
  • 11
  • I see, looks simple enough :). We eventually moved to `mPDF` where this functionality already works, so I cannot really test it anymore. – fritzmg Nov 06 '19 at 11:18