Reputation: 99
I want to merge 2 or more pdfs, but with the condition. I am using PDFMerger.php (http://pdfmerger.codeplex.com/). The code What I have found is :
<?php
include 'PDFMerger.php';
$pdf = new PDFMerger;
$pdf->addPDF('samplepdfs/one.pdf', '1, 3, 4')
->addPDF('samplepdfs/two.pdf', '1-2')
->addPDF('samplepdfs/three.pdf', 'all')
->merge('download', 'samplepdfs/TEST2.pdf');
?>
This code is working fine for me. But I have a problem here. I do not know the page count in my pdfs as they are generating dynamically. So what if I want to skip the first page or last page from the three.pdf(assume there are 5 pages) & I do not have the page count.
So the output should be all pages of one.pdf+all pages of two.pdf+2-5 pages of three.pdf
Thanks in advance.
Upvotes: 0
Views: 1325
Reputation: 242
Try use this following code to find number pages in PDF:
exec('/usr/bin/pdfinfo '.$tmpfname.' | awk \'/Pages/ {print $2}\'', $output);
or
function getNumPagesInPDF($file)
{
if(!file_exists($file))return null;
if (!$fp = @fopen($file,"r"))return null;
$max=0;
while(!feof($fp)) {
$line = fgets($fp,255);
if (preg_match('/\/Count [0-9]+/', $line, $matches)){
preg_match('/[0-9]+/',$matches[0], $matches2);
if ($max<$matches2[0]) $max=$matches2[0];
}
}
fclose($fp);
return (int)$max;
}
Upvotes: 1