1

I am prototyping a quote system for our guys on the road.

Essentially what I'm looking to do is take details from a web form, which will be taken to populate a PDF template.

At the moment I reckon the best way is to create a PDF template with placeholders, e.g. [[clientname]], find that reference in the PDF then swap it with what we have in the form.

I am using codeigniter for the app out of experience with it for rapid prototyping. Does anyone know of a library or other such functionality that will allow me to easily modify a PDF document in this way?

Thanks for your time!

Aaron
  • 1,549
  • 2
  • 13
  • 19
  • *At the moment I reckon the best way is to create a PDF template with placeholders, e.g. [[clientname]]* - I would propose using AcroForm form fields instead. Filling forms is generally a fairly easy in many pdf libraries. – mkl Dec 18 '13 at 20:43

2 Answers2

4

PDFs are collection of objects that will be rendered by a page renderer; modifying the blocks by "replacing placeholders" might work but, on the other hand, it might not. For example if you write "[[zip_code]]" in a document, chances are that it will be kerned into three or more chunks, and the PDF (once made readable - it might not be) will not contain the ASCII sequence "[[zip_code]]" at all.

Your best bet is to use PDFLib or CPDF to generate a blank pdf from a template where you will store the coordinates of each text block. Actually you could just do that in a single PHP file like this:

$tokens = array(
    'zip'     => array( 2500, 1730, ... ),
    'address' => array( 2600, 1234, ... ),
    ...
);
foreach ($tokens as $token => $info) {
    list ($x, $y /*, ..font... size...*/) = $info;
    PDF_show_XY ($pdf, $record[$token], $x, $y );
}
PDF_end_document($pdf);

Then when you have the page to "overlay" over your chosen background, you can do that with a tool such as pdftk:

pdftk foreground.pdf background background.pdf output merged.pdf

With the appropriate resolution unit (e.g. tenths of a mm), you can print the background and measure the positions of where you want the fields with an ordinary ruler.

The PDFs will be a bit bulkier than if you had generated them from scratch, but they will be print-perfect - the two layers will be "merged" on print, and won't otherwise interfere with each other.

LSerni
  • 55,617
  • 10
  • 65
  • 107
1

You could use something like https://code.google.com/p/dompdf/ which will generate PDF's based on a web page. Should make things easy - just make the page look how you want, have the user fill in the relevant info, then generate the pdf.

As far as editing existing PDF's, see PDF Editing in PHP?.

http://www.setasign.com/products/fpdi/about/ and Zend look like the best options. However, Zend seems to be difficult to work with given your constraints. Might be worth at least testing dompdf if budget is a concern.

Community
  • 1
  • 1
stormdrain
  • 7,915
  • 4
  • 37
  • 76
  • Hi, thanks. Not looking for HTML to PDF as we already have a stylised PDF template in use. Rather than alter this document manually each time we simply want placeholders in the PDF that will be replaced with variables held in/automated responses formulated in PHP. – Aaron Dec 18 '13 at 19:04