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

3

15

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.

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

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

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

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

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

Deservedly answered 27/11, 2012 at 2:2 Comment(3)
Please also supply the intermediary file Filled1.pdf. Test.pdf contains empty normal appearances streams for the form fields. If fpdf only sets the values of the fields but doesn't update their appearances, and if pdftk assumes the existing appearances to be correct, the observed behavior can be explained. In that case you have to change the form filling to update appearances or the the flattening to create appearances. I'm not into PHP, though, and so cannot say which is easier to accomplish and how.Lanfranc
mkl, Thanks for your response. I added the links to download the intermediary PDF (Filled1.pdf) and the final PDF (Filled1Flat.pdf). It's worth nothing that if I remove the "flatten" option from the PDFTK command, the PDF file does display the form data correctly: exec("pdftk cache/Filled1.pdf output cache/FilledProcessed.pdf"); download here: FilledProcessed.pdfDeservedly
Unfortunately I haven't had the time until now to inspect the additional files you supplied, and now they seem to have disappeared from the links you supplied. If you reactivate the links, I'll have a look at those files and would try and tell you which of the commands is the culprit.Lanfranc
D
13

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

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();
Deluna answered 11/7, 2014 at 21:27 Comment(9)
HelloWhat about Cyrillic symbols?Trek
@Trek Please check the docs. They are partially supported. But pdftk has some issues.Rupture
@MichaelHärtl - Nice package and very easy to use! Highly recommend.Anti
@MichaelHärtl Hey! Lovin this resource, anyway you could help me diagnose an issue I'm having: #39534295Savoy
Just another success story here. You need to enable the functions: exec, proc_open, proc_close, proc_get_status, proc_terminate, escapeshellarg, escapeshellcmd.Cruiserweight
Works really great, however there is no checkbox support like FPDM. I am using FPDM to fill checkboxes first and then this one to make it flatten-compatible.Rakish
@JayDadhania You can fill checkboxes just like any other form field. You have to pass the field name and the option value of the checkbox (predefined in the PDF) as value. Try pdftk file.pdf dump_data_fields and look for FieldStateOption on the respective field to find out the available values.Rupture
@MichaelHärtl Yep, FPDM allows any value that evaluates to true for checkboxes, so I thought the same will be provided here, and passing boolean true as a value failed. I tried with actual predefined value Yes, and it worked beautifully! Thank you so much for this awesome package!!Rakish
this doesn't work on linux servers which don't have PDFTK. Sad that in 2021 there is still no solution to flatten PDF on shared linux hosting without having to install dependencies. Also, PDFTK does not work on Redhat now which a lot of webhosts use.Padishah
D
11

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.

I followed the steps outlined here:

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

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

%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

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

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

Deservedly answered 27/11, 2012 at 19:37 Comment(4)
This seems to indicate that pdftk knows how to generate appearances; thus, I would surmise that fpdf neither creates new appearances nor marks the PDF as needing new appearances for form fields. As the intermediary files are not available at the supplied links, though, I cannot verify that surmise.Lanfranc
I have the exact same concern as this question, but this solution does not work for me considering I have utf-8 characters, which is why i went to the fpdf.org script in the first place.Omura
To "tick" a checkbox in FDF format use /V /Yes however any un-ticked checkboxes (the box itself) were completely removed when I used this method.Rakish
Works but sadly most webservers don't have PDFTK. PHP foundation need to add full PDF support... It's 2021Padishah
N
0

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

Novokuznetsk answered 20/10, 2013 at 16:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.