WP Easy Tuts

WpEasyTuts » Tutorials

Design and Create PDF with PHP in 10 minutes

Posted on July 25, 2019 by

In this article you will learn how to design and create awesome PDF using PHP in just 10 minutes. You can create PDF dynamically using PHP and Photoshop to auto generate invoices, receipts, tickets, offer letters, resumes etc with user information.

Step 1: Design or select a PDF template to use with PHP

Before we start generating PDF with user or customer information we need to select PDF template or design your own PDF template using Photoshop. I already have designed one PDF invoice template using Photoshop.

You may download sample invoice template I designed using Photoshop. If you are designing or selecting PDF make sure all the user information is blank on the template. For example, I have designed PDF for invoice with information such as invoice number, contact details, products, prices are left blank. This information need to be added dynamically using PHP.

Step 2: Download FPDF PHP Class

FPDF is PHP class which is use to generate dynamic PDF using pure PHP. You might need to download multiple classes in order to generate dynamic PDF. Please click here to download the full FPDF classes in one package. Once downloaded, include these files into your project to begin generating dynamic PDF with pure PHP.

Step 3: Create a PHP File to generate PDF with Pure PHP using PDF template

Lets start with creating one PHP file with name invoice.php and then start writing the code to dynamically generate PDF using PHP.

Initiate the FPDF classes.

use setasign\Fpdi\Fpdi;
use setasign\Fpdi\PdfReader;
require_once('fpdf.php');
require_once('autoload.php');

Create new page for the PDF. In the array 178.9,263.31 are sizes given in millimeters for PDF. Please change these sizes according to your PDF template sizes.

$pdf = new FPDI('P','mm',array(178.9,263.31));
$pdf->AddPage();

After initiating FPDF classes and setting PDF size, now we have to upload the PDF template we have designed in Photoshop.

$pagecount = $pdf->setSourceFile("invoice.pdf"); //source of your PDF template
$tpl = $pdf->importPage(1, PdfReader\PageBoundaries::MEDIA_BOX);
$pdf->useTemplate($tpl);

Finally, we have to fill in the blank information in the PDF template we have designed or selected. Four things are to be consider when filling in the information. First we have to define XY axis in millimeters to fill in the information on the right place inside PDF. Second we have to define the text background, in my case I added white background using RGB color code. But it can be change depending on your PDF design. Third, define font name, font weight and font size “Arial”, “B”, “9” respectively. Fourth, we have to define width, height, text, border, alignment, fill color and link for cell.

//Add Name for Invoice
$pdf->SetXY(31,37);
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial','',9);
$pdf->Cell(50, 4, "John Doe", 0, 1,'L',1);

You may read full documentation here for available functions. It allow you to add not only text but images and links as well in PDF. You may change color, add shapes, images, links, backgound etc to generate fully functional PDF using PHP like Photoshop.

Final Step, create and store the dynamically generated PDF.

//destination of dynamically generate pdf. F stands for saving the file.
$pdf->Output("invoice_generated.pdf", "F");

That’s all! We have successfully generated PDF using PHP with Photoshop PDF template. Here is the full code to fill in all the information in the PDF invoice we designed using Photoshop.

use setasign\Fpdi\Fpdi;
use setasign\Fpdi\PdfReader;
require_once('fpdf.php');
require_once('autoload.php');

$pdf = new FPDI('P','mm',array(178.9,263.31));
$pdf->AddPage();

//Set the source PDF file
$pagecount = $pdf->setSourceFile("invoice.pdf");
$tpl = $pdf->importPage(1, PdfReader\PageBoundaries::MEDIA_BOX);
$pdf->useTemplate($tpl);

//Add Name for Invoice
$pdf->SetXY(31,37);
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial','',9);
$pdf->Cell(50, 4, "John Doe", 0, 1,'L',1);

//Add Company for invoice
$pdf->SetXY(31,42.80);
$pdf->SetFont('Arial','',9);
$pdf->Cell(50, 4, "John & Doe Co.", 0, 1,'L',1);

//Add Address Line 1 for invoice
$pdf->SetXY(31,47.80);
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial','',9);
$pdf->Cell(50, 3, "23 Areena Plaza, Street 45", 0, 1,'L',1);

//Add Address Line 2 for invoice
$pdf->SetXY(31,51.80);
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial','',9);
$pdf->Cell(50, 3, "Old Town, Boston, MA", 0, 1,'L',1);

//Add Address Line 3 for invoice
$pdf->SetXY(31,56.80);
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial','',9);
$pdf->Cell(50, 3, "United States", 0, 1,'L',1);

//Add Invoice No.
$pdf->SetXY(140,37);
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial','',8);
$pdf->Cell(30, 5, "WPEASY-190726", 0, 1,'L',1);

//Add Date for invoice
$pdf->SetXY(140,42.80);
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial','',8);
$pdf->Cell(30, 5, "26 July 2019", 0, 1,'L',1);
	
//Add Product Name for invoice
$pdf->SetXY(11,82);
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial','',9);
$pdf->Cell(80, 6, "WordPress Website Development & Designing", 0, 1,'L',1);

//Add Price for Invoice
$pdf->SetXY(146,82);
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial','',9);
$pdf->Cell(15, 6, "500.00 USD", 0, 1,'L',1);

//Add Total Price for Invoice
$pdf->SetXY(146,171.8);
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial','B',9);
$pdf->Cell(15, 5, "500.00 USD", 0, 1,'L',1);

//Add Total amount payable in Words
$pdf->SetXY(9,185);
$pdf->SetFillColor(255,255,255);
$pdf->SetFont('Arial','B',9);
$pdf->Cell(60, 5, "FIVE HUNDREDS DOLLARS ONLY", 0, 1,'L',1);

//destination of dynamically generate pdf. F stands for saving the file.
$pdf->Output("invoice_generated.pdf", "F");

Above code will generate PDF with filled in information dynamically.

Final Words

You can call invoice.php file in the browser and it will dynamically generate PDF with your information filled in the PDF exactly. You can also add the code inside any file of your project. Please be careful with XY axis. The parameters should be given in millimeters. You may pull information from database and run loop to add multiple product names in the invoice.

Give it a try on your computer and you will explore more functions to generate fully functional PDF with pure PHP. Try to create some sample invoice, receipts, tickets or offer letter. Please leave comment if you have any question or you might be looking for help to generate more complex PDF using PHP.

No Comments

Leave a Reply