php 如何在php中填写PDF表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9139787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fill PDF form in php
提问by Sohail
I have many PDF forms, I would need to fill them each in php with the data I already have.
我有很多 PDF 表单,我需要用我已经拥有的数据在 php 中填写每个表单。
Here is just a sample PDF form, I want to fill this form using php. The data I already have in DB. I just want to fill this form and save it.
这只是一个示例 PDF 表单,我想使用 php 填写此表单。我已经在数据库中的数据。我只想填写此表格并保存。
I am working in PHP Zend Framework.
我在 PHP Zend 框架中工作。
All I want is when I would download those forms from my site, it would pre-fill all the fields in the pdf form with the information I already have.
我想要的只是当我从我的网站下载这些表格时,它会用我已有的信息预先填写 pdf 表格中的所有字段。
Please help me.
请帮我。
回答by Richard Ayotte
Calling pdftkis a nice way to accomplish this. I'll assume that you know how to execute an external programand leave that out.
调用pdftk是实现此目的的好方法。我假设您知道如何执行外部程序并忽略它。
First, create an FDFfile from your PDF.
首先,从您的 PDF创建一个FDF文件。
pdftk form.pdf generate_fdf output data.fdf
You can now use that as template. The sample that you provided looks like this:
您现在可以将其用作模板。您提供的示例如下所示:
%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
The fields are lines that start with /V ()
. Enter your desired values into those fields. For example:
字段是以/V ()
.开头的行。在这些字段中输入所需的值。例如:
%FDF-1.2
%<E2><E3><CF><D3>
1 0 obj
<<
/FDF
<<
/Fields [
<<
/V (February 4, 2012)
/T (Date)
...
Finally, merge the FDF with the PDF. Execute the command:
最后,将 FDF 与 PDF 合并。执行命令:
pdftk form.pdf fill_form data.fdf output form_with_data.pdf
If you don't need to keep the FDF and generated PDF files, you can simply pipe the data via stdin and stdout instead of using temp files.
如果您不需要保留 FDF 和生成的 PDF 文件,您可以简单地通过 stdin 和 stdout 管道数据,而不是使用临时文件。
回答by Peter Denev
I found this and it works for me. It is an unofficial patch for Zend to fill FDF Form Fields.Site with the discussion and patch
我找到了这个,它对我有用。它是 Zend 用于填充 FDF 表单字段的非官方补丁。带有讨论和补丁的站点
NO INSTALLATION, NO OUTSIDE PROGRAMS, NO COORDINATES
无需安装,无需外部程序,无需坐标
Use:
用:
$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');
If page no longer exists here is the patch for PDF.php (if someone have problems - write me privete message):
如果页面不再存在,这里是 PDF.php 的补丁(如果有人有问题 - 给我写私信):
--- 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.
回答by m3nda
I found fpdf library looking for the same on internet.
我发现 fpdf 库在互联网上寻找相同的库。
http://www.fpdf.org/en/script/script93.php
http://www.fpdf.org/en/script/script93.php
Code looks clean, easy to use and remember it, and works for pdf with forms (fillable).
代码看起来干净、易于使用和记住,并且适用于带有表单的 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();
?>
It comes with example that worked out of the box. Hope it helps.
它带有开箱即用的示例。希望能帮助到你。
回答by dbrumann
The Zend_Pdf-component can draw text in your pdf, but it is a bit clumsy as you have to specify the x- and y-coordinates on your pdf-page. Something like that:
该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');
I don't know if there are easy ways to find the exact coords, I usually play around until it fits.
我不知道是否有简单的方法可以找到确切的坐标,我通常会一直玩到合适为止。
回答by wizonesolutions
If you don't mind installing Drupal and setting up a simple site, you can install the Fill PDFmodule and configure it. If you already have the data, then this might be overkill, but if you can import that data into Drupal somehow (like with the Feedsmodule, for example), then you would save time on writing PDF-filling code. You can use a hosted service with that module or install stuff on your server.
如果您不介意安装 Drupal 并设置一个简单的站点,您可以安装Fill PDF模块并对其进行配置。如果您已经拥有数据,那么这可能有点过头了,但是如果您能以某种方式将该数据导入 Drupal(例如使用Feeds模块),那么您将节省编写 PDF 填充代码的时间。您可以使用该模块的托管服务或在您的服务器上安装东西。
Disclaimer: I maintain that module and run the hosted service.
免责声明:我维护该模块并运行托管服务。