Php 使用表单字段将图像添加到PDF

Php 使用表单字段将图像添加到PDF,php,forms,pdf,Php,Forms,Pdf,我正在尝试使用PHP填充现有PDF上的表单字段,并向其中添加图像 我找到了FPDM库来填充表单字段: $formInputArray = ['field1' => 'test value', 'field2' => 'second value']; $pdf = new FPDM('orginal-file.pdf'); $pdf->getEntries($templatePDF, 'PDF'); $pdf->Load($formInputArray); $pdf-&g

我正在尝试使用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添加图像?

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

如果使用FPDM,则只能填写文本字段。替代者将是最重要的。完整版本还允许您填写其他字段类型,如复选框或radiobutton组

使用图像填充单个文本字段和附加字段的简单示例如下:

<?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();

F.e.使用mPDF,您只需将一些带有
src
的html
img
标签添加到您的
表单填充文件中。pdf
,可能与
fpdf
相同。没错。问题是,mPDF正在使用GPL许可证,而我不能使用此许可证类型,正如我为mPDF库所写的。付费解决方案也受欢迎吗?@JanSlabon是的,付费解决方案也是一种选择。