使用 pdftk 填写表单字段(通过 PHP)的文件是否有特定的输入格式。
我的输入 PDF 是 Acrobat 9 创建的表单。
我尝试过“pdftk in.pdf output out.pdf”并尝试了 flatten 和 drop_xfa 选项的组合。
Everytime I get the following error : "FilterFlateDecode: invalid stream data"
输入文本是纯文本,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
稻谷
最佳答案
首先,您需要使用输入数据创建临时 .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);
就是这样。
关于php - 输入 PDF 格式以进行 PDFTK 表单填写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22171124/