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.
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.
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.
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.
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.
<?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;
}
?>
<?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;
}
?>
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.
2009-07-15 It has been a few years since I posted this and nearly as long since I have used it. However, I still get emails about this all the time. Apparently the programming is still useful for people to store the form data. I did receive an email about a month or so ago about a rogue line of code in the createXFDF function that was left over from the createFDF version of the function. Thank you AndreasDoöler for bringing this to my attention. I have now updated the function and download appropriately.
2006-09-08 For those of you having problems with using PDF files created from Acrobat 7 or Designer, you'll be please to hear that I have an update for you.
The problems were stemming from the way that the latest Acrobat, Acrobat Reader and Designer handle form data. Instead of using the previous FDF format, these programs are now defaulting to the newer XFDF file format. This is basically an XML-like file structure to define the values that would be filled into the fields.
2005-09-12 In the move from my last web server, I found that some of my files were corrupted, and this particular demonstrations was not working correctly. That should be all fixed up now.
Also, I did update the createFDF function a little as well. It turned out that Acrobat 7 was having problems importing data into the PDF form if there were spaces in the field definitions inside the FDF file. I have removed those spacer, and tested on a couple simple files. I will test on a better file with more options when I have a chance.
2005-04-21 - Thanks to Adrian James for submitting information about the FDF format of a multiple value selection field
(<select multiple="multiple">
HTML equivalent). The createFDF function has now been updated to support these fields as well.