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