Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FPDI with Multiple Pages

Tags:

html

php

pdf

fpdf

fpdi

I am new to PHP and am having a bit of a hard time with using FPDI when it comes to inserting multiple pages.

I have a .pdf file which consists of 3 pages. I ended up saving page 1 as a separate page out of the 3 and that worked with my code, but that is because my code is meant only for 1 page. When I change it back to the 3 page file, it gives me an internal server error.

This is the code which I am using:

<?php

require_once('prog/fpdf.php');
require_once('prog/fpdi.php');

// initiate FPDI
$pdf = new FPDI();

// add a page
$pdf->AddPage();

// set the source file
$pdf->setSourceFile("apps/Par.pdf");

// import page 1
$tplIdx = $pdf->importPage(1);

// use the imported page and place it at point 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx, null, null, 0, 0, true);

// font and color selection
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(200, 0, 0);

// now write some text above the imported page
$pdf->SetXY(40, 83);
$pdf->Write(2, 'THIS IS JUST A TEST');


$pdf->Output();

I am not sure how to turn this code into having the ability to see all 3 pages. Please help me out who ever can.

like image 373
hiter202 Avatar asked Sep 17 '14 19:09

hiter202


Video Answer


3 Answers

It works for me:

<?php

require_once('fpdf/fpdf.php');
require_once('fpdi/src/autoload.php');

use \setasign\Fpdi\Fpdi;

$pdf = new FPDI();
// get the page count
$pageCount = $pdf->setSourceFile('pdf_file.pdf');
// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
    // import a page
    $templateId = $pdf->importPage($pageNo);
    // get the size of the imported page
    $size = $pdf->getTemplateSize($templateId);

    // create a page (landscape or portrait depending on the imported page size)
    if ($size[0] > $size[1]) {
        $pdf->AddPage('L', array($size[0], $size[1]));
    } else {
        $pdf->AddPage('P', array($size[0], $size[1]));
    }

    // use the imported page
    $pdf->useTemplate($templateId);

    $pdf->SetFont('Helvetica');
    $pdf->SetXY(5, 5);
    $pdf->Write(8, 'A complete document imported with FPDI');
}

$pdf->Output();

Change array($size['w'], $size['h']) for array($size[0], $size[1])

like image 74
Chalén Avatar answered Oct 21 '22 23:10

Chalén


The setSourceFile() method will return the page count of the document you'd set. Just loop through this pages and import them page by page. You example for all pages would look like:

<?php
require_once('prog/fpdf.php');
require_once('prog/fpdi.php');

// initiate FPDI
$pdf = new FPDI();

// set the source file
$pageCount = $pdf->setSourceFile("apps/Par.pdf");

for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
    $tplIdx = $pdf->importPage($pageNo);

    // add a page
    $pdf->AddPage();
    $pdf->useTemplate($tplIdx, null, null, 0, 0, true);

    // font and color selection
    $pdf->SetFont('Helvetica');
    $pdf->SetTextColor(200, 0, 0);

    // now write some text above the imported page
    $pdf->SetXY(40, 83);
    $pdf->Write(2, 'THIS IS JUST A TEST');
}

$pdf->Output();

Regarding the "internal server" you should enable your error reporting:

error_reporting(E_ALL);
ini_set('display_errors', 1);

...or simply check your php error logs for details.

like image 40
Jan Slabon Avatar answered Oct 21 '22 23:10

Jan Slabon


<?php
require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');

// initiate FPDI
$pdf = new FPDI();

// get the page count
$pageCount = $pdf->setSourceFile('Laboratory-Report.pdf');
// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
// import a page
$templateId = $pdf->importPage($pageNo);
// get the size of the imported page
$size = $pdf->getTemplateSize($templateId);

// create a page (landscape or portrait depending on the imported page size)
if ($size['w'] > $size['h']) {
    $pdf->AddPage('L', array($size['w'], $size['h']));
} else {
    $pdf->AddPage('P', array($size['w'], $size['h']));
}

// use the imported page
$pdf->useTemplate($templateId);

$pdf->SetFont('Helvetica');
$pdf->SetXY(5, 5);
$pdf->Write(8, 'A complete document imported with FPDI');
}

// Output the new PDF
$pdf->Output();    
like image 4
Rakesh Patel Avatar answered Oct 22 '22 00:10

Rakesh Patel