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

发布于 2024-08-03 15:25:41 字数 224 浏览 4 评论 0原文

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

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

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

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

罪#恶を代价 2024-08-10 15:25:41

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

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.

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

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.

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

    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.

    passthru("/usr/local/bin/pdftk file.pdf fill_form data.fdf output - flatten");
天荒地未老 2024-08-10 15:25:41

今天还有另一种方法,不使用 passthru 也不使用 pdftk,而只是使用 2004 年编写的脚本,但仍然运行良好: forge_fdf< /a>

它可以帮助您构建一个可以直接合并到 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 黑客

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

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

Save this in a php file, let's say 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'

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

More infos on PDF HACKS

じее 2024-08-10 15:25:41

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

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

然后

yum install pdftk

就这样了!

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

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

then

yum install pdftk

and thats it!

娇柔作态 2024-08-10 15:25:41

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

这有效,类下载也从网站链接到,

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

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文