Merge FDF data into a PDF file using PHP
Asked Answered
C

4

21

Is it possible to merge FDF data with a PDF file using PHP alone? Or is there no option but to use a 3rd party command line tool to achieve this?

If that is the case can someone point me in the direction of one?

I am currently outputting the FDF file to the browser in the hope that it will redirect the user to the filled in PDF but for some people that is not the case. The FDF contents is being output to the screen, even though I am using header('Content-type: application/vnd.fdf');

Candelabra answered 7/9, 2009 at 15:50 Comment(0)
C
33

For future reference, it looks like there isn't a reliable way of doing it without a 3rd party app. Pdftk (http://www.accesspdf.com/pdftk/) ended up being my solution.

I first generated the FDF file as before, and then merged it into my PDF file using the following PHP code

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="Download.pdf"');
passthru("pdftk file.pdf fill_form data.fdf output - ");
exit;

It was much easier than I thought it would be. This instantly eliminates the need to hack around with headers and file extensions to ensure all browsers handle an FDF properly, as it simply makes the browser download the PDF file.

If you want the PDF output file to no longer be editable, use

    passthru("pdftk file.pdf fill_form data.fdf output - flatten");

Apologies if this is basic stuff, just thought I'd put it all in one place so that people don't go through the headache that I endured.

N.B. If your PATH variable is not set, you will need to use the full path to pdftk i.e.

    passthru("/usr/local/bin/pdftk file.pdf fill_form data.fdf output - flatten");
Candelabra answered 7/9, 2009 at 18:28 Comment(3)
It should be noted that fill_form also works with XFDF data as well. If you want a combination of editable and read only fields in your resulting PDF, set the "read only" property for the fields you want to be filled in when you generate the PDF. Read only fields will be filled in by PDFTK, but won't be editable by the end user.Crenulation
pdftk is no longer supported or installable on CentOS - if anybody has an alternative, let us know.Peterkin
How would I install pdftk on Hostgator VPS?Saline
B
3

There is another way to day that not using passthru nor pdftk but just a script made in 2004 but still working well : forge_fdf

it helps you to build a fdf that you can incoporate straithly in your pdf, it means that you

Save this in a php file, let's say generatePdf.php

require_once('forge_fdf.php');  

// leave this blank if we're associating the FDF w/ the PDF via URL
$pdf_form_url= "";


// default data; these two arrays must ultimately list all of the fields
// you desire to alter, even if you just want to set the 'hidden' flag;
//
//
$fdf_data_names= array(); // none of these in this example
$fdf_data_strings= array(); // none of these in this example

$fdf_data_strings['email']=mb_strtolower($row_delivreur['firstname']).'.'.mb_strtolower($row_delivreur['lastname']).'@gmail.com';

$fields_hidden= array();
$fields_readonly= array();

// set this to retry the previous state
$retry_b= false;

header( 'content-type: application/vnd.fdf' );

echo forge_fdf( $pdf_form_url,
        $fdf_data_strings, 
        $fdf_data_names,
        $fields_hidden,
        $fields_readonly );

Making a link to Pathtoyourpdf/nameofpdffile.pdf#FDF=generatePdf.php will open your PDF file in browser (alternatively there is a way to save it to disk i think i remember) and the field email will be filled with data from MYSQL : mb_strtolower($row_delivreur['firstname']).'.'.mb_strtolower($row_delivreur['lastname']).'@gmail.com'

It works with checkboxes, radio button,... It opens well in firefox, it has to be tested with other browsers.

More infos on PDF HACKS

Bead answered 26/4, 2011 at 4:56 Comment(1)
forge_fdf for PHP is also written by PDF Labs (the creators of Pdftk), I believe.Irreconcilable
P
2

by far the easiest way I have found to install pdftk on centos:

Install rpmforge repo - http://wiki.centos.org/AdditionalResources/Repositories/RPMForge#head-5aabf02717d5b6b12d47edbc5811404998926a1b

then

yum install pdftk

and thats it!

Profound answered 20/11, 2010 at 7:18 Comment(1)
No longer supported on CentOS 7Peterkin
W
1

http://www.setasign.de/products/pdf-php-solutions/fpdi/demos/concatenate-fake/

This works , the classes download are linked to from the web site too,

It requires no passthru/exec command and no additional extensions.

Edited to say, this doesn't work with newer pdf versions 1.5+, reverted to PDFTK, fiddly but works with all pdfs using the 'exec' command.

Weichsel answered 14/4, 2011 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.