Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Edit Exisiting pdf using fpdi

    • 1
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 1.73k
    Answer it

    Im using FPDI library for writing existing pdf file but my code is writing only 1 page ..why? please

    require_once '../includes/fpdf/fpdf.php';

    require_once '../includes/fpdf/fpdi.php';

    $pdf = new FPDI();

    $pageCount = $pdf->setSourceFile("contractFinalTrans.pdf");

    $tplIdx = $pdf->importPage(1);

    $pdf->addPage();

    $pdf->useTemplate($tplIdx, 10, 10, 200);

    //set position in pdf document

    // now write some text above the imported page

    //$pdf->SetFont('Arial');

    $pdf->SetFontSize(10);

    $pdf->SetTextColor(255,0,0);

    $pdf->SetXY(50, 50);

    $pdf->Write(0, "page 1");///print this output

    $pdf->SetAutoPageBreak(true,22);

    $pdf->addPage();

    $tplIdx = $pdf->importPage(2);

    //$pdf->addPage();

    $pdf->useTemplate($tplIdx, 10, 10, 200);

    //set position in pdf document

    // now write some text above the imported page

    //$pdf->SetFont('Arial');

    $pdf->SetFontSize(10);

    $pdf->SetTextColor(255,0,0);

    $pdf->SetXY(100 , 100);

    $pdf->Write(0, "page 2");

    $pdf->Output();

 2 Answer(s)

  • Thank you for your question. We just go through your code and found that you set the code only for first page. Kindly check importpages function which takes page number as first argument. You need to create loop for other pages as well. Kindly check the below code for more help.

    require_once('FPDI/fpdf/fpdf.php');
    require_once('FPDI/fpdi.php');
    require_once('FPDI/fpdf_tpl.php');
    
    // Original file with multiple pages 
    $fullPathToFile = "sample.pdf";
    
    class PDF extends FPDI {
    
        var $_tplIdx;
    
        function Header() {
    
            global $fullPathToFile;
    
            if (is_null($this->_tplIdx)) {
    
                // THIS IS WHERE YOU GET THE NUMBER OF PAGES
                $this->numPages = $this->setSourceFile($fullPathToFile);
                $this->_tplIdx = $this->importPage(1);
    
            }
            $this->useTemplate($this->_tplIdx, 0, 0,200);
    
        }
    
        function Footer() {}
    
    }
    
    // initiate PDF
    $pdf = new PDF();
    
    // add a page
    $pdf->AddPage();
    
    
    // The new content
    $pdf->SetFont("helvetica", "B", 25);
    $pdf->SetTextColor(255, 0, 0);
    $pdf->Text(40,120,"Sample Text over overlay");
    
    // THIS PUTS THE REMAINDER OF THE PAGES IN
    if($pdf->numPages>1) {
        for($i=2;$i<=$pdf->numPages;$i++) {
            //$pdf->endPage();
            $pdf->_tplIdx = $pdf->importPage($i);
            $pdf->AddPage();
        }
    }
    
    //show the PDF in page
    //$pdf->Output();
    
    // or Output the file as forced download
    $pdf->Output("sampleUpdated.pdf", 'D');
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: