Shantanu Marathe
Shantanu Marathe

Reputation: 11

Import pages from pdf in php script

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

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

// set the sourcefile
$sourceFileName = 'dnb.pdf';

//Set the source PDF file
$pagecount = $pdf->setSourceFile($sourceFileName);
$i = 1;
$j = 2;
$k = 3;
$l = 4;
$m = 5;
// add a page
$pdf->AddPage();
$pdf->AddPage();
$pdf->AddPage();
$pdf->AddPage();
$pdf->AddPage();
// import page
$tplidx = $pdf->ImportPage($i);
$tplidx = $pdf->ImportPage($j);
$tplidx = $pdf->ImportPage($k);
$tplidx = $pdf->ImportPage($l);
$tplidx = $pdf->ImportPage($m);
$pdf->useTemplate($tplidx, 10, 10, 200);
$pdf->SetFont('Arial');
$pdf->SetTextColor(0,0,0);
$pdf->SetFontSize(14);

$pdf->SetXY(40, 265);
//$pdf->Write(1, "Download from http://abir.kumarkhali.com");
$pdf->SetXY(140, 265);
//$pdf->Write(1, "Date: " . date("d/m/Y"));

//$pdf->Image('image-1.jpg',120,240,20,20);
//$pdf->Image('image-2.jpg',120,260,20,20);

$pdf->Output(time().'-'.$sourceFileName, "D");

I want to view first five pages of pdf. In the above code i'm only able to get the 5th page. Only context from 5th page is imported .I can see other 4 pages blank.Please help.

Upvotes: 0

Views: 4892

Answers (1)

Chantal
Chantal

Reputation: 11

Try using a for/while loop to display required pages...

<?php

require 'fpdf/fpdf.php';
require 'fpdfi/fpdi.php';

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

// set the sourcefile
$sourceFileName = 'dnb.pdf';

//Set the source PDF file
$pdf->setSourceFile($sourceFileName);
$pagecount = 5;

$counter = 1;
while ( $counter <= $pagecount) {

$pdf->AddPage();
$tplidx = $pdf->ImportPage($counter);

$pdf->useTemplate($tplidx, 10, 10, 200);
$pdf->SetFont('Arial');
$pdf->SetTextColor(0,0,0);
$pdf->SetFontSize(14);

$pdf->SetXY(40, 265);
//$pdf->Write(1, "Download from http://abir.kumarkhali.com");
$pdf->SetXY(140, 265);
//$pdf->Write(1, "Date: " . date("d/m/Y"));

//$pdf->Image('image-1.jpg',120,240,20,20);
//$pdf->Image('image-2.jpg',120,260,20,20);

$counter++;
}
$pdf->Output(time().'-'.$sourceFileName, "D");

Upvotes: 1

Related Questions