2

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 .= "/T\n";
    $fdfContent .= "($key)\n";
    $fdfContent .= "/V\n";
    $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;
Vindur
  • 364
  • 3
  • 12
  • I would recommend using this library instead: http://www.fpdf.org/ - FPDF Library - I have used it several time, with images and text and what not...:) it supports JPG,PNG and GIF and pretty easy to use! – Shlomtzion Jan 24 '22 at 09:48
  • Thanks but it seems like it only has text support http://www.fpdf.org/en/script/script93.php – Vindur Jan 24 '22 at 10:27
  • Image support (JPEG, PNG and GIF) - from the fpdf.org... and again I have used this tool with barcode Images and a watermark even. – Shlomtzion Jan 24 '22 at 11:09
  • This is not just image support. This is support for filling PDF form fields with images. If you check the link i sent, they state that it only supports text and links to github which has checkbox support. – Vindur Jan 24 '22 at 11:33
  • Did you find a solution for this I'm also kind of need this feature currently using pdftk – Ruzaik Nazeer Jul 28 '22 at 16:20
  • @RuzaikNazeer Not really, i ended up rewriting my entire soultion and made the PDF manually with TCPDF instead. – Vindur Aug 16 '22 at 07:28

1 Answers1

1

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.

K J
  • 8,045
  • 3
  • 14
  • 36