Dynamically Adding Text and Images to Existing PDF Using PHP

For one of the projects I am working on, I need to issue a press pass for customers that place an order. The traditional method of doing this would be to open up Photoshop each time and swap out the content and export as a PDF but I wanted a more streamlined approach—this is where PHP becomes an valuable asset.

After doing some research, I was able to find two libraries—one that handled importing an existing PDF (FPDI Libary) and one that handled adding text and images to the PDF (FPDF Library). There are massive applications for dynamically adding text and images to a PDF. Some of the applications include generating custom invoices, letters, certificates, print and so much more!

Below is a screenshot of the original template and final output for what I was working on:

php-adding-text-and-images-to-pdf

For both security and practical purposes, I cleaned up my code and removed the excess that was applicable to just my project. Below is the clean version of my PHP code:

require_once('fpdf.php');
require_once('fpdi.php');

function generatePDF($source, $output, $text, $image) {

$pdf = new FPDI('Portrait','mm',array(215.9,279.4)); // Array sets the X, Y dimensions in mm
$pdf->AddPage();
$pagecount = $pdf->setSourceFile($source);
$tppl = $pdf->importPage(1);

$pdf->useTemplate($tppl, 0, 0, 0, 0);

$pdf->Image($image,10,10,50,50); // X start, Y start, X width, Y width in mm

$pdf->SetFont('Helvetica','',10); // Font Name, Font Style (eg. 'B' for Bold), Font Size
$pdf->SetTextColor(0,0,0); // RGB 
$pdf->SetXY(51.5, 57); // X start, Y start in mm
$pdf->Write(0, $text);

$pdf->Output($output, "F");
}

generatePDF("template.pdf", "export.pdf", "Hello world", "image.jpg");