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.

<title>Fill PDF Forms using FDF</title>


require_once 'createFDF.php';

// allow for up to 25 different files to be created, based on the minute
$min=date('i') % 25;

// get the FDF file contents

// Create a file for later use
echo 'Unable to create file: '.$fdf_file.'<br><br>';



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



<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>


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

function createFDF($file,$info){
$data="%FDF-1.2\n%âãÏÓ\n1 0 obj\n<< \n/FDF << /Fields [ ";
foreach($info as $field => $val){
foreach($val as $opt)
$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


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???


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.

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

Anonymous said...

for sure this is not ganna work

Chandu said...

Thanks man, it has been very useful for me. Great explanation.

Yijing Io said...

Hi it helps, but it cannot be opened by acrobat and is there a way to show the pdf file rather than save it first after submit? Thanks!

Post a Comment