【问题标题】:Fill pdf form image field with image用图像填充 pdf 表单图像字段
【发布时间】:2022-01-24 08:16:54
【问题描述】:

我尝试使用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;

【问题讨论】:

  • 我建议改用这个库:fpdf.org - FPDF 库 - 我已经用过几次了,有图像和文本等等……:) 它支持 JPG、PNG 和 GIF 以及很容易使用!
  • 谢谢,不过好像只有文字支持fpdf.org/en/script/script93.php
  • 图像支持(JPEG、PNG 和 GIF) - 来自 fpdf.org... 我再次使用这个工具与条形码图像和水印。
  • 这不仅仅是图像支持。这是对用图像填充 PDF 表单域的支持。如果您检查我发送的链接,他们声明它只支持文本和链接到具有复选框支持的 github。

标签: php pdf pdf-generation pdftk


【解决方案1】:

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

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

%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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多