Manipulating PDF files with PHP using FPDF
In .NET, we have certain libraries that do pdf manipulation for us.
In PHP, we have open source library for PDF file generation called FPDF. There is an extension of the library: FPDI that allows for manipulating PDF files.
FPDI extracts the content of the pdf as a template, allows us to change the content and then output the changed pdf as a new pdf file.
There is no install. We need to download and include fdpf.php and fdpi.php libraries in our php script as would be demonstrated in the examples below:
Example 1: Creating your first PDF from the php code
<?php
require('fpdf/fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
Example 2: Changing the content of the PDF
<?php
require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');
$pdf =& new FPDI();
$pdf->AddPage();
//Set the source PDF file
$pagecount = $pdf->setSourceFile("existing_pdf.pdf");
//Import the first page of the file
$tpl = $pdf->importPage(1);
//Use this page as template
// use the imported page and place it at point 20,30 with a width of 170 mm
$pdf->useTemplate($template, 20, 30, 170);
#Print Hello World at the bottom of the page
//Select Arial italic 8
$pdf->SetFont('Arial','I',8);
$pdf->SetTextColor(0,0,0);
$pdf->SetXY(90, 160);
$pdf->Rotate(90);
$pdf->Write(0, "Hello World");
$pdf->Output("modified_pdf.pdf", "F");
?>The above code in example 2 takes a PDF file “existing_pdf.pdf”, and creates a copy of it “modified_pdf.pdf” with “Hello World” printed.
This just give me
Deprecated: Assigning the return value of new by reference is deprecated in C:\Users\lenovo\wamp\www\Nettport CMS4\admin\modules\dashboard\visumservice\visumguide\countries\45\digital_application_form\digital45_print.php on line 5
FPDF error: Template does not exist! :(
If you look at the code, you'll see that he defines $tpl as the template page brought from the pdf, but when he sends the request to fpdf he uses $template. just change the variable name and it works =)