For a few years starting back in 2005 or so I was looking for a way to use php to populate a pre-existing pdf document, as redrawing an existing pdf from scratch in php to have the exact same look as an existing pdf is one heluva task, esp when it comes to ever changing federal BK docs.
After an eternal search, I found this code in 2008, which 'works', but is unreliable, in the sense that it may not 'find' the fdf file (which has to be dropped into the awaiting pdf) so the user has to try a few times, the fdf loads into the browser cache and the thing finally fires:
$tstamp = date('mdYHis');
$test1 = mysql_real_escape_string($_POST['test1']);
$test2 = mysql_real_escape_string($_POST['test2']);
$fdf=dirname(__FILE__).'/doc_test/'.$tstamp.'.fdf';
if (file_exists($fdf)) {unlink($fdf);}
require_once 'createFDF_doc_test.php';
$pdf_file_doc_test=$link. '/forms/doc_test.pdf';
// allow for up to 25 different files to be created, based on the minute
$min=date('i') % 25;
$fdf_file_dt=dirname(__FILE__).'/doc_test/'.$tstamp.'.fdf';
if(isset($_POST['test1'])){
$_POST['test1']=$_POST['test1'];
// get the FDF file contents
$fdf_doc_test=createFDF_doc_test($pdf_file_doc_test,$_POST);
// Create a file for later use
if($fp=fopen($fdf_file_dt,'w')){
fwrite($fp,$fdf_doc_test,strlen($fdf_doc_test));
$CREATED=TRUE;
}else{
echo 'Unable to create file: '.$fdf_file_dt.'<br><br>';
$CREATED=FALSE;
}
fclose($fp);
}
Here are the contents of createFDF_doc_test.php:
<?php
function createFDF_doc_test($file,$info){
$data="%FDF-1.2
%âãÏÓ
1 0 obj
<<
/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.="]
/F (".$file.") /ID [ <".md5(time()).">
] >>".
"
>>
endobj
trailer
".
"<<
/Root 1 0 R
>>
%%EOF
";
return $data;
}
?>
What all this does is creates an fdf file and stores it in a folder on the server. The field names from a html form are matched with the pdf and the fdf acts as the conduit between the two.
The content of the fdf, which is always successfully saved to the server, will look something like this:
%FDF-1.2
%âãÏÓ
1 0 obj
<<
/FDF << /Fields [ <</T(test1)/V(Test)>><</T(test2)/V(Complete)>>]
/F (http://example.com/forms/doc_test.pdf) /ID [ <5d721ab873727ae4de3ebd21859bbc60>
] >>
>>
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF
The issue is the 'marriage' between the fdf and the pdf often doesn't fire because of some browser issue, or the timing the fdf is called, or something. Either way, the code is old, and I was hoping someone knew of a better, more efficient code that can quickly and buglessly move html form data, via php, into an awaiting pdf doc. I have been using the above for 8 years and I am tired of it.
We've been rebuilding our templating engine for generating reports based on user provided templates. After scouring the web for solutions a combination of pdftk + https://github.com/mikehaertl/php-pdftk rose as the best solution.
It has a very easy interface that enables populating pdf form using php arrays also has a good UTF8 support.
As seen in the documentation the simplest use case is:
use mikehaertl\pdftk\Pdf;
// Fill form with data array
$pdf = new Pdf('form.pdf');
$pdf->fillForm(array('name'=>'ÄÜÖ äüö мирано čárka'))
->needAppearances()
->saveAs('filled.pdf');
// Fill form from FDF
$pdf = new Pdf('form.pdf');
$pdf->fillForm('data.fdf')
->saveAs('filled.pdf');
I'm aware that this is not a help for the present problem, but believe this will give a more maintainable code.
But the real fun starts if you want to embed pictures, we had to resort to a helper python script to calculate the position of elements that we want to attach picture.