En primer lugar hemos de generar un fdf a partir de unos datos, ya sean extraidos de una base de datos o de un formulario.
En nuestro caso lo haremos todo en una función del controller que llamaremos function generate_pdf()
// we will use this array to pass to the createFDF function
$data=array();
$data['form1[0].#subform[0].field1[0]']=$item->field2;
$data['form1[0].#subform[0].field2[0]']=$item->field2;
// if we got here, the data should be valid,
// time to create our FDF file contents
// file name will be <the current timestamp>.fdf
$fdf_file=time().'.fdf';// the directory to write the result in
$fdf_dir=JPATH_SITE.DS.$config->get('fdf_folder');// need to know what file the data will go into
$pdf_doc=$config->get('PDF_file_location');
// generate the file content
$fdf_data=Helper::createFDF($pdf_doc,$data);// this is where you'd do any custom handling of the data
// if you wanted to put it in a database, email the
// FDF data, push ti back to the user with a header() call, etc.
// write the file out
if($fp=fopen($fdf_dir.'/'.$fdf_file,'w')){
fwrite($fp,$fdf_data,strlen($fdf_data));
}else{
die('Unable to create file: '.$fdf_dir.'/'.$fdf_file);
}
fclose($fp);
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="doc_to_print.pdf"');
passthru("pdftk ".$pdf_doc." fill_form results/".$fdf_file." output - ");
exit;
No debemos olvidarnos de poner la funcion createFDF en el Helper
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;
}
voilà !
Nota1: El pdftk debe estar instalado, recordamos ...
apt-get install pdftk
Nota2: Debemos saber el nombre de los campos tal y como estan declarados en el pdf. En caso de que no los sepamos, siempre nos queda el pdftk
pdftk file.pdf dump_data_fields