用 PHP 填写 PDF 表单

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

Filling PDF Forms with PHP

phppdf

提问by mno

Are there PHP libraries which can be used to fill PDF forms and then save (flatten) them to PDF files?

是否有可用于填写 PDF 表单然后将它们保存(拼合)为 PDF 文件的 PHP 库?

采纳答案by bmb

The libraries and frameworks mentioned here are good, but if all you want to do is fill in a form and flatten it, I recommend the command line tool called pdftk (PDF Toolkit).

这里提到的库和框架都不错,但是如果您只想填写表格并将其展平,我建议使用名为 pdftk(PDF 工具包)的命令行工具。

See https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/

请参阅https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/

You can call the command line from php, and the command is

可以从php调用命令行,命令是

pdftkformfile.pdffill_formfieldinfo.fdfoutputoutputfile.pdfflatten

pdftkformfile.pdf fill_formfieldinfo.fdf outputoutputfile.pdfflatten

You will need to find the format of an FDF file in order to generate the info to fill in the fields. Here's a good link for that:

您需要找到 FDF 文件的格式才能生成信息以填写字段。这是一个很好的链接:

http://www.tgreer.com/fdfServe.html

http://www.tgreer.com/fdfServe.html

[Edit: The above link seems to be out of commission. Here is some more info...]

[编辑:上述链接似乎已失效。这里有更多信息...]

The pdftk command can generate an FDF file from a PDF form file. You can then use the generated FDF file as a sample. The form fields are the portion of the FDF file that looks like

pdftk 命令可以从 PDF 表单文件生成 FDF 文件。然后,您可以将生成的 FDF 文件用作示例。表单域是 FDF 文件的一部分,看起来像

...
<< /T(f1-1) /V(text of field) >>
<< /T(f1-2) /V(text of another field) >>
...

You might also check out php-pdftk, which is a library specific to PHP. I have not used it, but commenter álvaro (below) recommends it.

您还可以查看php-pdftk,这是一个特定于 PHP 的库。我没有使用过它,但评论者 álvaro(下面)推荐了它。

回答by Val Redchenko

A big +1 to the accepted answer, and a little tip if you run into encoding issues with the fdf file. If you generate the fields.fdf and upon running

如果您遇到 fdf 文件的编码问题,则对已接受的答案大加+1,还有一点提示。如果您生成 fields.fdf 并在运行时

file -bi fields.fdf

you get

你得到

application/octet-stream; charset=binary

then you've most likely run into a UTF-16 character set issue. Try converting the ftf by means of

那么您很可能遇到了 UTF-16 字符集问题。尝试通过以下方式转换 ftf

cat fields.fdf | sed -e's/\x00//g' | sed -e's/\xFE\xFF//g' > better.fdf

I was then able to edit and import the better.fdf file into my PDF form.

然后我就可以编辑 Better.fdf 文件并将其导入到我的 PDF 表单中。

Hopefully this saves someone some Google-ing

希望这可以为某人节省一些 Google 搜索

回答by Val Redchenko

generating fdf File with php: see http://www.php.net/manual/en/book.fdf.php

用 php 生成 fdf 文件:见http://www.php.net/manual/en/book.fdf.php

then fill it into a pdf with pdftk (see above)

然后用 pdftk 将其填入 pdf(见上文)

回答by Nikolay

For:

为了:

  • Easier input format then XFDF
  • True UTF-8 (Russian) support
  • Complete php usage example
  • 比 XFDF 更简单的输入格式
  • 真正的 UTF-8(俄语)支持
  • 完整的php使用示例

Feel free to check my PdfFormFillerUTF-8.

随意检查我的PdfFormFillerUTF-8

回答by josh.chavanne

I've had plenty of success with using a form that submits to a php script that uses fpdf and passes in the form fields as get variables (maybe not a great best-practice, but it works).

我使用提交给使用 fpdf 的 php 脚本的表单并在表单字段中作为获取变量传递的表单取得了很多成功(可能不是一个很好的最佳实践,但它有效)。

 <?php
require('fpdf.php');
$pdf=new PDF();
$pdf->AddPage();
$pdf->SetY(30);
$pdf->SetX(100);
$pdf->MultiCell(10,4,$_POST['content'],0,'J');
$pdf->Output();
?>

and the you could have something like this.

你可以有这样的东西。

  <form action="fooPDF.php" method="post">
     <p>PDF CONTENT: <textarea name="content" ></textarea></p>
     <p><input type="submit" /></p>
    </form>

This skeletal example ought to help ya get started.

这个骨架示例应该可以帮助您入门。

回答by Chris Dolan

I wrote a Perl library, CAM::PDF, with a command-line interfacethat can solve this. I tried using an FDF solution years ago, but found it way too complicated which is why I wrote CAM::PDF in the first place. My library uses a few heuristics to replace the form with the desired text, so it's not perfect. But it works most of the time, and it's fast, free and quite straightforward to use.

我写了一个 Perl 库CAM::PDF,它的命令行界面可以解决这个问题。几年前我尝试使用 FDF 解决方案,但发现它太复杂了,这就是我首先编写 CAM::PDF 的原因。我的库使用了一些启发式方法将表单替换为所需的文本,因此它并不完美。但它大部分时间都有效,而且它快速、免费且使用起来非常简单。

回答by pix0r

Looks like this has been covered before. Click through for relevant code using Zend Framework PDF library.

看起来这已经被覆盖了。单击以获取使用 Zend Framework PDF 库的相关代码。

回答by foxxtrot

We use PDFLibat work. The paid version isn't very expensive, and there is a more limited open source edition, if you are unable to shell out for the paid version.

我们在工作中使用PDFLib。付费版本不是很贵,如果您无法支付付费版本,还有一个更有限的开源版本。