ConFoo Montreal 2026: Call for Papers

Voting

: three minus zero?
(Example: nine)

The Note You're Voting On

adam at andemyte dot com
23 years ago
Here is yet another example of generating pre-filled PDFs without using the FDF functions. This function takes two args: a URL to the PDF (like "http://domain.com/path/to/form.pdf" and an array with all the field's values.

/*
WARNING!! THIS FUNCTION SENDS HTTP HEADERS! It MUST be called before
any content is spooled to the browser, or the function will fail!

void output_fdf (string $pdf_file, array $pdf_data)

$pdf_file: a string containing a URL path to a PDF file on the
server. This PDF MUST exist and contain fields with
the names referenced by $pdf_data for this function
to work.
$pdf_data: an array of any fields in $pdf_file that you want to
populate, of the form key=>val; where the field
name is the key, and the field's value is in val.

*/

function output_fdf ($pdf_file, $pdf_data) {

$fdf = "%FDF-1.2\n%????\n";
$fdf .= "1 0 obj \n<< /FDF ";
$fdf .= "<< /Fields [\n";

foreach ($pdf_data as $key => $val)
$fdf .= "<< /V ($val)/T ($key) >> \n";

$fdf .= "]\n/F ($pdf_file) >>";
$fdf .= ">>\nendobj\ntrailer\n<<\n";
$fdf .= "/Root 1 0 R \n\n>>\n";
$fdf .= "%%EOF";

/* Now we display the FDF data which causes Acrobat to start */

header ("Content-Type: application/vnd.fdf");
print $fdf;

}

<< Back to user notes page

To Top