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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1389964/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:21:46  来源:igfitidea点击:

Merge FDF data into a PDF file using PHP

phppdffdfxfdf

提问by James

Is it possible to merge FDF data with a PDF file using PHP alone? Or is there no option but to use a 3rd party command line tool to achieve this?

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

If that is the case can someone point me in the direction of one?

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

I am currently outputting the FDF file to the browser in the hope that it will redirect the user to the filled in PDF but for some people that is not the case. The FDF contents is being output to the screen, even though I am using header('Content-type: application/vnd.fdf');

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

回答by James

For future reference, it looks like there isn't a reliable way of doing it without a 3rd party app. Pdftk (http://www.accesspdf.com/pdftk/) ended up being my solution.

为了将来参考,如果没有 3rd 方应用程序,似乎没有可靠的方法可以做到这一点。Pdftk ( http://www.accesspdf.com/pdftk/) 最终成为我的解决方案。

I first generated the FDF file as before, and then merged it into my PDF file using the following PHP code

我首先像以前一样生成了 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;

It was much easier than I thought it would be. This instantly eliminates the need to hack around with headers and file extensions to ensure all browsers handle an FDF properly, as it simply makes the browser download the PDF file.

这比我想象的要容易得多。这立即消除了使用标题和文件扩展名来确保所有浏览器正确处理 FDF 的需要,因为它只是让浏览器下载 PDF 文件。

If you want the PDF output file to no longer be editable, use

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

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

Apologies if this is basic stuff, just thought I'd put it all in one place so that people don't go through the headache that I endured.

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

N.B. If your PATH variable is not set, you will need to use the full path to pdftk i.e.

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

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

回答by Aureltime

There is another way to day that not using passthru nor pdftk but just a script made in 2004 but still working well : forge_fdf

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

it helps you to build a fdf that you can incoporate straithly in your pdf, it means that you

它可以帮助您构建一个可以直接合并到 pdf 中的 fdf,这意味着您

Save this in a php file, let's say generatePdf.php

将其保存在一个 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 );

Making a link to Pathtoyourpdf/nameofpdffile.pdf#FDF=generatePdf.php will open your PDF file in browser (alternatively there is a way to save it to disk i think i remember) and the field email will be filled with data from MYSQL : mb_strtolower($row_delivreur['firstname']).'.'.mb_strtolower($row_delivreur['lastname']).'@gmail.com'

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

It works with checkboxes, radio button,... It opens well in firefox, it has to be tested with other browsers.

它适用于复选框、单选按钮、... 它在 Firefox 中打开良好,必须使用其他浏览器进行测试。

More infos on PDF HACKS

有关PDF 黑客的更多信息

回答by Tom

by far the easiest way I have found to install pdftk on centos:

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

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

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

then

然后

yum install pdftk

yum 安装 pdftk

and thats it!

就是这样!

回答by TomW

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

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

This works , the classes download are linked to from the web site too,

这有效,课程下载也链接到网站,

It requires no passthru/exec command and no additional extensions.

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

Edited to say, this doesn't work with newer pdf versions 1.5+, reverted to PDFTK, fiddly but works with all pdfs using the 'exec' command.

编辑说,这不适用于较新的 pdf 版本 1.5+,恢复为 PDFTK,繁琐但适用于使用“exec”命令的所有 pdf。