我有很多PDF表单,我需要用我已有的数据在php中填充它们。如何在php中填写PDF表格
这里只是一个sample PDF form,我想用php填充这个表单。我已经在数据库中存储的数据。我只想填写此表格并保存。
我正在使用PHP Zend Framework。
我想要的是当我从我的网站上下载这些表单时,它会用我已有的信息预先填充pdf表单中的所有字段。
请帮帮我。
我有很多PDF表单,我需要用我已有的数据在php中填充它们。如何在php中填写PDF表格
这里只是一个sample PDF form,我想用php填充这个表单。我已经在数据库中存储的数据。我只想填写此表格并保存。
我正在使用PHP Zend Framework。
我想要的是当我从我的网站上下载这些表单时,它会用我已有的信息预先填充pdf表单中的所有字段。
请帮帮我。
调用pdftk是完成此操作的好方法。我会假设你知道如何execute an external program并离开。
首先,从您的PDF创建一个FDF文件。
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
的领域是与/V()
启动线。输入你想要的值到这些字段中。例如:
%FDF-1.2
%<E2><E3><CF><D3>
1 0 obj
<<
/FDF
<<
/Fields [
<<
/V (February 4, 2012)
/T (Date)
...
最后,将FDF与PDF合并。执行命令:
pdftk form.pdf fill_form data.fdf output form_with_data.pdf
如果您不需要保持FDF和生成的PDF文件,你可以简单地通过管道stdin和stdout,而不是使用临时文件中的数据。
Zend_Pdf -component可以在您的PDF中绘制文本,但它有点笨拙,因为您必须在PDF页面上指定x和y坐标。类似的东西:
// 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');
我不知道是否有简单的方法来找到确切的坐标,我通常玩,直到它适合。
我发现这个,它适合我。这是Zend填补FDF表格字段的一个非官方补丁。 Site with the discussion and patch
NO安装,无需外部程序,没有坐标
用途:
$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的(如果有人有问题 - 写我privete消息):
--- 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.
谢谢你,这正是我一直在寻找的! – joren 2012-10-16 16:57:35
谢谢,这比安装额外的软件要好得多。我不能完全相信这个补丁从来没有把它变成Zend Framework。 – 2012-12-06 10:35:11
我刚刚下载了Zend Framework,并且补丁已经在那里...所以现在不需要补丁。 – 2015-10-29 14:07:49
我发现fpdf库寻找相同的互联网。
http://www.fpdf.org/en/script/script93.php
代码看上去干净,易于使用,并记住它,并用表格(可填写)工程PDF。
<?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();
?>
它带有一个开箱即用的例子。 希望它有帮助。
非常感谢您的回答。我需要你多一点,是否应该在服务器上安装PDFTK?我没有太多的PDF工作,是否有一些例子,或者你能解释一点,我怎么能执行外部程序?请帮助我。 – Sohail 2012-02-04 20:43:32
是的,在服务器上安装pdftk。你可以在这里找到[说明](http://www.pdflabs.com/docs/install-pdftk/)。要从PHP执行系统命令,只需调用exec函数即可。例如。 '<?php exec('/ usr/bin/pdftk form.pdf fill_form data.fdf output form_with_data.pdf'); ?>' – 2012-02-04 22:42:13
Thanx Richard,我会试试。太感谢了。 – Sohail 2012-02-05 08:18:41