How to generate highly customizable PDFs using PHP?

Without doubt, PHP’s main use is web development. However, we can apply it to generate some highly customizable PDFs. This post walks you through the entire process, from initial setup to the final product. 

Task Description

Say, our company was hired to conduct a survey asking people if they’re willing to buy a specific product and collect any comments they might have. 

The client wants a PDF file for each person who took the survey. The PDF should have the person’s name, gender, the questions asked, and the answers and comments given. It should also have the client’s company logo and questionnaire title. If a person takes the survey multiple times, the client wants a separate PDF file for each time the survey is taken. Here is a sample dataset.

Fig 1 Sample Dataset

Set Up XAMPP

Download XAMPP installer for Windows here and install it on your computer. If you have another operating system, you can find the relevant installer in the parent folder. If you aren’t the admin of your computer (a likely case if you’re using the company’s machine), change the default installation path to the Documents folder. This is a portable version of XAMPP, so you can even install it on a USB drive. 

Fig 2 Download Portable XAMPP Installer for Windows

After installation, go inside the XAMPP folder and double click xampp-control. You should see the following pop-up window and click “Start”. 

Fig 3 Double Click xampp-control
Fig 4 Start XAMPP Server

Now the server is running, open a browser window, type the highlighted address in the URL bar, and you should see a similar page as shown in the figure below. If so, it means everything works correctly and the XAMPP setup is done. 

Fig 5 Open Index Page in Browser

Set Up FPDF library

We will use the FPDF library. It doesn’t have as many functions as the TCPDF library, but it’s powerful enough for most use cases and a great starting point for beginners. If you need more advanced functions, the TCPDF is a better option. 

Let’s go to the FPDF website, download the most recent version, and unzip the folder anywhere you like. Now we’re ready to code. 

Fig 6 Download FPDF Library

PHP Script

We first create a function that will check if a file already exists in a folder. If so, it will add a version number to the name of the newly generated file. 

Fig 7 File Name Function

The next part of the code imports the FPDF library and reads line by line and cell by cell of the CSV file (aka the sample dataset). Since the top portion of the PDF file is the same for all survey takers, we can create this part before looping through data for each individual. 

Fig 8 PHP Code Part 1

Then the code prints out the question asked and goes through each cell to find applicant name, gender, and answer and comment given. As you can see from the code, each line is customizable in terms of font, size, color, etc. 

Fig 9 PHP Code Part 2

Make sure you put the PHP script, company logo picture, and sample data CSV file in the htdocs folder under XAMPP. Also, create a folder called PDFfiles to place all the PDFs generated. To run the script, simply open the browser and type localhost/CreatePDF.php

Fig 10 Files Needed in htdocs Folder
Fig 11 Run PHP Script in Browser

Final Product

First, open the PDFfiles folder, you should see 6 PDF files. Since Adam took the survey twice, there are two files generated for him with version number added at the end of the file name. 

Fig 12 Generated PDFs in the Folder

Second, here is what the PDF actually looks like. Nothing fancy (we can certainly make it look nicer if we want to). 

Fig 13 Sample PDF

Now imagine if we surveyed 1000 or 5000 people and had to create a PDF file for each one of them while keeping track of those who took the survey multiple times! This solution is certainly a life saver!

End the Entire Process

Once we are done running the script, we stop the server and quit XAMPP.

Fig 14 Stop XAMPP Server
Fig 15 Quit XAMPP