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

2022-08-30 19:54:00

是否可以单独使用PHP将FDF数据与PDF文件合并?还是别无选择,只能使用第三方命令行工具来实现此目的?

如果是这样的话,有人可以给我指出一个方向吗?

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


答案 1

为了将来参考,似乎没有可靠的方法可以在没有第三方应用程序的情况下做到这一点。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");

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

注意:贝如果未设置 PATH 变量,则需要使用 pdftk 的完整路径,即

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

答案 2

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

它可以帮助您构建一个fdf,您可以在pdf中隐藏,这意味着您

将其保存在php文件中,假设生成Pdf.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 的更多信息


推荐