0

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.

Muhammad Abdul-Rahim
  • 1,980
  • 19
  • 31
V A S
  • 3,338
  • 4
  • 33
  • 39
  • 1
    FPDF does not allow for deleting pages. You might try TCPDF which has the function deletePage(page_no) allowing you to remove a page. – creatvepro Aug 15 '13 at 15:09
  • I assume that `unset($pdf->pages[pageNum])` should work, though not tested. After deletion you have to reindex the array to make array indexes to fit the real page numbers. The index of first page is 1. – Timo Kähkönen Jun 30 '16 at 00:45

1 Answers1

1

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.

Muhammad Abdul-Rahim
  • 1,980
  • 19
  • 31