使用PHP将FDF数据合并为PDF文件

时间:2009-09-07 15:50:58

标签: php pdf fdf xfdf

是否可以仅使用PHP将FDF数据与PDF文件合并?或者除了使用第三方命令行工具来实现这个目的之外别无选择吗?

如果是这种情况,有人能指出我的方向吗?

我目前正在将FDF文件输出到浏览器,希望它将用户重定向到填写的PDF,但对于某些人来说并非如此。 FDF内容正在输出到屏幕,即使我使用标题('Content-type:application / vnd.fdf');

4 个答案:

答案 0 :(得分:31)

为了将来参考,看起来如果没有第三方应用程序,就没有可靠的方法。 Pdftk(http://www.accesspdf.com/pdftk/)最终成为我的解决方案。

我首先像以前一样生成FDF文件,然后使用以下PHP代码将其合并到我的PDF文件中

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="Download.pdf"');
passthru("pdftk file.pdf fill_form data.fdf output - ");
exit;

这比我想象的容易得多。这样就可以立即消除对标题和文件扩展名的攻击,以确保所有浏览器都能正确处理FDF,因为它只是让浏览器下载PDF文件。

如果您希望PDF输出文件不再可编辑,请使用

    passthru("pdftk file.pdf fill_form data.fdf output - flatten");

道歉,如果这是基本的东西,只是想我把它放在一个地方,这样人们就不会经历我忍受的头痛。

N.B。如果未设置PATH变量,则需要使用pdftk的完整路径,即

    passthru("/usr/local/bin/pdftk file.pdf fill_form data.fdf output - flatten");

答案 1 :(得分:3)

还有另一种方式,不使用passthru或pdftk,而只是在2004年制作但仍然运作良好的脚本:forge_fdf

它可以帮助你建立一个你可以在你的pdf中直接包含的fdf,这意味着你

将其保存在php文件中,假设为generatePdf.php

require_once('forge_fdf.php');  

// leave this blank if we're associating the FDF w/ the PDF via URL
$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['email']=mb_strtolower($row_delivreur['firstname']).'.'.mb_strtolower($row_delivreur['lastname']).'@gmail.com';

$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 );

链接到Pathtoyourpdf / nameofpdffile.pdf#FDF = generatePdf.php将在浏览器中打开您的PDF文件(或者有一种方法可以将其保存到磁盘,我想我记得)并且字段电子邮件将填充数据来自MYSQL:mb_strtolower($row_delivreur['firstname']).'.'.mb_strtolower($row_delivreur['lastname']).'@gmail.com'

它适用于复选框,单选按钮,...它在Firefox中打开很好,必须与其他浏览器一起测试。

PDF HACKS

上的更多信息

答案 2 :(得分:2)

到目前为止,我发现在centos上安装pdftk的最简单方法是:

安装rpmforge repo - http://wiki.centos.org/AdditionalResources/Repositories/RPMForge#head-5aabf02717d5b6b12d47edbc5811404998926a1b

然后

yum install pdftk

就是这样!

答案 3 :(得分:1)

http://www.setasign.de/products/pdf-php-solutions/fpdi/demos/concatenate-fake/

这样做,类下载也从网站链接到

它不需要passthru / exec命令,也不需要额外的扩展名。

编辑说,这不适用于较新的pdf版本1.5+,恢复为PDFTK,但是使用'exec'命令可以与所有pdf一起使用。

相关问题