输入PDFTK表格填写的PDF格式

时间:2014-03-04 11:49:52

标签: php forms pdf pdftk

该文件是否有特定的输入格式,可以使用pdftk填充表单字段(通过PHP)。

我输入的PDF是Acrobat 9创建的表单。

我已尝试过“pdftk in.pdf output out.pdf”,并尝试了flatten和drop_xfa选项的组合。

  

每次我收到以下错误:   “FilterFlateDecode:无效的流数据”

输入文本是纯文本,PDF中的表单字段也是文本。

我知道我可以将PDF保存为优化的PDF,但尝试尝试和错误的选项还有很多选择!

这是我希望使用的PDF文件的-info输出(我使用了cpdf):

Encryption: Not encrypted
Permissions: 
Linearized: false
Version: 1.7
Pages: 1
Title: 
Author: 
Subject: 
Keywords: 
Creator: Adobe InDesign CS6 (Macintosh)
Producer: Adobe PDF Library 10.0.1
Created: D:20140219113433Z
Modified: D:20140304104401Z

水稻

1 个答案:

答案 0 :(得分:2)

首先,您需要使用输入数据创建临时.fdf文件,然后使用pdftk来模拟模板pdf和.fdf以创建填充的pdf。

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



$fileName = "Template_PDF.pdf";
$fdf_file = "temporary.fdf";
$result_file = "Result.pdf";
$arr['a_variable'] = 'value or a variable';


$fdf_data = createFDF($fileName, $arr);


if($fp=fopen($fdf_file,'w'))
{
    fwrite($fp,$fdf_data,strlen($fdf_data));
}
else
{
    echo 'Unable to create file: '.$fdf_file.'<br>';
}
fclose($fp);
exec("pdftk ".$fileName." fill_form ".$fdf_file." output ".$result_file );

就是这样。