I have a PDF with editable fields. I wish to pass values from an HTML form into this PDF. I've tried using FPDF and it works, however the fields in the pdf are no longer editable after passing the values to the PDF.
Another drawback is that we have to specify exact coordinates for each field when passing the values to the PDF. Any ideas on other tools I can use to keep the "PDF EDITABLE"?
I used the following code to generate pdf http://www.setasign.de/products/pdf-php-solutions/fpdi/demos/simple-demo/
I was unable to find a PDF filling method natively in PHP that I really liked. Instead, I use PHP to generate XFDF, then I use pdftk to push it into the fillable PDF. (Note that this code requires your PDF's fields to be named.)
Here is an example function to generate XFDF from an associative array:
function forge_xfdf($file,$info,$enc='UTF-8'){
$data='<?xml version="1.0" encoding="'.$enc.'"?>'."\n".
'<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">'."\n".
'<fields>'."\n";
foreach($info as $field => $val){
$data.='<field name="'.$field.'">'."\n";
if(is_array($val)){
$data.='<</T('.$field.')/V[';
foreach($val as $opt)
$data.='<value>'.$opt.'</value>'."\n";
}else{
$data.='<value>'.$val.'</value>'."\n";
}
$data.='</field>'."\n";
}
$data.='</fields>'."\n".
'<ids original="'.md5($file).'" modified="'.time().'" />'."\n".
'<f href="'.$file.'" />'."\n".
'</xfdf>'."\n";
return $data;
}
Then, I write that XFDF to a temporary file.
$empty_form = '/path/to/fillable/pdf/form.pdf'
$fdf_filename = tempnam(PDF_TEMP_DIR, 'fdf');
$output_filename = tempnam(PDF_TEMP_DIR, 'pdf');
$fdf_data = forge_xfdf($empty_form, $data, 'UTF-8');
if($fdf_fp = fopen($fdf_fn, 'wb')){
fwrite($fdf_fp, $fdf_data);
fclose($fdf_fp);
$command = '/usr/local/bin/pdftk "'.$empty_form.'" fill_form "'.$fdf_filename.'" output "'.$output_file.'" dont_ask';
passthru($command);
// SEND THE FILE TO THE BROWSER
unlink($output_file);
unlink($fdf_filename);
}
If you do want a non-editable PDF, add the word flatten
in the command before the word dont_ask
.
If you aren't attached to filling a PDF form, you could generate the form in HTML as well, then use dompdf to convert from HTML to PDF.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With