Using PHP to export content to Adobe Acrobat forms

When you need to export content to a printable form, nothing beats an Adobe PDF document. If you already have the PDF created, simply add some fields to it using Adobe Acrobat and read on below.

If you have a PDF already created and you would like to populate it dynamically with content, you can make use of Adobe Form Data files, which have the .FDF extension.

First, use Adobe Acrobat to add fields to your PDF.  Name the fields appropriately so they make sense in your code.  Let’s say you’ve created a PDF with three text fields: Name, Address, and Email.

Upload the PDF to your server so it can be accessed on the net.  Now, to create the script that generates the FDF files, use the following code as an example:

<?php
// retrieve data from form submission
$name = $_REQUEST['name'];
$address = $_REQUEST['address'];
$email = $_REQUEST['email'];

// generate FDF content
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="pdfexport.fdf"');?>
%FDF-1.2
%âãÏÓ
1 0 obj
<</FDF<</Fields[<</T(name)/V(<?= $name; ?>)>>
<</T(address)/V(<?= $address; ?>)>>
<</T(email)/V(<?= $email; ?>)>>
]/ID[<14A67153EE160EB1BBB52A395A124CFA><0D82D21127B3FE49A8BD30E8675CD875>]/UF(mypdf.pdf)/F(http://www.mysite.com/pdfs/mypdf.pdf)>>>>
endobj
trailer
<</Root 1 0 R>>
%%EOF

The code above assumes that the user has just submitted a form which contains a name, address and email field.  The script changes the content type to application/pdf, so instead of simply displaying the content in your web browser, the browser will attempt to find a suitable program to read the FDF, most likely the Adobe Reader.

When using the script above, be sure to change address of the PDF, which can be found after the /F switch near the end of the code above.  This way, when the downloaded .FDF file is opened, Adobe Reader will be able to locate the PDF file used to display this form data.