Fill out a PDF Form programmatically using FDF in PHP
Filed under PHP
I was looking for a solution to produce an exact official form in PDF format with the field values entered by a user in a web form. Here is what I found:
Create a PDF Form using Acrobat Writer. Note down the field names.
Get form values from user using a web form.
Generate the FDF(http://www.verypdf.com/pdfform/fdf.htm) file by passing the values along with the field names.
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; } ?>
Use pdftk(http://www.pdflabs.com) to fill out the PDF form using the FDF file.
header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename="Download.pdf"'); passthru("pdftk file.pdf fill_form data.fdf output - flatten"); exit;