使用PHP解析pdftk dump_data_fields?

3

我需要一些关于如何使用PHP解析pdftk dump_data_fields输出的最佳方法的建议。

此外,我需要提取的属性是:FieldNameFieldNameAlt以及可选的FieldMaxLengthFieldStateOptions

FieldType: Text
FieldName: TestName1
FieldNameAlt: TestName1
FieldFlags: 29360128
FieldJustification: Left
FieldMaxLength: 5
---
FieldType: Button
FieldName: TestName3
FieldFlags: 0
FieldJustification: Left
FieldStateOption: Off
FieldStateOption: Yes
---
...
2个回答

5
这样的东西足够吗?
$handle = fopen("/tmp/bla.txt", "r");
if ($handle) {
    $output = array();
    while (($line = fgets($handle)) !== false) {
        if (trim($line) === "---") {
            // Block completed; process it
            if (sizeof($output) > 0) {
                print_r($output);
            }
            $output = array();
            continue;
        }
        // Process contents of data block
        $parts = explode(":", $line);
        if (sizeof($parts) === 2) {
            $key = trim($parts[0]);
            $value = trim($parts[1]);
            if (isset($output[$key])) {
                $i = 1;
                while(isset($output[$key.$i])) $i++;
                $output[$key.$i] = $value;
            }
            else {
                $output[$key] = $value;
            }
        }
        else {
            // handle malformed input
        }
    }

    // process final block
    if (sizeof($output) > 0) {
        print_r($output);
    }
    fclose($handle);
}
else {
    // error while opening the file
}

这会给你以下输出结果:
Array
(
    [FieldType] => Text
    [FieldName] => TestName1
    [FieldNameAlt] => TestName1
    [FieldFlags] => 29360128
    [FieldJustification] => Left
    [FieldMaxLength] => 5
)
Array
(
    [FieldType] => Button
    [FieldName] => TestName3
    [FieldFlags] => 0
    [FieldJustification] => Left
    [FieldStateOption] => Off
    [FieldStateOption1] => Yes
)

钓出这些值就像这样简单:
echo $output["FieldName"];

1
我该如何请你喝咖啡? :D - Aleksandar Pavić

0

我对上述代码进行了一些修改,解决了一些问题,例如最后一个元素字段未添加到数组中。现在更新的代码如下所示:

        // Get form data fields 
        $fieldsDataStr = '';
        $fieldsDataStr = $pdf->getDataFields();

    /* explode by \n and convert string into array. */
    $lines = explode("\n", $fieldsDataStr);  
    /* added '---' into end of lines array beucase we need to get last field value also based on below logic. */
    array_push($lines, "---");

    $output = array();
    $pdfDataArray = array();
    $counterField = 0;
    foreach($lines as $line) {
    if (trim($line) === "---") {
        // Block completed; process it
        if (sizeof($output) > 0) { 
        $pdfDataArray[] = $output;
        $counterField = $counterField + 1; //fields counter
        }
        $output = array();
        continue;
    }
    // Process contents of data block
    $parts = array();           
    $parts = explode(":", $line, 2); //2 is return array max limit, it will return array with first occurence of colon          
    if (sizeof($parts) === 2) {
        $key = trim($parts[0]);
        $value = trim($parts[1]);
        $output[$key] = $value;
    }   
        }

    print_r($pdfDataArray);

它将返回正确的数组


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接