如何在php中填写PDF表单

Soh*_*ail 20 php forms pdf zend-framework

我有很多PDF表单,我需要在php中填写我已有的数据.

这里只是一个示例PDF表单 ,我想用php填写此表单.我在DB中已有的数据.我只想填写此表并保存.

我正在使用PHP Zend Framework.

我想要的只是当我从我的网站下载这些表格时,它会预先填写pdf表格中的所有字段以及我已有的信息.

请帮我.

Ric*_*tte 32

调用pdftk是实现这一目标的好方法.我假设您知道如何执行外部程序并将其排除在外.

首先,从PDF 创建FDF文件.

pdftk form.pdf generate_fdf output data.fdf
Run Code Online (Sandbox Code Playgroud)

您现在可以将其用作模板.您提供的示例如下所示:

%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
Run Code Online (Sandbox Code Playgroud)

字段是以字母开头的行/V ().在这些字段中输入所需的值.例如:

%FDF-1.2
%<E2><E3><CF><D3>
1 0 obj 
<<
/FDF 
<<
/Fields [
<<
/V (February 4, 2012)
/T (Date)
...
Run Code Online (Sandbox Code Playgroud)

最后,将FDF与PDF合并.执行命令:

pdftk form.pdf fill_form data.fdf output form_with_data.pdf
Run Code Online (Sandbox Code Playgroud)

如果您不需要保留FDF并生成PDF文件,则只需通过stdin和stdout管道数据,而不是使用临时文件.


Pet*_*nev 16

我发现了这个,它对我有用.这是Zend填补FDF表格字段的非正式补丁.网站与讨论和补丁

没有安装,没有外部程序,没有坐标

使用:

$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');
Run Code Online (Sandbox Code Playgroud)

如果页面不再存在,这里是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.
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这比安装额外的软件要好得多.我不能完全相信这个补丁永远不会进入Zend Framework. (4认同)
  • @Jestep - 我刚刚在这里找到它:https://github.com/zf1/zend-pdf并且很高兴开箱即用,补丁已经在代码中了.使用Composer非常容易安装它和4个必需的Zend依赖项 - 运行`composer require zf1/zend-pdf`并将`require_once'<路径添加到Composer的vendor目录> /autoload.php';`到你需要的文件使用PDF功能. (3认同)

m3n*_*nda 7

我发现 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();
?>
Run Code Online (Sandbox Code Playgroud)

它带有开箱即用的示例。希望能帮助到你。

  • 为什么这很重要?非常关心更新的人往往不太关心“有效的东西”。你有遇到什么问题吗?我将它与 pdf 表单和原始 pdf 一起使用 coords 没有问题。我从来没有看过“是”最后一个版本,我只是看着“如果有效”:-P (5认同)
  • 有人拿走了该项目,现在正在此处更新:https://github.com/codeshell/fpdm (4认同)
  • 确认这有效。正如网站中所解释的,如果您的 PDF 是较新版本,只需执行“pdftk modele.pdf 输出 modele2.pdf”即可工作。 (2认同)