Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FPDF / FPDI addPage() Orientation

I'm using the following code to add a new page to my existing PDF document and save it.

require('addons/fpdf.php');
require('addons/fpdi.php');

$pdf = new FPDI();                      
$pagecount = $pdf->setSourceFile($orgpdfpath);
for($i = 1; $i <=  $pagecount; $i++){
    $pdf->addPage();
    $tplidx = $pdf->importPage($i);
    $pdf->useTemplate($tplidx);
}
$pdf->addPage($pdforientation);
$pdf->Image($imgpath,$pdfxaxis,$pdfyaxis,$pdfwith,$pdfheight);

$pdf->Output($orgpdfpath,'F'); 

It works fine if I have a document that is A4, Page 1: portrait, Page 2: portrait, Page 3: portrait, etc.

It also works if I add a landscape A4 Page. However, after I have added a landscape page and try to add a portrait, the landscape is shifted back to a portrait and the whole formatting of the document breaks.

I suspect that this has to do something with addPage() inside the loop. Why does does it not rotate appropriately when applying ->useTemplate?

like image 642
mmackh Avatar asked Aug 29 '12 20:08

mmackh


People also ask

How do I set page breaks in Fpdf?

php require('fpdf. php'); $pdf = new FPDF('P','mm','A5'); $pdf->AddPage('P'); $pdf->SetDisplayMode(real,'default'); $pdf->SetFont('Arial','',10); $txt = file_get_contents('comments.

How do I insert an image in Fpdf?

Adding Image to PDF file by FPDF php'); $pdf = new FPDF(); $pdf->AddPage(); $pdf->Image('images/pdf-header. jpg',0,0); $pdf->Output(); ?> The ouput of above code is here . ( Show Output ) We can add position with height, width and link to above code.

How do you draw a line in Fpdf?

Php require('fpdf. php'); $pdf = new FPDF(); $pdf->AddPage(); $width=$pdf->GetPageWidth(); // Width of Current Page $height=$pdf->GetPageHeight(); // Height of Current Page $pdf->Line(0, 0,$width,$height); // Line one Cross $pdf->Line($width, 0,0,$height); // Line two Cross $pdf->Output(); ?>


1 Answers

I oversaw that there was a function called ->getTemplateSize(). Here's a working snippet:

$pdf = new FPDI();                      
$pagecount = $pdf->setSourceFile($orgpdfpath);
for($i = 1; $i <=  $pagecount; $i++){

    $tplidx = $pdf->importPage($i);
    $specs = $pdf->getTemplateSize($tplidx);
    $pdf->addPage($specs['h'] > $specs['w'] ? 'P' : 'L');
    $pdf->useTemplate($tplidx);
}

$pdf->addPage($pdforientation);
$pdf->Image($imgpath,$pdfxaxis,$pdfyaxis,$pdfwith,$pdfheight);

$pdf->Output($orgpdfpath,'F'); 
like image 126
mmackh Avatar answered Sep 28 '22 09:09

mmackh