代码之家  ›  专栏  ›  技术社区  ›  James

在PHP中通过Pdftk使用FDF填充FieldType:Choice字段和多个选项

  •  2
  • James  · 技术社区  · 6 年前

    我在用 Pdftk pdftk-php (基于forge-fdf)。我想生成一个可以与PDF合并的FDF来填写一个带有多个选项的下拉文本框。默认情况下,pdftk php的工作方式是只使用一个值填充下拉列表;向它传递数组不起作用。

    以下是pdftk filename dump_data_字段的相关输出。

    FieldType: Choice
    FieldName: VehicleIDData1
    FieldFlags: 4587520
    FieldJustification: Left
    

    根据PDF 1.2规范中的这个示例,下拉菜单似乎需要FDF中的/Opt选项。

    %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
    

    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不使用/Opt;如果向它传递字段的值数组,它将使用/Kids,这不会填充其他下拉项。我试图修改程序以使用/Opt代替,但是得到的pdf没有区别。我做错什么了?

    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
        }
    }
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   user1254498    10 年前

    我很想得到一个答案:)

    我正试图填补这个领域:

    FieldType: Button
    FieldName: conséquences
    FieldFlags: 0
    FieldValue: avec arr&#234;t
    FieldJustification: Left
    FieldStateOption: Off
    FieldStateOption: avec arr&#234;t
    FieldStateOption: deces
    FieldStateOption: sans arr&#234;t
    

    $pdf->fillForm(
        array(
             'conséquences' => 'avec arrêt'
        )
    );
    

    它不起作用。也没有 'avec arr&#234 ;t' . 但它与 'deces' .