Saturday, October 2, 2010

Filling PDF forms using XFDF

Description

This article is about user data that can be provided in PDF form back to the user. Don't use old methods in the web which suggest to use FDF. FDF and the PHP FDF module are deprecated, don't support UTF-8 and you'll have only problems with it ;). 

One common use case is a web form that requires the user to input information for an application that has to be sent by snail mail for confirmation or to print the information in a user friendly form that is exchangeable.

The key is creating an XFDF file that holds all the user information connected to IDs of form input fields that are defined in a PDF file with empty inputs. The empty fields are then filled with the user information. The URL to the PDF file is defined in the XFDF file. If the user opens the XFDF file with the help of the Adobe Reader browser plugin, all happens in the user's browser window. An alternative for the user is downloading the XFDF file to open it later.

Advantages:
  1. As filled PDF forms can't be saved (but can be printed), this is an alternative to keep the inputted data,
  2. you don't need HTML to PDF converters. Create professional looking PDFs in any program you like
Disadvantages:
  1. If the PDF file is available online, the user has to have an active internet connection whenever he or she opens the XFDF file, 
  2. Adobe Reader asks, if it should fill out the PDF with possibly unsecure data. The PDF stays empty without user's confirmation.
One workaround for the 1st disadvantage is PDFToolkit. It allows to flatten a PDF file with the XFDF file to create a user ready downloadable PDF file, but the data is not editable anymore. The 2nd workaround is to download the empty PDF file to disc and change the URL in the XFDF file which is in XML format.

Creating PDF forms with OpenOffice


OpenOffice has a toolbar with form elements. You can find it in the View->Toolbars->Form Elements menu. Just place the elements you need in the document and export the whole thing as PDF. Don't forget  to check the "Create PDF Form" option. Please be aware of the IDs of the form elements in the document. You can set the ID by chosing "Properties" in the context menu of the element. To check the form elements afterwards you can use PDFToolkit:

pdftk test.pdf dump_data_fields
\--\-
FieldType: Text
FieldName: textelement1
FieldNameAlt:
FieldFlags: 0
FieldValue:
FieldValueDefault:
FieldJustification: Left

Generating the XFDF file

The specification I used can be found on Adobe Website. This is the PHP code to create an XFDF file out of an array of user input data fields.


And this is how to use it:


As you can see, only the XFDF file is outputted. The Adobe Reader plugin parses it and tries to download the corresponding PDF file to fill in the user data.

No comments:

Post a Comment