How to populate a PDF file’s form with data from a web server

This fortnight I’ll provide 2 examples with step-by-step descriptions of how
to populate a PDF file’s form fields with data processed by a webserver. One example uses Microsoft’s Active Server Pages

(ASP) to populate a PDF form, and the other uses the ActiveX version of the FDF toolkit to populate a PDF form with data
submitted from an HTML form.

Why PDF forms and not HTML forms?

PDF file forms published on the World Wide Web can be

pre-populated by data from a webserver’s application or CGI script, much like HTML forms (by dynamically rendering the page

with the VALUE tag populated). However, unlike HTML forms the PDF form’s field data can be updated without reloading the page

from the server. When a user submits data to the webserver’s application or CGI for processing, only the form data is

updated, not the whole page. The data is updated with a form data format (FDF) unique to PDF files. The FDF data is

considerably smaller than HTML files. As a result, building Internet application that process data using PDF forms can be

much more efficient than HTML forms.


Despite the bandwidth efficiencies FDF data affords the Internet developer, the decision to build Internet forms in PDF

version HTML should be carefully considered. For some guidelines to determine which format is best for your form-based

applications be sure to read (or review) chapter 4 in my book Internet Publishing with Acrobat. You can purchase a

hard-copy at any of the online book stores or view the chapter online here at http://www.imagebiz.com/ipwa.

PDF forms function differently than HTML forms. Here’s a sample
of how a PDF form works:

  1. The user triggers the ‘Submit Form’ action from a PDF file object trigger like a submit button, a page action,

    a link, etc. Note: the user must be viewing the PDF within a web browser.


  2. The form data is sent to the webserver for processing (or storage and any number of other actions). This data is sent

    as either HTML-encoded form data or in the FDF format.


  3. The webserver’s application or script takes whatever action appropriate with the data. Depending on the application or

    script, the results of the storage or processing can then be sent back to the user.


  4. The data is received from the application or script and the form field’s are updated. FDF data can also create fields,

    display status dialog boxes, and generate new PDF pages dynamically (using ‘templates’). These other functions will

    be examined in later columns.

How to use FDF form data with a PDF form.

>For this example, you’ll build a PDF form using Microsoft’s
Active Server Pages (ASP). The form will display the server’s current date and time.


  1. Open the PDF file that contains the form you want to use. (For this example you can using an existing PDF file if you

    want to construct the PDF form according to the steps in this how-to guide. The complete form can be viewed at the end of

    this exercise.).


  2. If you’re using the example PDF file, add 2 fields with the following names:


  1. Create a submit button with the form tool. In the Field Properties dialog box, click the Actions tab.

  2. From the list, select the ‘Mouse Down’ trigger and click the Add button.

  3. From the Action type select list, choose the ‘Submit Form’ action and click the ‘Select URL’

    button.


  4. In the SubmitForm Selections dialog box, enter the URL of the application that will process the submitted form data. For

    this exercise use: ‘http://www.planetpdf.com/planetpdf
    /inetpub/demo1.asp’


  5. . If you wish to run the example for your server the URL could look like: http://www.yourserver.com/demo1.asp or

    http://www.yourserver.com
    /cgi-bin/demo1.pl.


  6. Set the Export Format to HTML Form (URL encoded).

  7. In the field selection sample, leave the default option. The default ‘All Fields’ radio button exports all

    names and values of all the fields are the PDF form. This section allows you to select particular fields for submission.


  8. Click Ok to close the SubmitForms Selections dialog box.

  9. In the Add Action dialog box, click Set Action.

  10. In the Field Properties dialog box, click Ok.

  11. Save the file as ‘demo1.pdf’ and upload it to your webserver.

  12. Next, copy the following ASP script and save it on your server as an ASP file. Be sure that the URL set in step 6 points

    to the location of this ASP file on your web server. Here’s the script:


  13. <br><%@ LANGUAGE = VBScript%><br><% Response.ContentType = 'application/vnd.fdf <br>' %> <br>%FDF-1.2<br>1 0 obj<br><<<br>/FDF << /Fields [ <br><< /V (<%=Date%>)/T (date) >> <br><< /V (<%=Time %>)/T (time)>><br> ] >> <br>>><br>endobj<br>trailer<br><<<br>/Root 1 0 R<br>>><br>%%EOF<br>

  14. That’s it! Open the example ‘demo1’ at HREF=’http://www.planetpdf.com/planetpdf/inetpub/demo1.pdf&#8217;>http://www.planetpdf.com/planetpdf
    /inetpub/demo1.pdf or open the example on your own server.

Submit values from an HTML form into a PDF file

PDF files can be used to take data submitted by users from HTML
forms and render a page that can be printed by the user. Order forms, tax forms, and sales collateral are all possible
applications for converting HTML form data to a PDF file for presentation.

Building a script to convert the HTML data to a FDF file that opens a PDF file is shown in the next example.
Unfortunately, user must make configure their web browser to properly view the data. If you or your users are viewing PDF
files with Acrobat 4.0, the installer should have made the configuration. If not, to configure your web browser to properly
view FDF data you must select Acrobat as the application that handles the MIME type application/vnd.fdf. Otherwise, your web
browser will try to download the FDF file.

How to configure Netscape (e.g., Communicator 4.x):


  1. Select Preferences > Applications.

  2. Choose Acrobat as the ‘helper’ application.

  3. Uncheck the checkbox ‘Ask me before opening downloaded files of this type’.

How to configure Internet Explorer (for Windows 9x users):


  1. Open Windows Explorer, choose the menu item View > Options > file Types.

  2. Check to make sure there is an entry for Adobe Acrobat Forms Document with the Default Extension for Content Type set to

    FDF and the Content Type (MIME): set to ‘application/vnd.fdf.’.


  3. Uncheck the checkbox ‘Confirm open after download’.

  4. Under ‘Actions:’ there should be an entry for ‘open,’ and in its properties, ‘Application used

    to perform action’ should be set to C:Program FilesAdobeAcrobat 4.0AcrobatAcrobat.exe (or wherever Acrobat

    resides).


  5. The checkbox ‘Use DDE’ should be unchecked.

  6. Also, be sure to the file > Preferences > Weblink menu item and choose your browser from within Acrobat.

Business Card Example

Here’s an example of a script that takes data sent from an HTML
form and renders the results in a PDF file. In this example we’re using Adobe’s FDF toolkit (the ActiveX flavor). You can
download the Adobe’s FDF toolkit from HREF=’http://partners.adobe.com/asn/developer/acrosdk/main.html&#8217; target=’TOP’>http://partners.adobe.com/asn
/developer/acrosdk/main.html. Additionally, you can find the ActiveX toolkit and unsupported versions of FDF Toolkits for Perl and Java at http://www.planetpdf.com/mainpage.asp?WebPageID=338.

Try the example:


  1. First, make sure you?ve set up your browser to handle FDF files as described above.

  2. Open the second demo from HREF=’http://www.planetpdf.com/planetpdf/inetpub/demo2.html&#8217;>http://www.planetpdf.com/planetpdf
    /inetpub/demo2.html.

  3. Fill out the HTML form and submit the data.

  4. The data submitted in the HTML form is rendered within the PDF file. Additionally, a status dialog box has been added.

Build your own version of the demo:


  1. Open the blank PDF file in Acrobat.

  2. Create three fields: ‘nametitle’, ‘address’ and ‘contact’. In the example, the appearance
    of nametitle is 8 point Bold Helvetica with right justification. ‘Address’ is 8 point Helvetica with right
    justification and contact is 8 point Helvetica Bold-Oblique with colored gray text, left justified.

  3. Copy these three fields throughout the business card template. For easy alignment, turn on the snap to grid option.
    Select View > Snap to Forms Grid.

  4. Either create the HTML form as used in the example (field names are firstname, middlei, lastname, title, street,
    poboxorfloor, city, state, zip, phoneareacode, phoneprefix, phonesuffix, faxareacode, faxprefix, faxsuffix, email, website;
    form method= ‘post’) or copy the source of the file for you use.

  5. Here’s the source of the ASP file:

<br><%@ LANGUAGE = VBScript%><br><%<br>'*** Create the FDF object. <br>Set FdfAcX = Server.CreateObject('FdfApp.FdfApp')<br>Dim objFdf <br>Set objFdf = FdfAcX.FDFCreate<br><br><br>'*** Set the location of the PDF file you create.<br>objFdf.FDFSetFile 'http://www.planetpdf.com/planetpdf/inetpub/demo2.pdf'<br><br><br><br>'*** Set the variables with data from the various Request <br>'*** data name/ value pairs posted from the HTML form. I'm using<br>'*** rather verbose code here&comma; hopefully to benefit beginners <br><br>nametitle = Request.Form('firstname') & ' ' & Request.Form('middlei') & ' <br> ' & Request.Form('lastname') & '&comma; ' & Request.Form('title')<br>address = Request.Form('street') & ' ' & Request.form('poboxorfloor') & ' <br> ' & Request.Form('city') & ' ' & Request.Form('state') & ' <br> ' & Request.Form('zip') <br>contact = '(' & Request.Form('phoneareacode') & ') <br> ' & Request.Form('phoneprefix') & '-' & Request.Form('phonesuffix') & ' <br> (' & Request.Form('faxareacode') & ') ' & Request.Form('faxprefix') & '-' <br> & Request.Form('faxsuffix') & ' ' & Request.Form('website') & ' <br> ' & Request.Form('email')<br><br><br>'*** Use the FDFSetValue method to the PDF's name/value<br>'*** where the value is one of the variables set above.<br>objFdf.FDFSetValue 'nametitle'&comma; nametitle&comma; Off<br>objFdf.FDFSetValue 'address'&comma;address&comma; Off<br>objFdf.FDFSetValue 'contact'&comma;contact&comma; Off<br><br><br>'*** Use the FDFSetStatus method to display an alert status box.<br>objFdf.FDFSetStatus 'Your business cards have been created.'<br><br><br>'*** Write the data back to the buffer stream.<br>Response.ContentType = 'application/vnd.fdf'<br>Response.BinaryWrite objFdf.FDFSaveToBuf<br><br><br>'*** Close the object and clean-up.<br>objFdf.FDFClose<br>Set objFdf = Nothing<br>Set FdfAcX = Nothing<br><br>Response.end<br>%><br>

These two working examples should give you the basics on how to build
a PDF form with either ASP or the ActiveX toolkit. As the ActiveX toolkit has much more functionality not covered in these examples, I plan to cover more of the FDF toolkit next month.

If you’ve got questions, I’ll do my best to answer them. Please let me know what you think of this column and how I can improve it. As always, I’m available to consult on projects big and small. Contact me at HREF=’mailto:gordon_kent@yahoo.com’>gordon_kent@yahoo.com.

You May Also Like

About the Author: Gordon Kent

Leave a Reply