Search code examples
phppdffpdffpdi

FPDI remove page from PDF output


I'm using FPDF and FPDI to do the following

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

// initiate FPDI 
$pdf = new FPDI(); 
// set the sourcefile 
$pdf->setSourceFile('pdfs/originals/document.pdf'); 

//for ($i = 1; $i < 43; $i++) { 
for ($i = 1; $i < 10; $i++) { 

$pdf->AddPage();
$tplidx = $pdf->ImportPage($i); 
$pdf->useTemplate($tplidx, 10, 0, 200); 

With that is there any way of hiding a page from the final output depending on if statements? For example if $middlepages is equal to or more than 2, I need to show 2 certain pages, but if $middlepages is 1, I only need to show the first page of the 2.


Solution

  • Looks like nobody got to this question... until now. Here's the basics of the FPDI workflow, straight from their documentation:

    1. Define the document to take pages from
    2. Import an existing page of the document
    3. Use the imported page on a page created with FPDF

    This is a simple FPDI example in action:

    $pdf = new FPDI();
    $pageCount = $pdf->setSourceFile('file.pdf');
    
    //  Iterate through every page
    for( $pageNo=1; $pageNo<=$pageCount; $pageNo++ )
    {
        $templateId = $pdf->importPage($pageNo);
    
        //  Determine if page is portrait or landscape
        $size = $pdf->getTemplateSize($templateId);
    
        if( $size['w']>$size['h'] )
            $pageFormat = 'L';  //  landscape
        else
            $pageFormat = 'P';  //  portrait
    
        $pdf->addPage($pageFormat,array($size['w'],$size['h']));
        $pdf->useTemplate($templateId);
    }
    
    $pdf->Output();
    

    Great! Now let's look at your specific desire, which is to add only a certain amount of pages. As you can see, we use a for loop to iterate through all the imported pages of the document, starting from page 1 and going to the last page, inclusive. If you wanted to change which pages are added, all you have to do is change the start and end points for the for loop. The concept is not to "hide" a page, but rather to only import pages we want to see in the final result.

    Here's a simple example. I'm going to define some constants to represent different page-adding schemes:

    const PDF_FIRSTPAGE     = 1;
    const PDF_MIDDLEPAGES   = 2;
    

    Before my page import loop, I will go through this switch to determine my start and end pages for each condition. This is after initially declaring the PDF. Notice how I check values against $pageCount to ensure that I do not attempt to import pages that do not exist:

    switch( $loopCondition )
    {
        //  By default, include everything
        default:
            $pageStart  = 1;
            $pageEnd    = $pageCount;
            break;
        //  Include only the first page
        case PDF_FIRSTPAGE:
            $pageStart  = 1;
            $pageEnd    = 1;
            break;
        //  Include pages 2-4, but only if those pages exist
        case PDF_MIDDLEPAGES:
            if( $pageCount>=2 )
            {
                $pageStart = 2;
                if( $pageCount>=4 )
                {
                    $pageEnd = 4;
                }
                else
                {
                    $pageEnd = $pageCount;
                }
            }
            else
            {
                $pageStart  = 1;
                $pageEnd    = 1;
            }
            break;
    }
    

    Finally, I modify the for loop you saw above:

    for( $pageNo=$pageStart; $pageNo<=$pageEnd; $pageNo++ )
    

    Now if I set my variable $loopCondition equal to PDF_MIDDLEPAGES I only get pages 2-4 as an output if the document originally had 4 or more pages. If I use a 3-page document, I get pages 2-3. You should alter the conditions to best suit your needs, as I do not know the nature of the PDF files you plan to import, but I think with this post you should be able to figure out everything you need to know on how to use FPDI to selectively include certain pages from a source PDF document. Happy coding!