What is the easiest way to remove one page (ie. the last page) from a local pdf file using php? I have around 100 files, and each one needs to have its last page dropped. Optimally, I want to replace the file with a file of the same name that is just one page shorter.
Asked
Active
Viewed 4,950 times
1 Answers
5
You can use FPDI
Example:
pdf = new FPDI();
$pageCount = $pdf->setSourceFile('document.pdf');
// Array of pages to skip -- modify this to fit your needs
$skipPages = [3,15,17,22];
// Add all pages of source to new document
for( $pageNo=1; $pageNo<=$pageCount; $pageNo++ )
{
// Skip undesired pages
if( in_array($pageNo,$skipPages) )
continue;
// Add page to the document
$templateID = $pdf->importPage($pageNo);
$pdf->getTemplateSize($templateID);
$pdf->addPage();
$pdf->useTemplate($templateID);
}
$pdf->Output();
You can delete de last page using the array size easily.

Br0wn
- 544
- 3
- 19
-
3Using this, the output PDF dimensions may be wrong. To ensure correct width and height, replace the `getTemplateSize` row with this: `$size = $pdf->getTemplateSize($templateID);` Replace the `addPage` row with this: `$pdf->addPage($size['orientation'], [$size[0], $size[1]]);` – i_love_nachos Aug 12 '19 at 20:20