php 使用 FPDF 填写 PDF 表单,然后使用 PDFTK 进行拼合显示未填写的 PDF

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

PDF Form filling with FPDF and then Flatten with PDFTK displays un-filled PDF

phppdffpdfpdftkpdf-form

提问by user1855093

I'm using the "Form Filling" script from fpdf.org to fill some fields on a PDF Form I created. This appears to work properly.

我正在使用 fpdf.org 中的“表单填写”脚本来填写我创建的 PDF 表单上的一些字段。这似乎工作正常。

I want the resulting PDF form to be flattened so users can not edit the form fields. I'm using PDFTKfor that. However, when I try to flatten the PDF, I get a PDF with the form fields empty.

我希望将生成的 PDF 表单展平,以便用户无法编辑表单字段。为此,我正在使用PDFTK。但是,当我尝试展平 PDF 时,我得到一个表单字段为空的 PDF。

Any suggestions on how to get the PDF flattened (using PHP) would be appreciated. Thanks!

任何关于如何使 PDF 变平(使用 PHP)的建议将不胜感激。谢谢!

Here's my code:

这是我的代码:

<?php
require('fpdm.php');
$fields = array("Name" => "John Doe", 
                "Address" => "123 White Lane", 
                "Age" => "30", 
                "Phone" => "123-1234");
$pdf = new FPDM("templates/Test.pdf");
$pdf->Load($fields, true);
$pdf->Merge();
$pdf->Output("cache/Filled1.pdf","F");

exec("pdftk cache/Filled1.pdf output cache/Filled1Flat.pdf flatten");
?>

Download original Test.pdf file: Test.pdf

下载原始 Test.pdf 文件:Test.pdf

Download Filled1.pdf file (displays pdf form correctly with data visible): Filled1.pdf

下载 Filled1.pdf 文件(正确显示 pdf 表格,数据可见):Filled1.pdf

Download Filled1Flat.pdf file (displays flattened pdf form with no form data visible): Filled1Flat.pdf

下载 Filled1Flat.pdf 文件(显示扁平的 pdf 表单,没有可见的表单数据):Filled1Flat.pdf

采纳答案by Michael H?rtl

Blatant self promotion: Try my php-pdftkpackage. If you have pdftkinstalled, it's very easy to use.

公然自我推销:试试我的php-pdftk包。如果已经pdftk安装,使用起来非常方便。

composer require mikehaertl/php-pdftk

<?php
use mikehaertl/pdftk/Pdf;

$pdf = new Pdf('form.pdf');
// Fill in UTF-8 compliant form data!
$pdf->fillForm(array('name' => 'Any char: ??ü'))
    ->saveAs('filled.pdf');

// Alternatively: Send to browser for download...
$pdf->send('filled.pdf');

// ... or inline display
$pdf->send();

回答by user1855093

I was able to find another process to fill a PDF form and then flatten it. I still don't know why using the "Form Filling" script from fpdf.org did not work.

我能够找到另一个过程来填写 PDF 表单,然后将其展平。我仍然不知道为什么使用 fpdf.org 中的“表格填写”脚本不起作用。

I followed the steps outlined here:

我按照此处概述的步骤操作:

1) Get the field names if they are not already known

1) 如果字段名称未知,则获取它们

exec("pdftk templates/Test.pdf dump_data_fields >cache/testfields.txt");

2) Create a FDF file with the field names and field values and save it as Test.fdf

2) 使用字段名称和字段值创建一个 FDF 文件并将其保存为 Test.fdf

%FDF-1.2
1 0 obj<</FDF<< /Fields[
<</T(Name)/V(John Doe)>>
<</T(Address)/V(123 White Lane)>>
<</T(Age)/V(30)>>
<</T(Phone)/V(123-1234)>>
] >> >>
endobj
trailer
<</Root 1 0 R>>
%%EOF

3) Then fill the form and flatten it

3)然后填写表格并将其展平

exec("pdftk templates/Test.pdf fill_form templates/Test.fdf output cache/FilledFDF.pdf flatten");

Download resulting PDF (filled and flattened): FilledPDF.pdf

下载生成的 PDF(填充和扁平化):FilledPDF.pdf

回答by cchantep

With Dhek PDF template editor, there is a PHP snippet to fill data into an existing PDF using FPDF/FPDI, according JSON mappings (to know where to put texts/checkboxes). https://github.com/applicius/dhek

使用 Dhek PDF 模板编辑器,根据 JSON 映射(知道将文本/复选框放在哪里),有一个 PHP 代码段可以使用 FPDF/FPDI 将数据填充到现有的 PDF 中。https://github.com/applicius/dhek