prodigitalson,我想为您发布此解决方案,以防您仍然对想要找到答案感到好奇。它仅适用于针对 1.5 版 (Acrobat 6.0) 优化的 PDF,但它确实运行良好。它是 Zend Framework 1.12.3 的一个非官方补丁,用于填充 PDF 表单字段。有讨论和补丁的站点
无需安装,无需外部程序,无需坐标
首先使用以下内容更新您的 php.ini 文件(注意:当我上传这些更改时,我将不得不在我的实际 Web 服务器上更改我的 .ini 文件):
include_path = ".;C:\wamp\www\includes"
请注意:我将所有库内容从 'ZendFramework-1.12.3\library' 文件夹移到一个名为 Zend 的文件夹中:C:\wamp\www\includes\Zend
只是为了方便引用库(这就是您所需要的全部)。
然后在您的 php 文件中(我使用了“DIRECTORY_SEPARATOR”,以便您可以在 Win 或 Unix 服务器上使用它,并且我不必根据我的 .php 文件的位置进行任何代码更改,我只需要进行服务器配置更改):
require_once('Zend'.DIRECTORY_SEPARATOR.'Loader'.DIRECTORY_SEPARATOR.'Autoloader.php');
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('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 文件,这样它就不会阻塞我的服务器:attach_mailer_class.php可在此处获得版权(c) 2006 年,奥拉夫·莱德勒):
// Write $_POST form data to associative array
foreach ($_POST as $key => $value) {
$NameArray[$key] = $value;
}
// Path to PDF application fillable file
$pdf_path = dirname(__FILE__) . "\\docs";
$pdf_filename = 'employment_applicationJBzend.pdf';
$pdf_file_path = $pdf_path . "\\" . $pdf_filename;
// Path to PDF application file save location
$result_path = dirname(__FILE__) . "\\results";
$result_filename = ucfirst($_POST['first_name']) . ucfirst($_POST['last_name']) . $filedatetime . '.pdf';
$result_file_path = $result_path . "\\" . $result_filename;
//Filling PDF fields | Example: $pdf->setTextField('position_applied_for', 'IT Manager');
$pdf = Zend_Pdf::load($pdf_file_path);
foreach ($NameArray as $key1 => $value) {
foreach($ExceptionArray as $key2 => $value)
{
if($key1 == $ExceptionArray[$key2]){
$boolSetText = false;
break;
}else{
$boolSetText = true;
}
}
if($boolSetText){
$pdf->setTextField($key1, $NameArray[$key1]);
}
}
$pdf->save($result_file_path);
//Create and send message using 'attach_mailer_class.php
$email = new attach_mailer($from_name, $from_mail, $mail_to, $cc = "", $bcc = "", $subject);
$email->text_body = $message;
$email->add_attach_file($result_file_path);
// $email->add_attach_file("ip2nation.zip");
$email->process_mail();
unlink($result_file_path);
如果此处页面不再存在,则为 PDF.php 的补丁(如果您不知道如何运行实际补丁,则基本上您将浏览您的 PDF.php 文件并替换下面所有带有“+”的行在它们前面。你可以通过位置标签'@@ -202,6 +202,13 @@'找到它们的位置,它就在第 200 行附近,然后只需复制并粘贴以用新代码替换旧代码) :
--- 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.