rss
email
twitter
facebook

Wednesday, April 7, 2010

How to fill PDF Forms in PHP using FDF

If you ever wanted to fill PDFs in PHP this tutorial will help you make it the easy way. The ingredients that you will need are:

  • PHP enabled web server (I am currently using WAMP)
  • PDF Form Maker (you can use adobe acrobat pro or scribus that is freeeeee!!)
  • Some php knowledge is preferred
First let’s create our PDF Form to be filled in scribus. You may want to activate the grid (View -> Show Grid) to help align the text. Using the insert text frame button insert two text frames and edit their text to First Name and Last Name. You may edit the text in the text frames right-clicking the frame and selecting Edit Text (Ctrl+Y). Then add two Text Fields with the Text Field button found on the drop down with the OK Button.




Double click the Text Fields to edit that name of the field. Set the first field to FirstName and the second field to LastName.



To save the PDF go to File -> Export -> Save as PDF…




Once you save the PDF we can proceed to the PHP part.

Now we are going to create our PHP Form that is going to fill the PDF with the following code. The files must be save as index.php and on a folder called test.


<html>
<head>
<title>Fill PDF Forms using FDF</title>
</head>
<body>

<?php

if(isset($_POST['FirstName'])){
require_once 'createFDF.php';
$pdf_file='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].'test.pdf';

// allow for up to 25 different files to be created, based on the minute
$min=date('i') % 25;
$fdf_file=dirname(__FILE__).'/posted-'.$min.'.fdf';

// get the FDF file contents
$fdf=createFDF($pdf_file,$_POST);

// Create a file for later use
if($fp=fopen($fdf_file,'w')){
fwrite($fp,$fdf,strlen($fdf));
$CREATED=TRUE;
}else{
echo 'Unable to create file: '.$fdf_file.'<br><br>';
$CREATED=FALSE;
}
fclose($fp);
}


if(isset($CREATED)){

?>

<script type="text/javascript">
<!--
function myPopup() {
window.open( "<?php echo str_replace(dirname(__FILE__).'/','',$fdf_file) ?>" )
}
//-->

myPopup();
</script>                
<?php
}        

?>

<h2>Fill the form:</h2>

<form method="post" action="/test/">

First Name:<input size="20" maxlength="20" type="text" name="FirstName" value="<?php if(isset($_POST['FirstName'])) echo $_POST['FirstName']; ?>" /><br><br>
Last Name:<input size="20" maxlength="20" type="text" name="LastName" value="<?php if(isset($_POST['LastName'])) echo $_POST['LastName']; ?>" /><br><br>

<button type="submit" value="Generar Documento">Generate PDF</button>
</form>

</body>
</html>



Make another PFP File called createFDF.php. This file is going to be called by index.php to generate the FDF File.

<?php
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;
}
?>


Make sure your files are in the test folder.



Now to test, fill out the PHP Form and hit the button.



It should generate an FDF file with the field and automatically called the PDF and Fill them.




It is very important that the name of the fields in PHP and the name of the fields in the PDF match exactly, they are case sensitive and if they don’t match it will make the data not appear on the PDF.

You can download the source code here

7 comments:

Stefan Zaharia said...
This comment has been removed by the author.
Anonymous said...

Hi. How can I do that when I press Generate PDF button to show open/download pop-up? Thanks.

Tad Neim said...

Hi, it is working in IE but not in chrome or firefox. Any ideas how to make it compatible???

Thanks

Anonymous said...

Is it necessary to install PDFTK ?
I am not able to see form data in pdf file (names are fine).
I am redirected to test.pdf.

Jonathan Matthews said...

fdf_file is an empty argument................

Anonymous said...

Hi. This service here allows you to easily edit your PDF documents.
http://goo.gl/hlHpFE

You can fill out PDF form, save it, fax it, and email it.

Anonymous said...

for sure this is not ganna work

Post a Comment