- 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
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
12 comments:
Hi. How can I do that when I press Generate PDF button to show open/download pop-up? Thanks.
Hi, it is working in IE but not in chrome or firefox. Any ideas how to make it compatible???
Thanks
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.
fdf_file is an empty argument................
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.
for sure this is not ganna work
Thanks man, it has been very useful for me. Great explanation.
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!
The FDF file is created and has the correct data but when the pdf loads in the browser, the text fields of the pdf are blank. In addition they cannot be typed in. Anybody have any ideas?
For a quick editable pdf document you can use pdf viewer online application where you can fill up the required information and also add company’s logo and other icons.
Respect and that i have a tremendous present: Renovation House Company total home renovation
Post a Comment