PHP:填写PDF表格 [英] PHP: Fill PDF form

查看:94
本文介绍了PHP:填写PDF表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PDF文档,其中包含带有几个表单字段的表单.我需要执行以下操作:

I have a PDF document which contains form with couple of form fields. I need to do following:

  • 加载该PDF文档
  • 遍历该文档的所有表单字段,并用数据填充它
  • 下载已填充的拼合文档

我看过的库很少,但是大多数库都依赖于PDFtk.但是这种情况是我无法在服务器上安装PDFtk.

I have seen few of libraries, but most of them are relying on the PDFtk. But the case is that I don't have the possibility to install PDFtk on the server.

是否有任何库允许这种操作并且是纯" php,所以我不必在服务器上安装任何东西?

Is there any library which allows such action and is "pure" php so I don't have to install anything on server?

推荐答案

您可以使用

You can use the SetaPDF-FormFiller component for this task (not free! And I'm related to the company behind). Let $data be your array of data, a simple usage example would be:

// Load that PDF document
$document = SetaPDF_Core_Document::loadByFilename('path/to.pdf');
$formFiller = new SetaPDF_FormFiller($document);

// Itearate through all the form fields of that document and fill it up with the data
$fields = $formFiller->getFields();
foreach ($fields->getNames() as $fieldName) {
    $field = $fields->get($fieldName);
    $field->setValue($data[$fieldName]);
}

// flatten document
$fields->flatten();

// Download filled flatten document
$writer = new SetaPDF_Core_Writer_Http('result.pdf');
$document->setWriter($writer);
$document->save()->finish();

所有操作均在纯PHP中完成,并且仅需要启用常见的PHP扩展即可(请参见系统要求).

It is all done in pure PHP and requires only common PHP extensions to be enabled (see system requirements).

这篇关于PHP:填写PDF表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆