首页 文章

输入 PDFTK 表格填写的 PDF 格式

提问于
浏览
1

是否有文件的特定输入格式使用 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 回答

  • 3

    首先,您需要使用输入数据创建临时.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 );
    

    而已。

相关问题