Domanda

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

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

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

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

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

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!

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

Paddy

È stato utile?

Soluzione

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

thats it.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top