Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new PDF from template with PHP

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
like image 235
Kim Avatar asked Jun 22 '11 10:06

Kim


People also ask

How create PDF in php explain with example?

php require('./fpdf. php'); $pdf=new FPDF(); $pdf->AddPage(); $pdf->SetFont('Arial','B',16); $pdf->Cell(40,10,'Hello World! '); $pdf->Output(); ?> Upon execution, the PHP script will generate a PDF file in your browser.

How can create HTML PDF in php?

To generate a pdf file, first, we need to include the library file fpdf. php. Then we need to create an FPDF object using the default constructor FPDF().


2 Answers

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.

like image 75
drakezilla Avatar answered Sep 23 '22 12:09

drakezilla


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

like image 39
Catalin Marin Avatar answered Sep 24 '22 12:09

Catalin Marin