Create new PDF from template with PHP

Kim picture Kim · Jun 22, 2011 · Viewed 69.3k times · Source

I have a docx document file set up with formating and layout, which I saved as a PDF file. I want to use this pdf file as a template for creating new pdf files pre-filled with user data.

I have seen FPDF, FPDI, tcPDF, domPDF, Zend PDF, http://koivi.com/fill-pdf-form-fields/ (Justin Koivisto), a few convertion tools like HTML2PDF and a few command line tools mentioned here on stackoverflow. With the exception of FPDI, none of them offer an example using a PDF template to either search-and-replace custom tags with actual data (usually from a database) or add data without specifying a bunch of values like x, y, postion, font, font-formating etc.

Creating a PDF from scratch seems like too much work. I have not yet considered using a HTML2PDF converter as the results appear to vary greatly, and then there is the issue with styling. Some tools allow CSS, but often with limits like no float support.

Are there no easy way of using a template like there is for Excel with PHPExcel ?

My need is:

  • Free
  • Load and use a template
  • Search-and-replace custom tags
  • Add data (text, image, font, formating etc)
  • Custom header and footer
  • Must work with shared hosting
  • Allow download, save/store to server and attach to email (preferable without having to save it first) of final generated PDF

Answer

drakezilla picture drakezilla · Aug 22, 2013

I really recommend MPDF :

  1. it is based on FPDF and HTML2FPDF, so you can use some of their functions or add others
  2. it has tons of cool features (create any kind of barcodes, locale support, watermarks, and many more)
  3. it has an extremely well made documentation
  4. it has a community forum
  5. it has examples!
  6. it has an easy learning curve

The only bad thing is that it is a little heavy-weight, but you can remove things that you dont need.