Question

I would like to remove some pages from my PDF created using fpdf library,

$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage(); 

Is there any function to remove the page. I am not familiar with FPDF.

Was it helpful?

Solution

You want to use FPDI. Get out of the mentality of "deleting" a page. Instead, view it as "not inserting" a page. Let's say I want to skip pages 3, 15, 17, and 22. Here's how you do it:

$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();

Please note that I didn't include a lot of things you could do with FPDI, including determining the orientation of the page. I also skipped out on some error checking for the sake of simplicity. View this as a template to work off of, not as the final code, because it is ultimately just a quick skeleton.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top