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

user1855093
  • 373
  • 1
  • 2
  • 14
  • 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. – mkl Nov 27 '12 at 08:48
  • 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.pdf](http://www.geo3o.com/KeyTech/pdf2/cache/FilledProcessed.pdf) – user1855093 Nov 27 '12 at 18:40
  • 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. – mkl Nov 28 '12 at 20:31

3 Answers3

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();
Michael Härtl
  • 8,428
  • 5
  • 35
  • 62
  • HelloWhat about Cyrillic symbols? – devzorg Mar 27 '16 at 19:13
  • @devzorg Please check the docs. They are partially supported. But pdftk has some issues. – Michael Härtl Mar 28 '16 at 09:11
  • @MichaelHärtl - Nice package and very easy to use! Highly recommend. – But those new buttons though.. May 06 '16 at 18:51
  • @MichaelHärtl Hey! Lovin this resource, anyway you could help me diagnose an issue I'm having: http://stackoverflow.com/questions/39533795/fatal-error-class-mikehaertl-pdftk-pdf-not-found-in-wordpress-theme-folder – LatentDenis Sep 16 '16 at 14:58
  • Just another success story here. You need to enable the functions: exec, proc_open, proc_close, proc_get_status, proc_terminate, escapeshellarg, escapeshellcmd. – cesAR Jul 01 '20 at 22:27
  • 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. – Jay Dadhania Jan 26 '21 at 15:56
  • 1
    @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. – Michael Härtl Jan 27 '21 at 07:03
  • @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!! – Jay Dadhania Jan 27 '21 at 07:30
  • 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. – pablorenato Jul 18 '21 at 11:54
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

user1855093
  • 373
  • 1
  • 2
  • 14
  • 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. – mkl Nov 28 '12 at 20:37
  • 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. – David Folkner Mar 18 '14 at 02:20
  • 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. – Jay Dadhania Jan 26 '21 at 15:58
  • Works but sadly most webservers don't have PDFTK. PHP foundation need to add full PDF support... It's 2021 – pablorenato Jul 18 '21 at 11:55
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

cchantep
  • 9,118
  • 3
  • 30
  • 41