输入PDF格式以填写PDFTK [英] Input PDF format for PDFTK form fill

查看:134
本文介绍了输入PDF格式以填写PDFTK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件是否有特定的输入格式,用于使用pdftk填写表单字段(通过PHP).

Is there a specific input format for the file for using pdftk to fill form fields (via PHP).

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

The input PDF I have is a Acrobat 9 created form.

我尝试了"pdftk in.pdf输出out.pdf",并尝试了flatten和drop_xfa选项的组合.

I've tried "pdftk in.pdf output out.pdf" and tried combinations of the flatten and drop_xfa options.

每次我都会遇到以下错误: "FilterFlateDecode:无效的流数据"

Everytime I get the following error : "FilterFlateDecode: invalid stream data"

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

Input text is plain text, and the form fields in the PDF are also text.

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

I'm aware I can save the PDF as an optimised PDF, but there's far too may options to be trying trial and error!

这是我要使用的PDF文件上的-info输出(我为此使用cpdf):

This is the -info output (I used cpdf for this) on the PDF file I wish to use:

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.

First you need to create temporary .fdf file with input data and then use pdftk to mearge template pdf and .fdf to create filled 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);

就这样.

这篇关于输入PDF格式以填写PDFTK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆