PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links 
search for in the  
previousunlinkfdf_add_templatenext
Last updated: Tue, 03 Sep 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | French | German | Hungarian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

XXXI. Forms Data Format functions

Introduzione

Forms Data Format (FDF) is a format for handling forms within PDF documents. You should read the documentation at for more information on what FDF is and how it is used in general.

The general idea of FDF is similar to HTML forms. The difference is basically the format how data is transmitted to the server when the submit button is pressed (this is actually the Form Data Format) and the format of the form itself (which is the Portable Document Format, PDF). Processing the FDF data is one of the features provided by the fdf functions. But there is more. One may as well take an existing PDF form and populated the input fields with data without modifying the form itself. In such a case one would create a FDF document (fdf_create()) set the values of each input field (fdf_set_value()) and associate it with a PDF form (fdf_set_file()). Finally it has to be sent to the browser with MimeType application/vnd.fdf. The Acrobat reader plugin of your browser recognizes the MimeType, reads the associated PDF form and fills in the data from the FDF document.

If you look at an FDF-document with a text editor you will find a catalogue object with the name FDF. Such an object may contain a number of entries like Fields, F, Status etc.. The most commonly used entries are Fields which points to a list of input fields, and F which contains the filename of the PDF-document this data belongs to. Those entries are referred to in the FDF documentation as /F-Key or /Status-Key. Modifying this entries is done by functions like fdf_set_file() and fdf_set_status(). Fields are modified with fdf_set_value(), fdf_set_opt() etc..

Requisiti

You must download the FDF toolkit from .

Istallazione

You must compile PHP with --with-fdftk[=DIR].

Nota: If you run into problems configuring PHP with fdftk support, check whether the header file FdfTk.h and the library libFdfTk.so are at the right place. They should be in fdftk-dir/include and fdftk-dir/lib. This will not be the case if you just unpack the FdfTk distribution.

Configurazione Runtime

Questa estensione non definisce alcuna direttiva di configurazione

Resource Type

Costanti Predefinite

Queste costanti sono definite da questa estensione e sono disponibili solo se l'estensione � stata compilata nel PHP o se � stata caricata dinamicamente a runtime.

FDFValue (integer)

FDFStatus (integer)

FDFFile (integer)

FDFID (integer)

FDFFf (integer)

FDFSetFf (integer)

FDFClearFf (integer)

FDFFlags (integer)

FDFSetF (integer)

FDFClrF (integer)

FDFAP (integer)

FDFAS (integer)

FDFAction (integer)

FDFAA (integer)

FDFAPRef (integer)

FDFIF (integer)

FDFEnter (integer)

FDFExit (integer)

FDFDown (integer)

FDFUp (integer)

FDFFormat (integer)

FDFValidate (integer)

FDFKeystroke (integer)

FDFCalculate (integer)

Esempi

The following examples shows just the evaluation of form data.

Esempio 1. Evaluating a FDF document

<?php
// Save the FDF data into a temp file
$fdffp = fopen("test.fdf", "w");
fwrite($fdffp, $HTTP_FDF_DATA, strlen($HTTP_FDF_DATA));
fclose($fdffp);

// Open temp file and evaluate data
// The pdf form contained several input text fields with the names
// volume, date, comment, publisher, preparer, and two checkboxes
// show_publisher and show_preparer.
$fdf = fdf_open("test.fdf");
$volume = fdf_get_value($fdf, "volume");
echo "The volume field has the value '<B>$volume</B>'<BR>";

$date = fdf_get_value($fdf, "date");
echo "The date field has the value '<B>$date</B>'<BR>";

$comment = fdf_get_value($fdf, "comment");
echo "The comment field has the value '<B>$comment</B>'<BR>";

if(fdf_get_value($fdf, "show_publisher") == "On") {
  $publisher = fdf_get_value($fdf, "publisher");
  echo "The publisher field has the value '<B>$publisher</B>'<BR>";
} else
  echo "Publisher shall not be shown.<BR>";

if(fdf_get_value($fdf, "show_preparer") == "On") {
  $preparer = fdf_get_value($fdf, "preparer");
  echo "The preparer field has the value '<B>$preparer</B>'<BR>";
} else
  echo "Preparer shall not be shown.<BR>";
fdf_close($fdf);
?>

Sommario
fdf_add_template -- Adds a template into the FDF document
fdf_close -- Close an FDF document
fdf_create -- Create a new FDF document
fdf_get_file -- Get the value of the /F key
fdf_get_status -- Get the value of the /STATUS key
fdf_get_value -- Get the value of a field
fdf_next_field_name -- Get the next field name
fdf_open -- Open a FDF document
fdf_save -- Save a FDF document
fdf_set_ap -- Set the appearance of a field
fdf_set_encoding -- Sets FDF character encoding
fdf_set_file -- Set the value of the /F key
fdf_set_flags -- Sets a flag of a field
fdf_set_javascript_action -- Sets an javascript action of a field
fdf_set_opt -- Sets an option of a field
fdf_set_status -- Set the value of the /STATUS key
fdf_set_submit_form_action -- Sets a submit form action of a field
fdf_set_value -- Set the value of a field
User Contributed Notes
Forms Data Format functions
add a note about notes
[email protected]
28-Apr-2000 11:42

The simplest thing to do is get the FDF data from $HTTP_RAW_POST_DATA. (unless you have the server library installed none of the fdf data gets parsed!) This is typical of what you get:
%FDF-1.2
1 0 obj
<<
/FDF << /Fields [ << /V (0)/T (amount0)>> << /V (0)/T (amount1)>> << /V (0)/T (amount2)>>
<< /V (0)/T (amount3)>> << /V (0)/T (amount4)>> << /V (0)/T (amount5)>>
<< /V (0)/T (amount6)>> << /V (0)/T (amount7)>> << /V (0)/T (amount8)>>
<< /V (0)/T (amount9)>> << /V /0102 /T (chase_bk)>> << /V (0)/T (count)>>
<< /V (0)/T (invtotal)>> << /V (12/21/2000)/T (sent_ap)>> << /V /Off /T (spec_hand)>>
<< /V (041232)/T (transit_no)>> << /V (THIS FORM IS NOT COMPLETE!!!)/T (X)>>
]
/F ()>>
>>
endobj
trailer
<<
/Root 1 0 R

>>
%%EOF
kill everything before the [ and then parse it down into key value pairs.
I wrote this to create an FDF, make sure you do a
header("Content-type: application/vnd.fdf");
before you echo the returned value to the user.
function FDFput(,$FDFpage)
{
$A = "%FDF-1.2\n1 0 obj\n<< \n/FDF << /Fields [ \n";
$C = " ] \n" ;
if ($FDFpage>"" ) {$C .=" /F ($FDFpage)>>\n";}
$C .= ">>\n>> \nendobj\ntrailer\n\n<</Root 1 0 R>>\n%%EOF\n";
$B = "";
reset($FDFData);
while (list($key, $val) = each($FDFData))
{
if (strlen(trim($val)) > 0 && is_string($key))
{
$B .= "<</T ($key) /V (". $val . ")>>\n";
//echo "<</T ($key) /V (". $val . ")>>\n";
}
}
return $A.$B.$C;
}

It ain't perfect - but it works. (I use HTML for posting to the server, FDF to the browser)
joe

[email protected]
21-Dec-2001 10:06

Here is an easy script to output fdf data to the browser without using the fdf toolkit or creating an actual fdf file on the server.
By the way acrobat is very picky about line breaks so you must leave the "\n" in the script. The script reads the variables posted to it from a form use POST and creates a fdf file from them. The field names posted to this script must match the field names in the pdf. Acrobat will ignore any that don't match.

<?php

//path to pdf file
$url="

$values=$HTTP_POST_VARS;

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

//loop that adds the field names and values
foreach($values as $key=>$val)
{
$fdfdata.="<< /V ($val)/T ($key) >> ";
}


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

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

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

?>

You can use javascript in the pdf to read the values from a GET method posted directly to the pdf. you can see both methods here:

[email protected]
09-Mar-2002 03:26

function parse($file) {
if (!preg_match_all("/<<\s*\/V([^>]*)>>/x",
$file,$out,PREG_SET_ORDER))
return;
for ($i=0;$i<count($out);$i++) {
$pattern = "<<.*/V\s*(.*)\s*/T\s*(.*)\s*>>";
$thing = $out[$i][1];
if (eregi($pattern,$out[$i][0],$regs)) {
$key = $regs[2];
$val = $regs[1];
$key = preg_replace("/^\s*\(/","",$key);
$key = preg_replace("/\)$/","",$key);
$key = preg_replace("/\\\/","",$key);
$val = preg_replace("/^\s*\(/","",$val);
$val = preg_replace("/\)$/","",$val);
$matches[$key] = $val;
}
}
return $matches;
}

[email protected]
02-Aug-2002 06:30

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 " 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;

}

add a note about notes
previousunlinkfdf_add_templatenext
Last updated: Tue, 03 Sep 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Fri Sep 6 12:35:50 2002 CEST