Reputation: 125
I have read quite a bit about this and have seen mention of various libraries and such (e.g. FPDF) but all approaches seem to have an unnecessary number of steps, creation of intermediate files, etc. I have users who will fill in an online form, and then upon completion and validation on server side, I want to simply write the collected data to an existing PDF form. I do not need to create a new PDF. There will always be a blank PDF form on my site, which I want to copy, write to, and then email.
Also, now sure how to reverse engineer the PDF form in order to do the above. That is, with whatever library I use, I need to know how to determine the key names, etc in the existing form.
Any thoughts?
Regards, Brian
Upvotes: 0
Views: 438
Reputation: 125
Ok, I found a site to convert XFA to AcroForm. So now my form exists as AcroForm supposedly.
Still not sure how to use this form to enable a free library to recreate the form on server side on the fly.
Upvotes: 0
Reputation: 5058
It's not done by passing the values to the file like a string-replace. There's much more logic needed. Not FPDF nor PDFLib allow you to fill in a PDF form. Also your form seems not to be a normal AcroForm but an XFA form which is somewhat more complicated.
If you want to fill in this PDF form in PHP you should take a look at the SetaPDF-FormFiller component (not free!).
It's just that easy:
$writer = new SetaPDF_Core_Writer_Http('filled-form.pdf');
$document = SetaPDF_Core_Document::loadByFilename('form.pdf', $writer);
$formFiller = new SetaPDF_FormFiller($document);
$fields = $formFiller->getFields();
$fields['form1[0].#subform[0].Yes[0]']->push();
$fields['form1[0].#subform[0].No[0]']->pull();
$fields['form1[0].#subform[0].LastName[0]']->setValue('Wyss');
...
$document->save()->finish();
Upvotes: 1
Reputation: 125
I found an online site to analyze the existing form. It has entries such as this:
form1[0].#subform[0].Yes[0]
form1[0].#subform[0].No[0]
form1[0].#subform[0].Yes[1]
form1[0].#subform[0].No[1]
form1[0].#subform[0].Prefix[0]
form1[0].#subform[0].LastName[0]
form1[0].#subform[0].MiddleName[0]
Some are check boxes, some are text, some are drop downs. So if pdflib or fpdf are both free and can do what I need, ideally I would simply use this form info and write the POST data directly to the PDF. Is that possible? Are there examples somewhere to demonstrate this? I have about 40 fields to write for what it is worth.
Thanks!
Upvotes: 0
Reputation: 3605
So, you essentially want server-side filling.
There are various libraries out there which provide this capabilitiy, such as iText, pdflib etc.
Another option, particularly for a productive environment would be using an utility, such as FDFMerge by Appligent; the overall cost may be lower, depending on your internal rates for development.
As an alternative, you could look at client-side filling by creating an (X)FDF file with the correct reference to the Base PDF. When this FDF file is opened in Acrobat/Reader, it will automatically pull in the base PDF (your blank form), and fill it with the data.
FDF uses PDF syntax, but is quite simple and plain text (normally), and can be easily assembled with text manipulation tools of the database system. XFDF is the XML representation of FDF. To create a sample FDF, you take your form, completely fill it out, and export the data as FDF. That's what you have to recreate.
Upvotes: 0