Ever wanted to generated PDF documents on the fly with PHP? Perhaps populate a standard contract with a customers name and address? FPDF and FPDI are two neat libraries which greatly helps when working with PDF files. FPDF is the main library for handling PDF files, while FPDI lets your import existing PDF documents into the FPDF documents. In the code sample I’ve prepared here I’m importing a PDF template document, writing some information to the document, then sending it to the “customer”. FPDF offers several neat ways to expose the generated document, such as a stream to the browser, a string, or just storing it to file. You can download PDF Sample code to test it.

Be sure to head on over to the FPDF site and FPDI site for more information on these two libs.

Related posts:
Generated PDFs over HTTPS with Internet Explorer

[tags]PHP,PDF,Frameworks[/tags]

17 Responses to “Populate PDF templates with PHP / FPDF / FPDI”

  1. Thanks! FPDI is just what I’ve been looking for when your post popped up out of nothingness on the main page at planet-php. Great timing! ;)

  2. [...] Populate PDF templates with PHP / FPDF / FPDI (tags: php) [...]

  3. It’s suitable for small needs, but for serious pdf documents with unicode, floating texts and tables pdflib is the only solution I have found so far. Still you must pay for it.

  4. [...] Hoem recently pointed out two libraries that can be used in PHP to generate PDF files dynamically – FPDF and FPDI. Ever [...]

  5. The Zend_PDF class is very good for doing these types of things too!

  6. Shaun: I’ve not seen that, but I will check it out. Thanks :)

  7. Brilliant! You just saved me a good few hours work. Thanks :)

  8. I just want to say that a new version of FPDI is up: 1.2.1 which is compatible with TCPDF as of now.

    Cheers!
    Jan

  9. Great article. Exactly what I wanted!

    Thanks for saving my day :)

  10. Thank You So Much……….

    I was looking for this code since last 2 Days..

    Once again Thanx..

  11. This code is not working. Where to place this code?
    I m using XAMPP.

  12. Your sample code puts the name, address etc. at a specific place on PDF. If I have a placeholder in PDF and do not know the exact location, how can I do the replacement? Thanks,

  13. Harry Yuan :
    Your sample code puts the name, address etc. at a specific place on PDF. If I have a placeholder in PDF and do not know the exact location, how can I do the replacement? Thanks,

    Norbert Mocsnik :
    Thanks! FPDI is just what I’ve been looking for when your post popped up out of nothingness on the main page at planet-php. Great timing!

  14. Harry Yuan :
    Your sample code puts the name, address etc. at a specific place on PDF. If I have a placeholder in PDF and do not know the exact location, how can I do the replacement? Thanks,

    this is my question too,is there any way?

  15. Great Article! I was looking like more than a week at different scripts…this works!

    But now is the major problem that some pdf-files work flawless, and some of them just don’t.

    Any idea?

    Thanks a lot!

  16. Hej, a very nice article, it helped me to put info in a pdf template. However, i have a ‘small’ problem. After i add the info, i want to add another page to the template, an already made pdf. For this i am using a code provided by fpdi. However, i am not able to concatenate the two files with the input i want to be in it. I can concatenate them, but then, the first pdf i empty. Otherwise, it is just one page, not two… Do you think you can help me to figure this out….
    I would greatly appreciate any help!!!!

  17. hi. I have been testing this for the last hour and I don’t think it can do hat I need it to do. Here is what I need: I have an html form on my website, name, address, phone number, your equipment’s serial number, etc.
    When customers fill that out and press submit, I want a pdf document to open with all this information, so they can print it out and send it…is that possible? When I tried to test it, all it does is print “John Doe, Downtown Streeet 12…”

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">