Using HTML forms to fill in PDF fields with PHP

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.

What This Does and Does NOT Do

Ever since I posted this article (Aug 12, 2003!), I've gotten emails about this over and over again, so I am going to state it right up front, at the top of the article now that I am rewriting it: This source code does not create a PDF file with all the posted data in it. The function included in this article is for generating XFDF data. This is the type of data that is used with PDF forms to import into the fields. The original function (createFDF) has been removed from the article to remove confusion for readers. That function is far out of date and the format has been all but abandoned for future development. Adobe moved on to the XFDF format for transporting data between PDF documents. That is what the createXFDF function returns.

The Functional Code

Below you will find the most recent copy of the PHP function. I've used this extensively with Acrobat PDF files as well as a number of PDF forms created with LiveCycle for testing. There may be some things that I have not come across yet, and I appreciate and feedback that you may have to help improve this function for those that are looking for ways to fill a PDF file with values posted from the web.

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>' .
                    htmlentities( $opt, ENT_COMPAT, $enc ) .
                    '</value>' . "\n";
        }
        else
        {
            $data .= '<value>' .
                htmlentities( $val, ENT_COMPAT, $enc ) .
                '</value>' . "\n";
        }
        $data .= '</field>' . "\n";
    }
    $data .= '</fields>' . "\n" .
        '<ids original="' . md5( $file ) . '" modified="' .
            time() . '" />' . "\n" .
        '<f href="' . $file . '" />' . "\n" .
        '</xfdf>' . "\n";
    return $data;
}

Additional Code Examples

Over the past nearly decade I've had many requests to help get this work for individuals. Most of them have the same requests like sending the XFDF (or more common the filled-in PDF) as email attachments to a pre-defined address. I've taken a little extra time while rewriting and testing for this article to put together some samples that may help you with getting some things to work for you. The samples here are not meant to be used in a production website, they are simply examples of how to use the function to get your desired end results. They don't have any of the normal input filtering or type checking that I'd normally use (as you should!).

Suggestions or Improvements?

Do you have something that may benefit others regarding what you find on this page? Would you like to share it? If so, simply contact me and I will review it and possibly post it here.

Did this article help you save time for a project? Did this information help you create your website or web application? If so, please send a donation my way - no amount is too small to show your appreciation.