用图像填充 pdf 表单图像字段

Fill pdf form image field with image

我曾尝试使用 pdftk 将文本和图像填充到 PDF 表单中。 填写文本字段工作正常,但它似乎无法将图像添加到 PDF 表单图像字段。

有什么方法可以使用 pdftk 将图像添加到表单域吗?或者有其他方法吗?

这是我的 pdf:https://easyupload.io/b1emej

这是我的代码

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

可以通过 FDF 携带图像,但表单数据的目的是携带简单的文本对象,例如文本字段条目或其他评论。因此对于图像,它需要作为单独的注释标记,并且不确定是否可以将其附加到字段中。

这是添加到“干净”文件的图章(注意它在字段条目“下方”)

%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

图像可以是签名,也许可以将流转换为文本流用于简单的文本传输或没有二进制内容的文本模板。