Я нашел ответ, выложу его здесь.Может быть, есть лучший способ ...: -)
$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();