tags:

views:

2452

answers:

4

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');

+6  A: 

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");
James
I think you meant to add the "flatten" command to the second example.
bmb
I have done that now, thanks
James
Wow, helped me a lot!
DaNieL
A: 

So I'm brand new to php. To be honest, I'm piecing it together as I go. I'm confused as to where you are putting this code. Is it in the same php file that your code which created the fdf file is in? Or is this a different php file?

Steve
It can be in the same file, or another file. The FDF file needs to be written to disk. Then you can pass its file location to pdftk as per the example above
James
A: 

TYPP "passssthru" should be "passthru"

someguy
Thanks, not quite sure how that happened. Fixed
James
+1  A: 

also if your PATH variable is not set you have to give an absolute path to the app so , instead of pdftk

use /usr/local/bin/pdftk

that caused me a few hours of pain . hope this helps! thanks man!

corey
Thanks! Have edited this in to the answer
James