Skip to content
Advertisement

Fill pdf form image field with image

I have tried to use pdftk to fill a PDF form with text and images. Filling out text fields works fine, but it cant seem to add an image to an PDF form image field.

Is there any way to add an image to a form field with pdftk ? Or any other way to do this?

Heres my pdf: https://easyupload.io/b1emej

Heres my code

$templatePath = '/path/to/pdf/clean.pdf';
$fdfHeader = <<<FDF
        %FDF-1.2
        %,,oe"
        1 0 obj
        <<
        /FDF
        <<
        /Fields [
        FDF;

$formFields = [
    'Text3' => 'Test value',
    'Billede4_af_image' => '/path/to/image/test.png'
];

$fdfContent = '';
foreach ($formFields as $key => $field) {
    $fdfContent .= "<<n";
    $fdfContent .= "/Tn";
    $fdfContent .= "($key)n";
    $fdfContent .= "/Vn";
    $fdfContent .= "($value)n";
    $fdfContent .= ">>n";
}

$fdfFooter = <<<FDF
        ]
        >>
        >>
        endobj
        trailer

        <<
        /Root 1 0 R
        >>
        %%EOF;
        FDF;

$fdfFilePath = '/tmp/fdffile.fdf';
$fdfContent = $fdfHeader . $fdfContent . $fdfFooter;
file_put_contents($fdfFilePath, $fdfContent);

$pdf = System::exec("pdftk $templatePath fill_form $fdfFilePath output - flatten");

header('Content-Type: application/pdf');
header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
echo $pdf;

Advertisement

Answer

It is possible to carry an image via FDF however the aim of Forms Data is to carry simple text objects such as text field entries or other comments. So for an image it needs to be as separate annotation stamp and unsure if that can be attached to a field as such.

Here is a stamp added to the “clean” file ( note it is “under” the field entries)

enter image description here

%FDF-1.4
%âãÏÓ
1 0 obj
<<
/FDF <<
/Annots [2 0 R]
/F (clean.pdf)
/ID [<BBB71BA5F45E7149AAF360F13C5FB61A> <B7FE6051163F4F46B31241A24E573F8B>]
/UF (clean.pdf)
>>
/Type /Catalog
>>
endobj
2 0 obj
<<
/AP <<
/N 3 0 R
>>
/C [1 0 0]
/CreationDate (D:20220127132137Z)
/F 4
/IT /Stamp
/M (D:20220127132137Z)
/Name /image.08000006
/NM (1a68adc3-7c8a-45a2-b661dc794cc8ea96)
/Page 0
/Rect [0 399 412 792]
/Subj (Stamp)
/Subtype /Stamp
/T (K)
/Type /Annot
>>
endobj
3 0 obj
<<
/BBox [0 0 412 393]
/Filter /FlateDecode
/Length 34
/Resources <<
/XObject <<
/Im0 4 0 R
>>
>>
/Subtype /Form
/Type /XObject
>>
stream
xÚ+ä214R0...
                      ... continues lots of binary lines ...
(F8F8F1Â1ŠŽŽQ¾KþM

endstream
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF

The image could be a signature and perhaps the stream be converted to a textual stream for simple text transfer or text templating without binary content.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement