我有许多PDF表格,我需要使用已有的数据在php中填写每一个表格。
这是一个样例PDF表格,我想使用php填写这个表格。我已经在数据库中拥有所需的数据,只需要填写并保存该表格。
我正在使用PHP Zend框架。
我的目标是在从网站上下载这些表格时,让所有字段都预先填充为我已有的信息。
请帮助我。
我有许多PDF表格,我需要使用已有的数据在php中填写每一个表格。
这是一个样例PDF表格,我想使用php填写这个表格。我已经在数据库中拥有所需的数据,只需要填写并保存该表格。
我正在使用PHP Zend框架。
我的目标是在从网站上下载这些表格时,让所有字段都预先填充为我已有的信息。
请帮助我。
调用 pdftk 是一个不错的方法来实现这个。我假设你知道如何 执行外部程序 并略过此步骤。
首先,从你的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)
...
pdftk form.pdf fill_form data.fdf output form_with_data.pdf
我找到了这个方法并且对我有用。它是一个非官方的修补程序,可以填充FDF表单字段的Zend。讨论和修补程序的网站
无需安装、无需外部程序、无需坐标。
使用方法:
$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.
composer require zf1/zend-pdf
并将 require_once '<path to Composer's vendor dir>/autoload.php';
添加到需要使用PDF功能的文件中。 - Jan Żankowski<?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();
?>
这个例子可以直接使用,希望它能够对你有所帮助。
ObjStm
对象流。 - Sumit WadhwaZend_Pdf组件可以在您的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');
我不知道是否有简单的方法来找到精确的坐标,通常我会试着调整直到它符合要求。
(
,)
和反斜杠\\
。有很多PHP库可以为您处理,比如这个https://github.com/admonkey/php-fdf。 - Jeff Puckett.pdf
和.fdf
文件的例子吗? 目前我使用passthru('pdftk file.pdf fill_form file.xdfd output - flatten');
,但是我想要避免在磁盘上留下这两个文件。 - luckydonald/V /Yes
。 - Jay Dadhania