如何在php中填写PDF表单
我有很多PDF表单,我需要用php将它们填入我已有的数据 这里只是一个例子,我想用php填写这个表格。我已经有了数据库中的数据。我只想填写这张表格并保存它 我在PHP Zend框架中工作 我想要的是当我从我的网站下载这些表格时,它会用我已有的信息预先填充pdf表格中的所有字段 请帮助我。组件可以在pdf中绘制文本,但它有点笨拙,因为您必须在pdf页面上指定x坐标和y坐标。诸如此类:如何在php中填写PDF表单,php,forms,zend-framework,pdf,Php,Forms,Zend Framework,Pdf,我有很多PDF表单,我需要用php将它们填入我已有的数据 这里只是一个例子,我想用php填写这个表格。我已经有了数据库中的数据。我只想填写这张表格并保存它 我在PHP Zend框架中工作 我想要的是当我从我的网站下载这些表格时,它会用我已有的信息预先填充pdf表格中的所有字段 请帮助我。组件可以在pdf中绘制文本,但它有点笨拙,因为您必须在pdf页面上指定x坐标和y坐标。诸如此类: // This is the pdf-file you want to fill out... $pdf = Ze
// This is the pdf-file you want to fill out...
$pdf = Zend_Pdf::load('/path/to/pdf-template.pdf')
$page = $pdf->pages[0]; // Use first page
$page->drawText("firstname lastname", 200, 600);
...
$pdf->pages[0] = $page;
// be sure to write to a new file, not the original empty form.
$pdf->save('/path/to/pdf-output.pdf');
我不知道是否有简单的方法可以找到准确的坐标,我通常会一直玩到合适为止。打电话是一个很好的方法。我想你知道怎么做,但不必说了
首先,从PDF创建一个文件
pdftk form.pdf generate_fdf output data.fdf
现在可以将其用作模板。您提供的示例如下所示:
%FDF-1.2
%<E2><E3><CF><D3>
1 0 obj
<<
/FDF
<<
/Fields [
<<
/V ()
/T (Date)
>>
<<
/V /
/T (CheckBox2)
>>
<<
/V /
/T (CheckBox3)
>>
<<
/V /
/T (CheckBox4)
>>
<<
/V /
/T (CheckBox5)
>>
<<
/V ()
/T (Your_Last_Name)
>>
<<
/V ()
/T (Your_First_Name)
>>
<<
/V /
/T (CheckBox1)
>>]
>>
>>
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF
最后,将FDF与PDF合并。执行以下命令:
pdftk form.pdf fill_form data.fdf output form_with_data.pdf
如果您不需要保留FDF和生成的PDF文件,只需通过stdin和stdout来传输数据,而不必使用临时文件。我发现了这一点,它适合我。这是Zend填充FDF表单字段的非官方补丁 没有安装,没有外部程序,没有坐标 使用: 如果页面不再存在,这里是PDF.php的补丁(如果有人有问题,请给我写privete消息):
我发现fpdf图书馆在互联网上寻找同样的东西 代码看起来干净,易于使用和记忆,适用于pdf格式的表单(可填充)
它附带了一个开箱即用的例子。
希望有帮助。非常感谢您的回答。我需要从你们那个里得到更多,PDFTK服务器上应该安装什么东西吗??我很少使用PDF,有没有一些例子,或者你能再解释一下如何执行外部程序??请帮助我。是的,在服务器上安装pdftk。你可以找到那个。要从PHP执行系统命令,只需调用exec函数。比如说<代码>Thanx Richard,我试试看。非常感谢。@RichardAyotte这是一个很好的发现:)我必须做完全相同的事情,有一个转折。我还需要在其中一个表单字段中包含一个图像。我该怎么做呢?我还没有做过图片,但找到了这条解释它的线索。我现在没有时间更新答案。但这并不是填写PDF表格,这对于平面PDF来说毫无疑问是有效的,但现在对于容易出错的PDF表格来说是有效的。我知道,我应该提到这一点。但据我所知,这是使用Zend_pdf将文本转换为pdf的唯一方法。谢谢你,这正是我想要的!谢谢,这比安装额外的软件要好得多。我不太相信这个补丁没有进入Zend Framework。我刚刚下载了Zend Framework,补丁已经在那里了。。。所以现在不需要修补。@GonzaloBrusco它是完整的软件包还是github的Zend PDF库?我希望在一个项目中只使用zend pdf,但它看起来有太多zend依赖项,它本身几乎是无用的。从我看到的情况来看,这里绝对没有任何代码。@Jestep-我刚刚在这里找到了它:它正在开箱即用,补丁已经在代码中了。使用Composer安装它和4个必需的Zend依赖项非常容易-运行
Composer require zf1/Zend pdf
和添加require_once'上一个版本是2016年。为什么相关?对更新如此关注的人往往对“正常工作的事情”不太关心。你有没有遇到什么问题?我使用它与pdf表单和原始pdf使用coords没有问题。我从来没有看过上一个版本的“was”,我只是看了看“if worked”:-PI甚至用它来连接pdf的页面,使用from_模板功能,能够打印150页文档和150个1页文件。有些系统不允许您选择150个文件然后打印它们。上一次我在ie:Windows中看到它时,我注意到我只能拾取15个。。。奇怪的我用一个宏记录器把它们全部打印出来,但最后将它们与fpdf结合起来,效率更高,更干净,更不容易出错。好的,确认一下。如网站中所述,如果您的PDF是较新版本,只需执行pdftk modele.PDF output modele2.PDF
即可。有人参与了该项目,现在正在此处更新:
pdftk form.pdf fill_form data.fdf output form_with_data.pdf
$pdf = Zend_Pdf::load('input-file-containing-form.pdf');
$pdf->setTextField('name', 'Someone');
$pdf->setTextField('address', '1 Main Street');
$pdf->setTextField('city', 'Cyberspace');
$pdf->save('outputfile.pdf');
--- Pdf.php.orig 2009-11-15 17:52:57.000000000 +0100
+++ Pdf.php 2010-01-07 04:05:23.000000000 +0100
@@ -202,6 +202,13 @@
* @var array
*/
protected static $_inheritableAttributes = array('Resources', 'MediaBox', 'CropBox', 'Rotate');
+
+ /**
+ * List of form fields
+ *
+ * @var array - Associative array, key: name of form field, value: Zend_Pdf_Element
+ */
+ protected $_formFields = array();
/**
* Request used memory manager
@@ -315,6 +322,7 @@
$this->_loadNamedDestinations($this->_trailer->Root, $this->_parser->getPDFVersion());
$this->_loadOutlines($this->_trailer->Root);
+ $this->_loadFormfields($this->_trailer->Root);
if ($this->_trailer->Info !== null) {
$this->properties = $this->_trailer->Info->toPhp();
@@ -557,6 +565,61 @@
$this->_originalOpenOutlinesCount = $root->Outlines->Count->value;
}
}
+
+ /**
+ * Load form fields
+ * Populates the _formFields array, for later lookup of fields by name
+ *
+ * @param Zend_Pdf_Element_Reference $root Document catalog entry
+ */
+ protected function _loadFormFields(Zend_Pdf_Element_Reference $root)
+ {
+ if ($root->AcroForm === null || $root->AcroForm->Fields === null) {
+ return;
+ }
+
+ foreach ($root->AcroForm->Fields->items as $field)
+ {
+ if ( $field->FT->value == 'Tx' && $field->T !== null ) /* We only support fields that are textfields and have a name */
+ {
+ $this->_formFields[$field->T->value] = $field;
+ }
+ }
+
+ if ( !$root->AcroForm->NeedAppearances || !$root->AcroForm->NeedAppearances->value )
+ {
+ /* Ask the .pdf viewer to generate its own appearance data, so we do not have to */
+ $root->AcroForm->add(new Zend_Pdf_Element_Name('NeedAppearances'), new Zend_Pdf_Element_Boolean(true) );
+ $root->AcroForm->touch();
+ }
+ }
+
+ /**
+ * Retrieves a list with the names of the AcroForm textfields in the PDF
+ *
+ * @return array of strings
+ */
+ public function getTextFieldNames()
+ {
+ return array_keys($this->_formFields);
+ }
+
+ /**
+ * Sets the value of an AcroForm text field
+ *
+ * @param string $name Name of textfield
+ * @param string $value Value
+ * @throws Zend_Pdf_Exception if the textfield does not exist in the pdf
+ */
+ public function setTextField($name, $value)
+ {
+ if ( !isset($this->_formFields[$name]))
+ throw new Zend_Pdf_Exception("Field '$name' does not exist or is not a textfield");
+
+ $field = $this->_formFields[$name];
+ $field->add(new Zend_Pdf_Element_Name('V'), new Zend_Pdf_Element_String($value) );
+ $field->touch();
+ }
/**
* Orginize pages to tha pages tree structure.
<?php
/***************************
Sample using a PHP array
****************************/
require('fpdm.php');
$fields = array(
'name' => 'My name',
'address' => 'My address',
'city' => 'My city',
'phone' => 'My phone number'
);
$pdf = new FPDM('template.pdf');
$pdf->Load($fields, false); // second parameter: false if field values are in ISO-8859-1, true if UTF-8
$pdf->Merge();
$pdf->Output();
?>