Home > PHP > PHP to PDF

PHP to PDF

December 23rd, 2008

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:

  1. Design of the form using InDesign (CS4) to look exactly as the original millimeter by millimeter.
  2. Save the form as a PDF file.
  3. Create the PHP file.
  4. Load of the PDF file into FPDF using FPDI.
  5. Add the information text using FPDF functions in the specific places.
  6. Generate the new PDF file.
  7. Test in the navigator.
  8. Finish.

The examples include in the FPDF and FPDI websites are self explanatory for all the necessary coding.

PHP

  1. No comments yet.
  1. No trackbacks yet.