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.
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd49e61fd5d94760b21366_XAMPPsurveydata.jpg)
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.
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd4abdc011ae6ad98b0b98_XAMPPinstaller.jpg)
After installation, go inside the XAMPP folder and double click xampp-control. You should see the following pop-up window and click “Start”.
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd4a7be2408d7db7b83882_XAMPPcontrol.jpg)
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd4aaf9f00c0317cda6214_XAMPPstart.jpg)
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.
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd4b5e1c5db8b882b9646c_XAMPPindex.jpg)
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.
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd4c8c1c14868677836e71_XAMPPfpdfSetup.jpg)
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.
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd4cd7b3dca23dde8b1ff4_XAMPPfn.jpg)
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.
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd4cccded9e63269971ebd_XAMPPcode1.jpg)
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.
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd4d5dd87714c03b2d87e2_XAMPPcode2.jpg)
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.
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd4d8dc011ae1d698b2686_XAMPPfileLoc.jpg)
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd4b503440522538d95221_XAMPPrunPHP.jpg)
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.
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd4e44f7c27960647bb324_XAMPPresult1.jpg)
Second, here is what the PDF actually looks like. Nothing fancy (we can certainly make it look nicer if we want to).
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd4e6fa37cb5732debcc32_XAMPPresult2.jpg)
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.
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd4efe00728b13547c98b2_XAMPPstop.jpg)
![](https://cdn.prod.website-files.com/5fa05e9d96c17367ec930104/5fbd4f12f7c27960567bb413_XAMPPquit.jpg)