0

I'm trying to find an example of setasign/fpdi code.

I have a pdf document with multiple pages. I want to create a new pdf that imports each page AND adds a background (this background is another pdf doc - portrait or landscape depending on root pdf page) + some extra text to it.

I'm unable to find a correct example for this issue, hope someone can help me with this one.

John Wings
  • 11
  • 3

1 Answers1

0

I found the answer, I will post it here. Maybe there is a better way... :-)

$portrait_backgroundpdf = "pdf/background_portrait.pdf";
$landscape_backgroundpdf = "pdf/background_landscape.pdf";
$originalpdf = "pdf/original.pdf";
// First get number of pages + orientations of original pdf
$temp_pdf = new Fpdi();
$count = $temp_pdf->setSourceFile($originalpdf);
$sizes = [];
for ($pageNo = 1; $pageNo <= $count; $pageNo++) {
    $templateId = $temp_pdf->importPage($pageNo);
    $size = $temp_pdf->getTemplateSize($templateId);
    $sizes[$pageNo] = $size['orientation'];
}
// sizes is now an array like this example:
// [
//      1 => "P", <-- portrait
//      2 => "P", <-- portrait
//      3 => "L", <-- landscape
//      4 => "L", <-- landscape
// ]

// Now start the new PDF
$pdf = new Fpdi();
foreach($sizes as $page => $size) {
    $pdf->AliasNbPages();
    $pdf->AddPage();
    if ($size == "P") {
        $pdf->setSourceFile($portrait_backgroundpdf);
    } else {
        $pdf->setSourceFile($landscape_backgroundpdf);
    }
    $tplId = $pdf->importPage(1);
    $pdf->useImportedPage($tplId, 0, 0, null);
    $pdf->setSourceFile($pdfurl.$pastepdf);
    $template = $pdf->importPage($page);
    $pdf->useTemplate($template, 10, 30, 180);
    // Do other pdf stuff here per page
}
$pdf->Output();
John Wings
  • 11
  • 3