PHP to PDF
I had the task to populate a PDF form using PHP. There are many ways to do that, here is a very interesting one. However in my case the PDF form I need to work with had a part which was a graphic form included in the PDF. When the PDF was printed it did not look nice, acceptable but not nice.
Because I wanted to learn how to create a PDF file with PHP I investigate for another way. I found a nice article about Generating PDFs with PHP which uses PDFLib. I didn’t decided to use it because it is a commercial product and the free option they offer (PDFlib Lite) is limited. I didn’t like also because is necessary to compile the library plus other related task. I am comfortable compiling software but in small projects this is to much I think.
I wanted a free and fast option, so after searching and testing I become comfortable with FPDF. This is a class which is just add to the php code and you are ready to create PDFs. It is very easy to learn. I also found FPDI. This is a free class which allows to import existing PDF documents into FPDF in order to add more elements to it.
I accomplished the task using this process:
- Design of the form using InDesign (CS4) to look exactly as the original millimeter by millimeter.
- Save the form as a PDF file.
- Create the PHP file.
- Load of the PDF file into FPDF using FPDI.
- Add the information text using FPDF functions in the specific places.
- Generate the new PDF file.
- Test in the navigator.
- Finish.
The examples include in the FPDF and FPDI websites are self explanatory for all the necessary coding.