【问题标题】:Add an image to an PDF with form fields将图像添加到带有表单域的 PDF
【发布时间】:2020-01-25 07:01:24
【问题描述】:

我正在尝试使用 PHP 来填充现有 PDF 上的表单域并向其中添加图像。

我找到了FPDM 库来填写表单字段:

$formInputArray = ['field1' => 'test value', 'field2' => 'second value'];

$pdf = new FPDM('orginal-file.pdf');
$pdf->getEntries($templatePDF, 'PDF');
$pdf->Load($formInputArray);
$pdf->Merge();
$pdf->Output('F', 'form-filled-file.pdf');

到目前为止有效。

在下一步中,我尝试将带有 Fpdi 类的图像添加到编辑后的文档中:

$pdf = new Fpdi();
$pdf->setSourceFile('form-filled-file.pdf');
$pageId = $pdf->importPage(1, \setasign\Fpdi\PdfReader\PageBoundaries::MEDIA_BOX);
$pdf->addPage();
$pdf->useTemplate($pageId);
$pdf->Image('test-image.jpg', 150*0.39, 150*0.39, 100*0.39);
$pdf->Output('F', 'finished-file.pdf');

问题是,Fpdi 正在将模板 pdf 结构转换为新的 pdf 结构。所以所有给定的表单字段都消失了。

所以问题是:

如何将图像添加到带有表单域的现有 PDF?

我还查看了 iText / PDFtk(服务器端)和 mPDF PHP 库,但由于 GPL 许可,它们不是正确的。

是否有其他方式或其他库来填充表单字段并将图像添加到 PHP 中的 PDF?

【问题讨论】:

  • F.e.使用 mPDF,您只需将一些带有 src 的 html img 标签添加到您的图像到您的 form-filled-file.pdf,可能与 fpdf 相同。
  • 没错。问题是,mPDF 使用的是 GPL 许可证,我不能使用这种许可证类型,就像我为 mPDF 库编写的那样。
  • 是否也欢迎付费解决方案?
  • @JanSlabon 是的,付费解决方案也是一种选择。

标签: php forms pdf


【解决方案1】:

我们(Setasign - 也是 FPDI 的作者)为这两个任务提供商业解决方案:用纯 PHP 填充 PDF 表单和用图像填充字段。

如果您使用 FPDM,您只能填写文本字段。替代品将是SetaPDF-FormFiller Lite Component。完整版还允许您填写其他字段类型,例如复选框或单选按钮组。

一个用图像填充单个文本字段和一个附加字段的简单示例是:

<?php

require_once('library/SetaPDF/Autoload.php');
// or if you use composer require_once('vendor/autoload.php');

// create a file writer
$writer = new SetaPDF_Core_Writer_File('image-in-form-field.pdf');
// get the main document instance
$document = SetaPDF_Core_Document::loadByFilename($filename, $writer);

// now get an instance of the form filler
$formFiller = new SetaPDF_FormFiller($document);

// Get the form fields of the document
$fields = $formFiller->getFields();

// Let's fill a field
$fields['Text Field']->setValue("Some example text.");

// Now prepare an appearance for the Logo field
// First of all let's get the annotation of the form field
$annotation = $fields['Logo']->getAnnotation();
// Remember the width and height for further calculations
$width = $annotation->getWidth();
$height = $annotation->getHeight();

// Create a form xobject to which we are going to write the image.
// This form xobject will be the resulting appearance of our form field.
$xobject = SetaPDF_Core_XObject_Form::create($document, array(0, 0, $width, $height));
// Get the canvas for this xobject
$canvas = $xobject->getCanvas();

// Let's create an image xobject
$image = SetaPDF_Core_Image::getByPath('Logo.png')->toXObject($document);

// scale image into available space and align in the center
if ($image->getHeight($width) >= $height) {
    $image->draw($canvas, $width / 2 - $image->getWidth($height) / 2, 0, null, $height);
} else {
    $image->draw($canvas, 0, $height / 2 - $image->getHeight($width) / 2, $width);
}

// Now add the appearance to the annotation
$annotation->setAppearance($xobject);

// Flatten all appearances to the pages content stream
$fields->flatten();

// finish the document
$document->save()->finish();

此脚本是this 演示的简短版本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-02
    • 2013-07-10
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多