PDF In-Depth

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

July 13, 1999


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
  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
  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:
    <%@ LANGUAGE = VBScript%>
    <% Response.ContentType = "application/vnd.fdf 
    " %> 
    1 0 obj
    /FDF << /Fields [ 
    << /V (<%=Date%>)/T (date) >> 
    << /V (<%=Time %>)/T (time)>>
      ] >> 
    /Root 1 0 R
  14. That's it! Open the example "demo1" at http://www.planetpdf.com/planetpdf
    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 Files\Adobe\Acrobat 4.0\Acrobat\Acrobat.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 http://partners.adobe.com/asn
. 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 http://www.planetpdf.com/planetpdf
  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:

<%@ LANGUAGE = VBScript%>
'***   Create the FDF object.    
Set FdfAcX = Server.CreateObject("FdfApp.FdfApp")
Dim objFdf 
Set objFdf = FdfAcX.FDFCreate

'***   Set the location of the PDF file you create.
objFdf.FDFSetFile "http://www.planetpdf.com/planetpdf/inetpub/demo2.pdf"

'***   Set the variables with data from the various Request 
'***   data name/ value pairs posted from the HTML form. I'm using
'***   rather verbose code here, hopefully to benefit beginners 

nametitle = Request.Form("firstname") & " " & Request.Form("middlei") & " 
 " & Request.Form("lastname") & ", " & Request.Form("title")
address = Request.Form("street") & " " & Request.form("poboxorfloor") & " 
 " & Request.Form("city") & " " & Request.Form("state") & " 
 " & Request.Form("zip") 
contact = "(" & Request.Form("phoneareacode") &  ") 
 " & Request.Form("phoneprefix") & "-" & Request.Form("phonesuffix") & " 
 (" & Request.Form("faxareacode") &  ") " & Request.Form("faxprefix") & "-" 
 & Request.Form("faxsuffix") & "   " & Request.Form("website") & " 
 " & Request.Form("email")

'***   Use the FDFSetValue method to the PDF's name/value
'***   where the value is one of the variables set above.
objFdf.FDFSetValue "nametitle", nametitle, Off
objFdf.FDFSetValue "address",address, Off
objFdf.FDFSetValue "contact",contact, Off

'***   Use the FDFSetStatus method to display an alert status box.
objFdf.FDFSetStatus "Your business cards have been created."

'***   Write the data back to the buffer stream.
Response.ContentType = "application/vnd.fdf"
Response.BinaryWrite objFdf.FDFSaveToBuf

'***   Close the object and clean-up.
Set objFdf = Nothing
Set FdfAcX = Nothing


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 gordon_kent@yahoo.com.

PDF In-Depth Free Product Trials Ubiquitous PDF

Debenu Quick PDF Library

Get products to market faster with this amazing PDF developer SDK. Over 900 functions and an equally...

Download free demo

Scanned documents to high-compressed PDF

Color scanning is state-of-the-art today. Scanner hardware can produce color scans with high-quality from small MFP scanners up to high-end production scanners.

April 11, 2016
Platinum Sponsor

Search Planet PDF
more searching options...
Planet PDF Newsletter
Most Popular Articles
Featured Product

Debenu PDF Aerialist 12

The ultimate plug-in for Adobe Acrobat. Advanced splitting, merging, stamping, bookmarking, and link control. Take Acrobat to the next level.


Adding a PDF Stamp Comment

OK, so you want to stamp your document. Maybe you need to give reviewers some advice about the document's status or sensitivity. This tip from author Ted Padova demonstrates how to add stamps with the Stamp Tool along with related comments.