I am using Pdftk and pdftk-php (which is based on forge-fdf). I'd like to generate an FDF that can be merged with a PDF to fill out a dropdown text box with multiple options. The way pdftk-php works by default, it will only fill dropdowns with a single value; passing it an array doesn't work.
Here is the relevant output from pdftk filename dump_data_fields.
FieldType: Choice
FieldName: VehicleIDData1
FieldFlags: 4587520
FieldJustification: Left
According to this example from the PDF 1.2 specification, dropdowns seem to require the /Opt option in the FDF.
%FDF-1.2
1 0 obj <<
/FDF <<
/Fields
[
<<
/T (My Children)
/V (Tali)
/Opt [(Maya) (Adam) (Tali)]
November 12, 1996
: 379
>>
]
/F (Dependents.pdf)
>>
>>
endobj
trailer
<</Root 1 0 R>>
%%EOF
This is supported by the table on page 717 in the 1.7 specification
Opt
array
(Required; choice fields only) An array of options to be presented to the user. Each element of the array can take either of two forms:
•A text string representing one of the available options
•A two-element array consisting of a text string representing one of the available options and a default appearance string for constructing the item’s appearance dynamically at viewing time (see “Variable Text” on page 677)
pdftk-php doesn't use /Opt; if you pass it an array of values for a field, it will use /Kids, which isn't filling out the other dropdown entries. I've tried to modify the program to use /Opt instead, but there's no difference in the resulting PDFs. What am I doing wrong?
protected function forge_fdf_fields( &$fdf,
&$fdf_data,
&$fields_hidden,
&$fields_readonly,
$accumulated_name,
$strings_b ) // true <==> $fdf_data contains string data
//
// string data is used for text fields, combo boxes and list boxes;
// name data is used for checkboxes and radio buttons, and
// /Yes and /Off are commonly used for true and false
{
if( 0< strlen( $accumulated_name ) ) {
$accumulated_name.= '.'; // append period seperator
}
foreach( $fdf_data as $key => $value ) {
// we use string casts to prevent numeric strings from being silently converted to numbers
$fdf.= "<< "; // open dictionary
if( gettype($value)== 'array' ) { // parent; recurse
$fdf.= "/T (".$this->escape_pdf_string( (string)$key ).") "; // partial field name
$fdf .= "/V (" . $this->escape_pdf_string((string)$value[0]) . ") ";
$fdf .= "/Opt [";
foreach ($value as $option) {
$fdf .= "(" . $this->escape_pdf_string((string)$option) . ") ";
}
/* This is the older code I'm replacing.
$fdf.= "/Kids [ "; // open Kids array
// recurse
$this->forge_fdf_fields( $fdf,
$value,
$fields_hidden,
$fields_readonly,
$accumulated_name. (string)$key,
$strings_b );
*/
$fdf.= "] "; // close Kids array
} else {
// field name
$fdf.= "/T (".$this->escape_pdf_string( (string)$key ).") ";
// field value
if( $strings_b ) { // string
$fdf.= "/V (".$this->escape_pdf_string( (string)$value ).") ";
}
else { // name
$fdf.= "/V /".$this->escape_pdf_name( (string)$value ). " ";
}
// field flags
$this->forge_fdf_fields_flags( $fdf,
$accumulated_name. (string)$key,
$fields_hidden,
$fields_readonly );
}
$fdf.= ">> \x0d"; // close dictionary
}
}