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;