PHP不能自动填充PDF格式的数据

时间:2013-02-11 23:32:03

标签: php pdf

我一直在使用这个问题中给出的建议:

Merge FDF data into a PDF file using PHP

确定如何使用从数据库中提取的数据预填充pdf表单但不将数据提取到文件。我正在Mountain Lion上运行php 5.3.15,我正在使用以下链接将pdf与fdf组合在一个表格页面上,我想让用户点击并将它们带到一个填充的pdf文件中:

something.pdf#FDF=generatePdf.php

generatePdf.php如下所示:

<?php
$pdf_form_url= "";
require_once('forge_fdf.php');

$fdf_data_names= array(); // none of these in this example
$fdf_data_strings= array(); // none of these in this example

$fdf_data_strings['form1[0].#subform[0].q1a_LastName'] = "Smith";
$fields_hidden= array();
$fields_readonly= array();

// set this to retry the previous state
$retry_b= false;

header( 'content-type: application/vnd.fdf' );

echo forge_fdf( $pdf_form_url,
    $fdf_data_strings, 
    $fdf_data_names,
    $fields_hidden,
    $fields_readonly );

?>

,当我通过pdftk命令行运行它时,FDF文件看起来像这样:

FieldType: Text
FieldName: form1[0].#subform[0].q1a_LastName[0]
FieldNameAlt: Enter Family Name
FieldFlags: 0
FieldJustification: Left
FieldMaxLength: 35

但是,当我运行它时,表单不会自动填充。我尝试了很多变化,包括单独使用q1a_LastName,我仍然以空表格结束。 forge_fdf.php位于同一目录中。

更新: 我还尝试将合并FDF数据中概述的传统方法转换为我使用PHP链接的PDF问题,这意味着我不仅将用户引导至pdf文件,而且还让用户下载pdf文件。 。在那种特殊情况下,我会收到一条消息,说pdf是空的。我检查了创建的原始FDF文件,它似乎填充了数据。在任何一种情况下,我都没有得到我需要的东西。

2 个答案:

答案 0 :(得分:0)

经过多次试验和错误后,我意识到自动填充到链接到pdf的URL的最佳方法是为上面的代码定义$ pdf_form_url变量。此代码将回显fdf文件的详细信息。如果您明确指定了pdf网址,php和您的浏览器将采取必要的步骤将您链接到网页浏览器中的pdf,其中嵌入的字段信息来自fdf输出。

答案 1 :(得分:0)

所以我偶然发现了这个线程,即使我填充$ pdf_form_url变量也无法使它工作。这是我的代码:

require_once('forge_fdf.php');  

// leave this blank if we're associating the FDF w/ the PDF via URL
$pdf_form_url= "http://test.pdf";
// $pdf_form_url= "";


// default data; these two arrays must ultimately list all of the fields
// you desire to alter, even if you just want to set the 'hidden' flag;
//
//
$fdf_data_names= array(); // none of these in this example
$fdf_data_strings= array(); // none of these in this example
$fdf_data_strings['Address, Row 1']="XXXX Balboa Arms Drive #XXX, San Diego CA 01234";

$fields_hidden= array();
$fields_readonly= array();

// set this to retry the previous state
$retry_b= false;

header( 'content-type: application/vnd.fdf' );

echo forge_fdf( $pdf_form_url,
        $fdf_data_strings, 
        $fdf_data_names,
        $fields_hidden,
        $fields_readonly );

这是我想用pdftk dump_data_fields更新的表单字段:

FieldType: Text
FieldName: Address, Row 1
FieldNameAlt: (Address, &lt;Row 1&gt;)
FieldFlags: 0
FieldJustification: Center