Using HTML forms to fill in PDF fields with PHP and FDF

Using a simple HTML form and a small PHP function, it is possible to fill in the fields of a PDF form file for viewing without having to install any PHP libraries or modules. This is accomplished by using a pre-made PDF file with all the form fields necessary and FDF files.

By taking values provided via a GET or POST web request from an HTML form or hyperlink, we can generate an FDF file that, when opened, will provide the values to fill the PDF form fields in the original document.

Tutorial

I have now posted a tutorial on how to use the programming found in this article for those of you who need just a little extra help implementing this.

Creating the PDF file

First, you need to have created your PDF form file. I do this using the form tools in Adobe Acrobat. Remember to name each field in your document, you will need those names later when you create the HTML form.

Creating the HTML form

Next, you need to create the form fields to fill out in an HTML form. Below is an example that fits with the above PDF file. Note that the field names in your HTML form must match the field names in your PDF file.

Name
Title
Email
Address
City
State
Zip
Phone
Fax

Processing the POST request

When the form is submitted, the $_POST array is passed to a function along with the location of the PDF file. The function returns the contents of an FDF file. This can them be used to write an FDF file to download an view.

The code behind this

<?php
/*
KOIVI HTML Form to FDF Parser for PHP (C) 2004 Justin Koivisto
Version 2.1.2
Last Modified: 9/12/2005

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or (at
    your option) any later version.

    This library is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this library; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

    Full license agreement notice can be found in the LICENSE file contained
    within this distribution package.

    Justin Koivisto
    justin.koivisto@gmail.com
    http://koivi.com
*/

/*
*   createFDF
*
*   Takes values submitted via an HTML form and fills in the corresponding
*   fields into an FDF file for use with a PDF file with form fields.
*
*   @param  $file   The pdf file that this form is meant for. Can be either
*                   a url or a file path.
*   @param  $info   The submitted values in key/value pairs. (eg. $_POST)
*   @result Returns the FDF file contents for further processing.
*/
function createFDF($file,$info){
    
$data="%FDF-1.2\n%âãÏÓ\n1 0 obj\n<< \n/FDF << /Fields [ ";
    foreach(
$info as $field => $val){
        if(
is_array($val)){
            
$data.='<</T('.$field.')/V[';
            foreach(
$val as $opt)
                
$data.='('.trim($opt).')';
            
$data.=']>>';
        }else{
            
$data.='<</T('.$field.')/V('.trim($val).')>>';
        }
    }
    
$data.="] \n/F (".$file.") /ID [ <".md5(time()).">\n] >>".
        
" \n>> \nendobj\ntrailer\n".
        
"<<\n/Root 1 0 R \n\n>>\n%%EOF\n";
    return 
$data;
}
?>

Code for using XFDF formatted data

<?php
/*
KOIVI HTML Form to FDF Parser for PHP (C) 2004 Justin Koivisto
Version 1.0
Last Modified: 2009-07-15

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or (at
    your option) any later version.

    This library is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this library; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

    Full license agreement notice can be found in the LICENSE file contained
    within this distribution package.

    Justin Koivisto
    justin.koivisto@gmail.com
    http://koivi.com
*/

/**
 * createXFDF
 * 
 * Tales values passed via associative array and generates XFDF file format
 * with that data for the pdf address sullpiled.
 * 
 * @param string $file The pdf file - url or file path accepted
 * @param array $info data to use in key/value pairs no more than 2 dimensions
 * @param string $enc default UTF-8, match server output: default_charset in php.ini
 * @return string The XFDF data for acrobat reader to use in the pdf form file
 */
function createXFDF($file,$info,$enc='UTF-8'){
    
$data='<?xml version="1.0" encoding="'.$enc.'"?>'."\n".
        
'<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">'."\n".
        
'<fields>'."\n";
    foreach(
$info as $field => $val){
        
$data.='<field name="'.$field.'">'."\n";
        if(
is_array($val)){
            foreach(
$val as $opt)
                
$data.='<value>'.$opt.'</value>'."\n";
        }else{
            
$data.='<value>'.$val.'</value>'."\n";
        }
        
$data.='</field>'."\n";
    }
    
$data.='</fields>'."\n".
        
'<ids original="'.md5($file).'" modified="'.time().'" />'."\n".
        
'<f href="'.$file.'" />'."\n".
        
'</xfdf>'."\n";
    return 
$data;
}
?>

Code Download

Because of the number of requests I have received for help getting this to work or copies of the code and PDF files, I have created a ZIP archive of this entire directory and am making it available for download (~180K - updated 2006-09-08).

Inside this zip file you will also find a file named process-xfdf.php which contains sample code for sending results as an email attachment as well as sending the output directly to the browser.

Updates