4

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).

NewGuy
  • 73
  • 1
  • 7
  • For your 2nd PHP (the one you say it fails), please replace all the `$this->pdf->`by `$pdf->` and retry – Ken Lee Jun 19 '21 at 17:17
  • @KenLee Thank you for the reply. After the change, I still get the same error. If I delete all the codes and just leave the following line of statement `require_once('../fpdf183/FPDI/FPDI-2.3.6/src/Fpdi.php');` in my PHP file, I still get the same error: – NewGuy Jun 19 '21 at 17:28
  • Do you have another version of fpdi ? (the one like this : `require_once('vendor/setasign/fpdi/fpdi.php');` – Ken Lee Jun 19 '21 at 17:35
  • @KenLee No, I don't have another version and this fpdi. The one I downloaded doesn't have a folder called `vendor/seatasign/..` either. https://www.setasign.com/products/fpdi/downloads/ – NewGuy Jun 19 '21 at 17:52
  • Please see the suggested way in my answer. Thanks – Ken Lee Jun 19 '21 at 18:21
  • Just enable PHP errors to see the error message. – Olivier Jun 20 '21 at 08:12

1 Answers1

3

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.

Ken Lee
  • 6,985
  • 3
  • 10
  • 29
  • I have downloaded the files from github site you mentioned and extracted them into a different folder in my hosting server. When I use `require_once 'FPDI-master/src/autoload.php';` it works fine. I can even have a test file in there that outputs a "Hello World" to the screen, so the path is correct: `require_once('FPDI-master/src/echohello.php');`. However, whenever it encounters this other line: `require_once('FPDI-master/src/fpdi.php');` it gives that same error message: This page isn’t working, ..... Can you please tell me from where did you get these `vendor/autoload.php` files? – NewGuy Jun 19 '21 at 19:47
  • I have revised my answer by adding a download link of zip file (containing both fpdf and fpdi). Please try – Ken Lee Jun 20 '21 at 03:10
  • Thank you for going this extra mile. I really appreciate it. Your code and any code I write works well with this `ok.pdf` file you have attached. There are no issues. However, if I try to use any pdf file other than your `ok.pdf` file, I get that same error. For example, can you try your code with this pdf file from adobe site: https://www.adobe.com/support/products/enterprise/knowledgecenter/media/c4611_sample_explain.pdf – NewGuy Jun 20 '21 at 14:59
  • The pdf in your link points to an encrypted pdf. (if you check the source of this particular pdf you will see the /Encryp flag) The fpdi documentation has stated that it does not support importation of encrypted pdf. I have just revised my answer to point out this. (and included in my answer one of the ways to tackle this problem) – Ken Lee Jun 20 '21 at 15:36
  • 2
    Thank you so much for explaining your answers with sample code and putting all these efforts. I really appreciate all you did. You are a big help. – NewGuy Jun 22 '21 at 23:30