phppdffpdffpdi

Can't edit a existing PDF file with FPDI


I am trying to learn how to edit a PDF file with FPDF and FPDI using PHP language. I have this sample PDF file where I want to insert some values. If I were to create a PDF file using FPDF, everything works fine. But if I try to edit an existing PDF file using FPDI, it gives me the following error message: This page isn’t working. Failed to load resource: the server responded with a status of 500 ( ). crbug/1173575, non-JS module files deprecated. The following shows two programs for editing PDF files. The first one works and the second one fails.

<?PHP
  //This program works and create a new PDF file.
  require('fpdf.php');
  $pdf = new FPDF();
  $pdf->AddPage();
  $pdf->SetFont('Arial','B',16);
  $pdf->Cell(40,10,'Hello World!');
  $pdf->Output();
?>

The following program for editing an existing PDF file fails:

<?PHP
      require_once('fpdf.php'); 

      //This second line of code fails.
      require_once('../fpdf183/FPDI/FPDI-2.3.6/src/Fpdi.php'); //This path is correct. I have tested it out with a simple echo “hello world”; file. 
      //I assume I didn’t install fpdi correctly. I have tried replacing /Fpdi.php part with autoload.php as well. 


      $pdf = new FPDI();
      $pdf->AddPage(); 
    
      $pdf->setSourceFile('testfile.pdf'); 
      $tplIdx = $this->pdf->importPage(1); // import page 1 
      $this->pdf->useTemplate($tplIdx, 0, 0, 0, 0, true); 
      $this->pdf->SetFont('Arial', '', '13'); 
      $this->pdf->SetTextColor(0,0,0);
      $this->pdf->SetXY(20, 20); //set position in pdf document    
      $this->pdf->Write(0, 'Some texts will go in here');    
      $this->pdf->Output('newfile.pdf', 'D');//force the browser to download the output
    ?>

First I have downloaded FPDF .zip file from fpdf.org and extracted it in my hosting file server. Then I downloaded FPDI from setasign.com website and uploaded this .zip file to my hostinger online webserver. Then I have extracted it inside the same fpdf folder. (zlib extension is enabled in my server).


Solution

  • A. FPDI download

    After you download the fpdi from say https://github.com/Setasign/FPDI, please use the following to get the fpdi started:

    require_once 'FPDI-master/src/autoload.php';
    require_once('FPDI-master/src/fpdi.php');
    
    
    

    The following is a fully working example which I have used in the past for your reference (I've used TCPDF, but I have changed to use fpdf):

    B. Testing PHP: testgen.php

    
    <?php
    
    require_once 'vendor/autoload.php';
    //require_once('tcpdf/tcpdf.php');
    
    require_once('fpdf/fpdf.php');
    
    require_once('vendor/setasign/fpdi/fpdi.php');
    
    
    $pdf = new FPDI();
    
    
    $pagecount = $pdf->setSourceFile('ok.pdf');
    
    for ($n = 1; $n <= $pagecount; $n++) {
    $pdf->AddPage();
    
    
    $tplIdx = $pdf->importPage($n);
    $pdf->useTemplate($tplIdx);
    
    $pdf->SetFont('Helvetica', 'B', 10);
    
    $pdf->SetXY(150, 10);
    $pdf->Write(0, "Appendix 1(new)");
    }
    
    $pdf->Output("output_sample_ken.pdf", "D");
    
    ?>
    

    In order to faciliate you to further test it, you may download the fpdf / fpdi files from this link:

    http://www.createchhk.com/SO/pdfpack_20June2021.zip
    

    after that, unzip and upload the files to your webserver PHP folder, then use a browser to run the testgen.php to see the effect. (the php will add the text Appendix 1(new) on each page of the original ok.pdf file, and then download the file)

    C. Problem in processing Encrypted PDF

    Last but not least, please note that FPDI does not support importing of encrypted PDF documents see the following link:

    https://www.setasign.com/support/faq/fpdi/can-fpdi-import-encrypted-pdf-documents/

    In my experience, to process an encrypted pdf, you may use something like pdf995 to "print" the encrypted pdf so as to generate a normal pdf, then this latter pdf can be processed by FPDI.