Create new PDF from template with PHP

2020-05-19 05:40发布

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

10条回答
一夜七次
2楼-- · 2020-05-19 06:16

The problem with trying to use a PDF as a "template" is that PDF is a typeset document format. Eg. it is a fixed layout and you shouldn't really mess with the layout too much.

It would be much simpler, if you want to do more with this, to use something like XSL-FO to generate your documents from an XML "template". All you need do is generate the XML with the right fields in place and the XSL-FO renderer can then create the PDF for you.

The best (only?) free XSL-FO renderer is Apache FOP.

查看更多
萌系小妹纸
3楼-- · 2020-05-19 06:18

use TCPDF you can create the PDF template using HTML tags principles it is fast to create, easy to use, easy to integrate I used it for generating detailed invoices of a law office(multiple pages with different format), the production envelope of a jewellery workshop (included photo), stock notes and invoices.

TCPD examples

查看更多
Melony?
4楼-- · 2020-05-19 06:22

Take a look at http://template2pdf.com/ It provides an API that allows you to use OpenOffice or LibreOffice templates and do substitution of text or images.

查看更多
Rolldiameter
5楼-- · 2020-05-19 06:26

Instead of doing this with PHP, you could use Word or OpenOffice Mail Merge feature to create the template and import the data into the tags. Then you can "print" but export as a pdf instead of actually printing. This might be a faster solution provided you don't need to update the template regularly. I did this for about 150 products and it took less than 30 minutes including creating the template and uploading the pdfs. Just an after-thought, hope this helps somebody else!

查看更多
虎瘦雄心在
6楼-- · 2020-05-19 06:28
放我归山
7楼-- · 2020-05-19 06:35

I was needing that for a project so I've created a fast tool for that. You can check it and feel free to contribute here

查看更多
登录 后发表回答