TweetFollow Us on Twitter

Fill Online PDF Forms Using HTML Forms

Volume Number: 20 (2004)
Issue Number: 11
Column Tag: Programming

Fill Online PDF Forms Using HTML Forms

by Sid Steward

Collect data using an HTML form, To deliver a filled-out PDF form that works in Preview

Adobe's Portable Document Format (PDF) is really only as portable as the viewer used to read or print it. This has become an issue in recent years as the Adobe Reader (nee Acrobat Reader) has evolved to support some platforms better than others. Web publishers who desire maximum portability must now take stock: would this work on OS X or Linux as well as Windows? This issue is complicated by the rise of alternative PDF viewers such as Apple's Preview and alternative web browsers such as Konqueror.

Basic PDF viewing and printing is generally okay. Interactive PDF forms, however, are a different story. Adobe Reader on Windows integrates closely with popular web browsers, allowing a web developer to drive an interactive PDF form filling session using the web server (e.g., OS X users, however, won't have the same experience, nor will many Linux users.

One solution is to use HTML form features instead of PDF form features when collecting data. The web server can manage this data collection session, providing data validation and any necessary database access. When the form is complete, the web server can load the PDF form with the user's data, flatten the form, and then serve it to the user. "Flattening" makes the dynamic form data a permanent part of the page, so the resulting PDF will display properly using any PDF viewer.

Collecting data online using HTML forms is old hat. We'll discuss the part where you pack this data into the PDF form for delivery to your user. We'll also talk about how you can automatically convert a PDF form into an HTML form. My free, command-line tool, pdftk, makes both of these possible. We'll need to discuss how to get pdftk working on OS X (it also works on FreeBSD, Linux, Solaris and Windows). We should also touch on PDF forms.

PDF Forms

Using Adobe Acrobat 4, 5, or Acrobat 6.0 Pro (but not 6.0 Standard), you can add interactive form fields to PDF documents. PDF form fields closely resemble the form fields available to HTML form programmers. You have text boxes, check boxes, radio buttons, combo boxes, list boxes, and buttons. These can be further configured to suit your needs. For example, a text box can be configured to be multi-line or to mask password input, and buttons can be configured to submit the form data to a web server.

You can even program PDF forms using JavaScript, although the PDF document object model is quite different than the DOM familiar to web developers. To learn more about programming PDF using JavaScript, see the Acrobat JavaScript Object Specification ( developer/pdfs/tn/5186AcroJS.pdf) and the Acrobat JavaScript Scripting Guide (http://partners.adobe. com/asn/acrobat/sdk/public/docs/AcroJSGuide.pdf). We won't discuss using JavaScript with PDF forms, here. Though, I will mention the site html, where you will find JavaScript powered PDF games, such as Tic-Tac-Toe and Naval Battle.

For our purposes, the important thing about PDF forms is that you can permanently merge them with form data. You can do this using Acrobat, or you can use the free, command-line PDF Toolkit, pdftk.

Pdftk, the PDF Toolkit

Pdftk is a command-line program for manipulating PDF documents; it is free software. I created it one year ago to fulfill my own requirements. Since then, I have added features that I believed this free, general-purpose PDF tool should provide. It can:

  • Merge PDF Documents
  • Split PDF Pages into a New Document
  • Decrypt Input as Necessary (Password Required)
  • Encrypt Output as Desired
  • Fill PDF Forms with FDF Data and/or Flatten Forms
  • Apply a Background Watermark
  • Report on PDF Metrics such as Metadata, Bookmarks, and Page Labels
  • Update PDF Metadata
  • Attach Files to PDF Pages or the PDF Document
  • Unpack PDF Attachments
  • Burst a PDF Document into Single Pages
  • Uncompress and Re-Compress Page Streams
  • Repair Corrupted PDF (Where Possible)

The pdftk web site ( describes these features and explains how to get pdftk working on your system. Pdftk does not require Acrobat or Java. An OS X 10.3 installer is available for pdftk 1.11 from the site. Alternatively, you can build pdftk yourself, a non-trivial task described below. You must have version 1.11 if you want to automatically create an HTML form from a PDF form.

Under pdftk's hood, the iText PDF library does all the heavy lifting. iText is written in Java, but I prefer programming in C++. So I used GCJ, the Java compiler maintained as part of GNU GCC. GCJ allows me to compile iText and then link it with my C++ program. The result is a stand-alone binary that does not need Java. Very cool.

The problem is that your OS X system probably doesn't have GCJ. You must build GCJ (along with GCC) before you can build pdftk on OS X. Happily, John M. Gabriele provides instructions at: gcj_osx/gcj_on_osx.html. Brian D. Foy documents his experience building GCJ and pdftk at:

After building and installing GCC/GCJ, download and unpack the latest version of pdftk (currently 1.11) from If you configured GCC/GCJ with --prefix=/usr/local/gcj as John describes, then you won't need to edit the OS X Makefile. Otherwise you will need to edit Makefile.MacOSX so that TOOLPATH matches your location of GCC/GCJ.

After unpacking pdftk 1.11, change into the pdftk-1.11/pdftk directory and run make -f Makefile.MacOSX. It will take awhile to finish compiling. When it is done, move the resulting pdftk program to a convenient location in your $PATH, such as /usr/bin. Test pdftk by displaying its help page:

pdftk --help
and merging a couple PDFs together:
pdftk 1.pdf 2.pdf cat output 12.pdf

Note that you cannot name pdftk's output PDF so it overwrites an input PDF. Also, upon success, pdftk will overwrite files with its output without warning. Change this latter behavior by appending do_ask to the end of the command line, or change the ASK_ABOUT_WARNINGS setting in Makefile.MacOSX and recompile pdftk.

Before we begin using pdftk to fill PDF forms with data, let's talk about FDF.

Store Form Data Using FDF

FDF is Adobe's Forms Data Format, a file format for storing and managing PDF form data. FDF is usually plain text, so you can create it pretty easily using a text editor or your favorite scripting language. FDF is fully documented in section 8.6.6 of the PDF Reference, fourth edition. You can download the latest version of the PDF Reference from: pdf/specifications.jsp. Here is an example of an FDF file that assigns the value "San Francisco" to the PDF field named city:

1 0 obj
<< /FDF << /Fields [	<< /T (city)  /V (San Francisco) >>
                                 << /T (state) /V (California) >> ] >>
trailer << /Root 1 0 R >>

To simplify FDF creation, I created a PHP program called forge_fdf. It takes form data as name/value pairs and then spins out the matching FDF. The program logic should be easy to reproduce in any language. Visit to download the latest version. In PHP, you would use forge_fdf like so:

require_once( 'forge_fdf.php' );

// use this array for text fields, combo box, and list box form field values
$fdf_data_strings= array( 'city'  => 'San Francisco',
                          'state' => 'California' );

// use this array for check box and radio button values
$fdf_data_names= array();

// these aren't used in this example
$fields_hidden= array();
$fields_readonly= array();

$fdf= forge_fdf(	'',
                        $fields_readonly );

$fdf_fn= tempnam( '.', 'fdf' );
$fp= fopen( $fdf_fn, 'w' );
if( $fp ) {
   fwrite( $fp, $fdf );
   fclose( $fp );

   // serve PDF, but prompt the user to save it to disk
   header( 'Content-type: application/pdf' );
   header(	'Content-disposition: attachment; '.
                'filename=filled_form.pdf' );

   // our pdftk magic; "flatten" merges data with the page
   passthru(   'pdftk form.pdf fill_form '. $fdf_fn.
                                         ' output - flatten' );
   unlink( $fdf_fn ); // delete temp file
else { // error
   echo 'Error: unable to write temp fdf file: '. $fdf_fn;

One FDF peculiarity is that text field, combo box and list box form field values are represented as PDF "strings," where check box and radio button values are represented as PDF "names." For our purposes, names and strings are the same; they are just encoded a little differently in the FDF. That is why forge_fdf takes two arrays of data: fdf_data_strings and fdf_data_names; pack them appropriately. By default, check boxes and radio buttons use the values "Yes" and "Off" to represent their true and false states, respectively. The form designer can choose an alternative to "Yes," but "Off" always means false.

The arrays fields_hidden and fields_readonly have no role in this discussion, so you can ignore them.

Now things are beginning to come together. We have a PDF form, we have an FDF data file, and we can also see, above, that pdftk can merge these two files into a single, non-interactive PDF. Let's talk about that.

PDF Form Filling and Flattening with Pdftk

The pdftk command for filling a PDF form looks like this:

pdftk <input PDF form> fill_form <input FDF data> output <output PDF file> [flatten]

The PDF input, the FDF input, and the PDF output can be a filename, a hyphen (-), or "PROMPT." Passing a hyphen into pdftk instead of an input filename causes pdftk to look for data on stdin. Similarly, passing a hyphen into pdftk instead of an output filename causes pdftk to return data on stdout. You can see we used this latter technique in the snippet, above. Finally, you can pass "PROMPT" into pdftk if you would like pdftk to ask you for the necessary filename at run time.

If you include the flatten output option, then all form field data is converted into static page elements. All of the interactive form features are removed, so the result is a plain old PDF that any viewer can handle. If you omit the flatten option, then form fields are filled to match your input data, but they also remain interactive. You can flatten a PDF form at any time by running:

pdftk filled_form.pdf output flattened_form.pdf flatten

So, these are the back-end pieces to our workaround for online PDF forms. We can take form data, cast it into FDF, merge it with the PDF form, and then serve it to the user. Now let's look into creating the front-end HTML form. To help us along the way, we'll use pdftk to discover PDF form field information.

PDF Form Field Discovery with Pdftk

A PDF form can have dozens of interactive fields. Manually mirroring these fields in HTML would be cumbersome and error-prone. Instead, let's use one of pdftk's reporting features. You can learn everything you need to know about your PDF's interactive form fields by running:

pdftk form.pdf dump_data_fields > form.pdf.fields

This will create an easily parsible plain text report on your form's fields. The output might look like this:

FieldType: Text
FieldName: name_last
FieldNameAlt: Last Name
FieldFlags: 8392706
FieldJustification: Left
FieldMaxLength: 200
FieldType: Button
FieldName: previous1
FieldFlags: 0
FieldJustification: Left
FieldStateOption: Off
FieldStateOption: Yes
FieldType: Choice
FieldName: select_one
FieldFlags: 4587520
FieldValue: a
FieldValueDefault: c
FieldStateOption: a
FieldStateOption: b
FieldStateOption: c

You can see that the field named title has a maximum length of 200 characters, that a button named previous1 has two possible states: Off and Yes, and a combo box named select_one has three possible states: a, b, and c. Note that push buttons, check boxes and radio buttons all have a FieldType of Button. To tell them apart, you must consult the FieldFlags. Similarly, list boxes and combo boxes both have a FieldType of Choice. See section 8.6 of the PDF Reference for details on field flags and their meanings. We won't be bothering with them, here.

This plain text report should provide you with all the information you need to create an HTML interface to your form. For fun, let's use PHP to do this automatically. Here's a script that reads this text report and generates an HTML form to suit. If you added a "Short Description" to each field in Acrobat, then that text will appear as the FieldNameAlt entry in our report. Our script will use this information, if present, to label the HTML field.


// this function loads a data file created using pdftk dump_data_fields
load_field_data( $field_report_fn )
   $ret_val= array();

   $fp= fopen( $field_report_fn, "r" );
   if( $fp ) {
      $line= '';
      $rec= array();
      while( ($line= fgets($fp, 2048))!== FALSE ) {
         $line= rtrim( $line ); // remove trailing whitespace
         if( $line== '---' ) {
            if( 0< count($rec) ) { // end of record
               $ret_val[]= $rec;
               $rec= array();
            continue; // skip to next line

         // split line into name and value
         $data_pos= strpos( $line, ':' );
         $name= substr( $line, 0, $data_pos+ 1 );
         $value= substr( $line, $data_pos+ 2 );

         if( $name== 'FieldStateOption:' ) {
            // pack state options into their own sub-array
            if( !array_key_exists('FieldStateOption:',$rec) ) {
               $rec['FieldStateOption:']= array();
            $rec['FieldStateOption:'][]= $value;
         else {
            $rec[ $name ]= $value;
      if( 0< count($rec)) { // pack final record
         $ret_val[]= $rec;

      fclose( $fp );

   return $ret_val;
// open our web page; the form action is a script we provide, below
echo '<html>
<form method="POST" action="pdf_form_fill.php">
// create the file form.pdf.fields using pdftk's dump_data_fields
$field_arr= load_field_data( 'form.pdf.fields' );
foreach( $field_arr as $field ) { // iterate form fields
   echo '<tr><td>'; // one row per field
   if(array_key_exists('FieldNameAlt:', $field)) {
      // use human readable name, if available; you can add these in Acrobat
      echo $field['FieldNameAlt:'];
   else {
      echo $field['FieldName:'];
   echo '</td><td>';

   if( $field['FieldType:']== 'Text' ) {
      // construct an HTML text form field to match our PDF text form field;
      // cannot use periods in field names with PHP, so translate them to tildes
      echo   '<input type="text" name="'.
            strtr($field['FieldName:'],'.','~'). '" ';
      // text field default value
      if(array_key_exists('FieldValueDefault:', $field)) {
         echo 'value="'. $field['FieldValueDefault:']. '" ';
      // text field size and maxlength
      if(array_key_exists('FieldMaxLength:', $field)) {
         echo 'maxlength="'. $field['FieldMaxLength:']. '" ';
         if( $field['FieldMaxLength:']< 80 ) {
            echo 'size="'. $field['FieldMaxLength:']. '" ';
         else {
            echo 'size="80" ';
      echo '>';
   else if(array_key_exists('FieldStateOption:', $field)) {
      // use an HTML selection field for all other PDF form fields
      // (check boxes, radio buttons, list boxes, combo boxes);
      // cannot use periods in field names with PHP, so translate them to tildes
      echo   '<select name="'.
            strtr($field['FieldName:'], '.', '~'). '">';
      foreach( $field['FieldStateOption:'] as $option ) {
         echo '<option>'.$option.'</option>';
      echo '</select>';
   echo "</td></tr>\n";
// close our table and our HTML page; don't forget the submit button
echo '</table>
<input type="submit" value="Create PDF">

Now, we need a companion script that takes this submitted data, packs it into the PDF form and serves it to the user. This script is the pdf_form_fill.php action in our above HTML form. It looks much like our earlier form filling example:

require_once( 'forge_fdf.php' );

$fdf_data_strings= array();
$fdf_data_names= array();

// funny thing; for our purpose, we can get away with packing everything
// everything into fdf_data_strings; that's handy
foreach( $_POST as $key => $value ) {
   // translate tildes back to periods
   $fdf_data_strings[ strtr($key, '~', '.') ]= $value;
// ignore these in this example
$fields_hidden= array();
$fields_readonly= array();

$fdf= forge_fdf( '',
                        $fields_readonly );

$fdf_fn= tempnam( '.', 'fdf' );
$fp= fopen( $fdf_fn, 'w' );
if( $fp ) {
   fwrite( $fp, $fdf );
   fclose( $fp );

   header(   'Content-type: application/pdf' );
   header(   'Content-disposition: attachment; '.
             'filename=filled_form.pdf' );

   passthru(   'pdftk form.pdf fill_form '. $fdf_fn.
               ' output - flatten' );
   unlink( $fdf_fn ); // delete temp file
else { // error
   echo 'Error: unable to write temp fdf file: '. $fdf_fn;

When filling forms this way, it turns out you can pass everything into forge_fdf using the fdf_data_strings array; there's no need to use fdf_data_names. That's handy.

Now the Fun Begins

We have done it! We have created an HTML front-end to filling PDF forms. This is where the fun begins. You can now take everything you know about web programming, such as data validation and database access, and use it to fill PDF forms. Your users will be glad, too, because your resulting PDFs will work in alternative viewers such as Preview, and because you give them a filled-out PDF form for their records (which Adobe Reader does not provide).

To see an online example of these scripts, visit You will also find the code, quoted in this article, available for download.

Sid Steward is a longtime PDF service provider and software developer. He developed the free PDF Toolkit ( and wrote the book PDF Hacks (O'Reilly Media). You can reach him at


Community Search:
MacTech Search:

Software Updates via MacUpdate

FotoMagico 6.2.2 - Powerful slideshow cr...
FotoMagico lets you create professional slideshows from your photos and music with just a few, simple mouse clicks. It sports a very clean and intuitive yet powerful user interface. High image... Read more
Default Folder X 5.7 - Enhances Open and...
Default Folder X attaches a toolbar to the right side of the Open and Save dialogs in any OS X-native application. The toolbar gives you fast access to various folders and commands. You just click on... Read more
f.lux 42.1 - Adjusts the color of your d...
f.lux makes the color of your computer's display adapt to the time of day, warm at night and like sunlight during the day. Ever notice how people texting at night have that eerie blue glow? Or wake... Read more
Spotify - Stream music, creat...
Spotify is a streaming music service that gives you on-demand access to millions of songs. Whether you like driving rock, silky R&B, or grandiose classical music, Spotify's massive catalogue puts... Read more
Vitamin-R 4.15 - Personal productivity t...
Vitamin-R creates the optimal conditions for your brain to work at its best by structuring your work into short bursts of distraction-free, highly focused activity alternating with opportunities for... Read more
OfficeTime 2.0.628 - Easy time and expen...
OfficeTime is time and expense tracking that is easy, elegant and focused. Other time keepers are clumsy or oversimplified. OfficeTime balances features and ease of use, allowing you to easily track... Read more
Slack 4.28.182 - Collaborative communica...
Slack brings team communication and collaboration into one place so you can get more work done, whether you belong to a large enterprise or a small business. Check off your to-do list and move your... Read more
DEVONthink Pro 3.8.6 - Knowledge base, i...
DEVONthink is DEVONtechnologies' document and information management solution. It supports a large variety of file formats and stores them in a database enhanced by artificial intelligence (AI). Many... Read more
FileMaker Pro 19.5.4 - Quickly build cus...
FileMaker Pro is the tool you use to create a custom app. You also use FileMaker Pro to access your app on a computer. Start by importing data from a spreadsheet or using a built-in Starter app to... Read more
Backblaze - Online backup serv...
Backblaze is an online backup service designed from the ground-up for the Mac. With unlimited storage available for $6 per month, as well as a free 15-day trial, peace of mind is within reach with... Read more

Latest Forum Discussions

See All

SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for September 26th, 2022. In today’s article, we kick off the week with a bang. And by “bang", I mean four reviews. Family Man, Radiant Silvergun, The Legend of Heroes: Trails from Zero... | Read more »
‘Romancing SaGa: Minstrel Song Remastere...
Following its showing at TGS 2022, Square Enix has released a new gameplay trailer for the previously announced remaster of the PS2 remake of the Super Famicom original (yes) Romancing SaGa game, Romancing SaGa: Minstrel Song Remastered. | Read more »
Gamabilis reveal release date for realis...
Realistic Sims are very fun experiences and give gamers an excellent chance to experience other walks of life, and Gamabilis has released its hyper-real farm management game Roots of Tomorrow. Whilst the more arcade-type games like Stardew Valley... | Read more »
Best iPhone Game Updates: ‘Streets of Ra...
Hello everyone, and welcome to the week! It’s time once again for our look back at the noteworthy updates of the last seven days. We’ve got a nice mix of Apple Arcade, free-to-play, and even a proper paid game. We don’t see those often! So yes, a... | Read more »
The House of Da Vinci 3 launches on Andr...
Following its earlier release on iOS this year, The House of Da Vinci 3 has also officially launched on Android devices. Blue Brain Games' 3D puzzle adventure boasts an average rating of 4.9/5 and will give players the much-awaited conclusion to... | Read more »
‘Oxenfree: Netflix Edition’ Is Out Now o...
Over the weekend, Netflix and Nightschool Studio announced and released Oxenfree: Netflix Edition (Free) worldwide on iOS and Android. This new version of Oxenfree: Netflix Edition is a separate release, and the prior version that I own, is no... | Read more »
‘Genshin Impact’ Version 3.1 Update Pre-...
Genshin Impact (Free) version 3.1 ‘King Deshret and the Three Magi’ goes live in a few days across iOS, Android, PC, PS5, and PS4. As with prior updates, pre-installation for the upcate has just gone live a few days before release. | Read more »
We’re Digging ‘Shovel Knight Dig’ – The...
We spend the bulk of this week’s podcast talking about the new iPhone 14. Specifically, the iPhone 14 Pro Max which both Eli and myself picked up. The consensus seems to be: They’re great! They’re iPhones! We do lay down our hot takes on all the new... | Read more »
TouchArcade Game of the Week: ‘Loose Noz...
There aren’t a lot of stories like that of the development of Loose Nozzles, and of those games that do have an interesting development story, even fewer are actually decent games to play. Loose Nozzles nails both, though. The way it was created is... | Read more »
SwitchArcade Round-Up: ‘Shovel Knight Di...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for September 23rd, 2022. In today’s article, we’ve got the rest of this week’s releases to look at. There are actually a few big games today, including the hot-hot-hot Shovel Knight Dig... | Read more »

Price Scanner via

13-inch Apple MacBook Airs with M2 processors...
Amazon has 13″ MacBook Airs with M2 CPUs in stock today and on sale for $1099. Shipping is free. Their prices are $100 off Apple’s MSRP, and they are the lowest prices available for M2-powered Macs... Read more
AR Glasses That Work With Apple’s Hardware? T...
NEWS – Lenovo has created quite the spectacle(s) with its latest product. “Apple Glass” — the purported name of Apple’s forthcoming AR glasses — is not expected to be released until 2025 (at the... Read more
New today at Apple: 13-inch M2 MacBook Pros f...
Apple 13″ MacBook Pros with M2 CPUs in stock and available today starting at $1169, Certified Refurbished, and ranging up to $150 off original MSRP. These are the cheapest 13″ M2 MacBook Pros for... Read more
Sunday Sale: 13″ Apple M1 MacBook Air availab...
Amazon has Space Gray Apple 13″ M1 MacBook Airs on sale for $690.95 for an extremely limited time. Other models are on sale for $849. Their price for the Space Gray model is the cheapest we’ve ever... Read more
Use our exclusive Apple Price Trackers to fin...
Our Apple award-winning price trackers are the best place to look for the lowest prices and latest sales on all the latest Apple gear this season. Scan our price trackers for the latest information... Read more
New promo at Verizon: Get Apple Watch Series...
Purchase a new iPhone 14 at Verizon, and get an Apple Watch Series 8 for as low as $5 per month. $120 in promo credits for the Watch are spread over a 36 month term, reducing the price of the Watch... Read more
Visible drops prices on Apple iPhone 13 model...
Verizon’s low-cost wireless cell service, Visible has dropped prices on iPhone 13 models to new low prices starting at $599: – iPhone 13 Pro Max: starting at $980 + free $200 gift card – iPhone 13... Read more
Back in stock! 14″ MacBook Pros with Apple M1...
Amazon has restocked 14″ MacBook Pros M1 Pro CPUs for $400 off MSRP, starting at only $1599. Shipping is free. Be sure to make your purchase from Amazon rather than a third-party seller. Their prices... Read more
This is the final week to take advantage of A...
Apple’s Back to School promotion for 2022 ends on September 26, 2022. As part of this promotion, Apple will include a free $150 Apple Gift Card with the purchase of any MacBook Air, MacBook Pro, or... Read more
Mac Studio with M1 Max CPU back in stock toda...
Apple has the base standard-configuration Mac Studio available again in their Certified Refurbished section for $1799, and it’s in stock today. Each Mac Studio comes with Apple’s one-year warranty,... Read more

Jobs Board

Physician Assistant, Primary Care, *Apple*...
Physician Assistant, Primary Care, Apple Valley (1.07FTE) + Job ID: 65766 + Department: AV Primary Care + City: Apple Valley, MN + Location: HP - Apple Read more
Operations Manager - Mac/ *Apple* Engineerin...
…Responsible for the day-to-day activities relating to the engineering of Apple Macs in a complex, multi-platform environment. Demonstrates strong leadership, Read more
Lead Developer - *Apple* tvOS - Rumble (Uni...
…earnings, and positive sentiment About the role: We are looking for a Lead Apple tvOS Developer to join our application engineering team to expand our video centric Read more
Systems Administrator - *Apple* Devices / J...
…Administration **Duties and Responsibilities** + Configure and maintain the client's Apple Device Management (ADM) solution. The current solution is JAMF supporting Read more
Sr Product Manager, *Apple* TV Platforms -...
…an experienced senior product manager to drive the strategy and requirements for our Apple TV devices, acting as the champion and owner of the holistic experience in Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.