7

I'm looking at the options for building an invoice and ordervoucher generator in PHP. I've created several PDF's for other occasions before with FPDF and TCPDF and both work fine but are sooooo verbose that i'm getting sick of it.

I get a feeling that generating PDF's in any programming language is super verbose.

Is there any way at all to generate a PDF-blueprint with Adobe Acrobat and use that as a template with variable fields/area's? That way i could for example allow the designers overhere create me a letterhead and overall invoice theme and i would only write the content of the variable parts (order lines, totals, due date, buyer details) into the pdf.

To put it simple, a barebones templating system that reads a pdf and outputs it again after variable substitution.

ChrisR
  • 14,370
  • 16
  • 70
  • 107

4 Answers4

7

You can create a PDF with formfields and replace the content of those fields in the source. You have to replace the exact number of characters though, otherwise the PDF might get corrupted. In your case, where you want to add whole tables, that might not be feasible.

We did something very similar but used a different approach. Using http://www.setasign.de/products/pdf-php-solutions/fpdi/ we loaded the PDF, and put the content we wanted on top (FPDI extends TCPDF or FPDF).

konsolenfreddy
  • 9,551
  • 1
  • 25
  • 36
  • FPDI is pretty much the only decent solution for using a previous PDF template and annotating. Personally, I think TCPDF is better for extending. – Orbling Mar 14 '11 at 12:23
1

You could use the Zend PDF module which is free: http://framework.zend.com/manual/en/zend.pdf.html

See this question for editing existing files or creating placeholders to swap out using PHP: PDF Editing in PHP?

You can use FPDF for creating new PDF files although it has it's flaws (but it very quick to get something created: http://www.fpdf.org/

Community
  • 1
  • 1
Andrew
  • 9,967
  • 10
  • 64
  • 103
1

It is much simpler to hold templates in some kind of meta-language (like for FPDF) and generate documents as to fiddle with binary streams and lone glyphs in PDFs. Since /dev/null exists, verbosity should be not a problem.

p4553d
  • 818
  • 1
  • 7
  • 17
1

Sounds like you want Form Fields. There are two flavors of forms in PDF:

AcroForms: Forms created with the tech originally built into Acrobat 4.x & 5. It's been extended quite a bit along the way. Lots of software out there can read and write AcroForm-based PDFs.

XFA: The New Hotness. Adobe LiveCycle Designer (ships with Acrobat Pro) can build you some really spiffy forms that can only be processed with a few libraries... mostly Adobe's own. Adobe's server products are Not Cheap.

Your PDF library du jur may also let you import pages from existing PDFs. You build your PDF "boilerplate" in whatever app you like, export to PDF, and then draw on top of it with your PDF library. This lets you draw anything you want, not just what fields let you display (line art, images, and text, not mostly-just-text).


There are also quite a few HTML->PDF libraries floating around. You could build your reports in HTML then translate them to PDF if you feel more comfortable going that route. Some are better at it than others, and only one I know of will take HTML script into account (wkhtmltopdf).

Mark Storer
  • 15,672
  • 3
  • 42
  • 80