php 使用php动态插入内容到pdf文件中

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

Dynamically insert content into pdf files with php

phppdf

提问by Ian

I have an ebook in word that I convert to PDF before distributing to my clients. I'd like to dynamically insert their email address into all links in the ebook to allow them access to the members-only content on my site, and I'd like to do this on the fly, as part of the book download process.

我有一本电子书,在分发给我的客户之前,我将其转换为 PDF。我想将他们的电子邮件地址动态插入到电子书中的所有链接中,以允许他们访问我网站上的会员专享内容,并且我想即时执行此操作,作为图书下载过程的一部分。

I've briefly looked at http://us.php.net/pdfand FPDF, but I was wondering what specific technique I'd use to insert this data.

我已经简要地查看了http://us.php.net/pdf和 FPDF,但我想知道我会使用什么特定的技术来插入这些数据。

I was thinking I'd insert an email token string where I want the email address to go, and then use some function to update those tokens in the PDF document.

我想我会在我想要电子邮件地址的地方插入一个电子邮件令牌字符串,然后使用一些函数来更新 PDF 文档中的这些令牌。

Can anyone point me in the right direction? I have php experience, but not with editing / generating pdf documents from php.

任何人都可以指出我正确的方向吗?我有 php 经验,但没有从 php 编辑/生成 pdf 文档的经验。

EDIT: Yes, this commercial script http://www.setasign.de/products/pdf-php-solutions/setapdf-linkreplacer/does exactly what I needed.

编辑:是的,这个商业脚本http://www.setasign.de/products/pdf-php-solutions/setapdf-linkreplacer/正是我所需要的。

回答by Ian

So far it's looking like this is my best bet:

到目前为止,看起来这是我最好的选择:

http://www.setasign.de/products/pdf-php-solutions/setapdf-linkreplacer/

http://www.setasign.de/products/pdf-php-solutions/setapdf-linkreplacer/

Trying an eval copy of it, will update post with results.

尝试它的评估副本,将使用结果更新帖子。

回答by Anthony

Without using the Adobe LiveCycle Designer, the easiest way to generate a custom PDF is to use an FDF file. There are tons of ways to do this, one of which is to download binaries from Adobe and install them on your server. But none of that is really needed. All you need is a pdf with fillable forms and a simple script that makes and FDF file. The FDF simply holds the data that needs to be filled in and a pointer to the pdf file to be filled in. I use this for our timesheets at work. The data goes into a web form, but must come out static and ugly and using a paper from from 30 years ago. Here's what your fdf file will look like (both with code and raw):

在不使用 Adob​​e LiveCycle Designer 的情况下,生成自定义 PDF 的最简单方法是使用 FDF 文件。有很多方法可以做到这一点,其中之一是从 Adob​​e 下载二进制文件并将它们安装在您的服务器上。但这些都不是真正需要的。您所需要的只是一个带有可填写表格的 pdf 和一个制作 FDF 文件的简单脚本。FDF 只保存需要填写的数据和指向要填写的 pdf 文件的指针。我在工作中使用它作为时间表。数据进入网络表单,但必须静态且丑陋,并使用 30 年前的论文。这是您的 fdf 文件的外观(包括代码和原始文件):

 $file = "http://www.example.com/blankpdfform.pdf";
 $data = "%FDF-1.2\n%a??ó\n1 0 obj\n<< \n/FDF << /Fields [ ";
 foreach($datafields as $field => $val) {

      $data.='<< /T ('.$field.') /V ('.trim($val).')>> ';
 }

 $data.="] \n/F (".$file.") /ID [ <".md5(time()).">\n] >>".
      " \n>> \nendobj\ntrailer\n".
      "<<\n/Root 1 0 R \n\n>>\n%%EOF\n";

The end result being:

最终结果是:

 %FDF-1.2\n%a??ó\n1 0 obj\n<< \n/FDF << /Fields [<< /T (email) /V ([email protected])>>
 /F ("http://www.example.com/blankpdfform.pdf") /ID [ <"SomeUniqueID">
 ] >> \nendobj\ntrailer<<
 /Root 1 0 R 
 >>
 %%EOF

回答by jab11

you can do this with FPDI extension for FPDF http://www.setasign.de/products/pdf-php-solutions/fpdi/

你可以用 FPDF 的 FPDI 扩展来做到这一点http://www.setasign.de/products/pdf-php-solutions/fpdi/

it enables fpdf to import existing pdf files, though I'm not sure how can one replace links.

它使 fpdf 能够导入现有的 pdf 文件,但我不确定如何替换链接。

I'd say your best shot would be to generate the whole thing in php, or just save it in html, replace links in html, then convert html to pdf.

我想说你最好的办法是在 php 中生成整个东西,或者只是将它保存在 html 中,替换 html 中的链接,然后将 html 转换为 pdf。

回答by staktrace

If you have the PDF template with the email token stored in a file on the webserver, you can do this fairly easily. First you need to read in the file using PHP. You can do this using the file_get_contents method. Then use str_replace to replace the email token with the actual email. Finally, serve the file with the correct content-type.

如果您将带有电子邮件令牌的 PDF 模板存储在网络服务器上的文件中,则可以很容易地做到这一点。首先,您需要使用 PHP 读入文件。您可以使用 file_get_contents 方法执行此操作。然后使用 str_replace 将电子邮件令牌替换为实际电子邮件。最后,使用正确的内容类型提供文件。

$pdf = file_get_contents( 'template.pdf' );
$pdf = str_replace( '__EMAIL__TEMPLATE__', $userEmail, $pdf );
header( 'Content-type: application/pdf' );
print $pdf;

Doc links: http://ca2.php.net/manual/en/function.file-get-contents.phphttp://ca2.php.net/manual/en/function.str-replace.php

文档链接:http: //ca2.php.net/manual/en/function.file-get-contents.php http://ca2.php.net/manual/en/function.str-replace.php

(I haven't actually tried this and you may run into some issues since PDF is a binary format, but in theory it should work...)

(我还没有真正尝试过这个,你可能会遇到一些问题,因为 PDF 是一种二进制格式,但理论上它应该可以工作......)