我有很多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