PHP: Extract fdf fields as an array from a PDF

旧时模样 提交于 2019-12-02 04:52:59

under online documentation for "fdf_next_field_name" the following example is given that you can modify to store the field names into an array

<?php
$fdf = fdf_open($HTTP_FDF_DATA);
for ($field = fdf_next_field_name($fdf); $field != ""; $field = fdf_next_field_name($fdf, $field)) {
    echo "field: $field\n";
}
?>
ftrotter

I upvoted Murray's answer because her was in ernest and I am pretty sure that he is right pre php 5.3

Sadly, pecl fdf is no more.

Thankfully, one "noah" made a comment on the php documentation with a preg_match_all regex solution to the problem. Included here with slight modifications for clarity. Long live noah.

function parse($text_from_file) {
            if (!preg_match_all("/<<\s*\/V([^>]*)>>/x",$text_from_file,$out,PREG_SET_ORDER))
                    return;
            for ($i=0;$i<count($out);$i++) {
                    $pattern = "<<.*/V\s*(.*)\s*/T\s*(.*)\s*>>";
                    $thing = $out[$i][2];
                    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;
    }

I expect that someone will get fedup with the lack of true fdf support in php and fix this.

Since we are all probably after the same basic workflow if you are reading this question, then you should know that the basic workflow that I am following is:

HTH

-FT

If you control the pdf and just want the keys, the following will work. Uses php, no other libraries (good if you host doesn't have them).

Set the pdf submit button to html and set the page to the address where your php code will run.

$q_string  = file_get_contents("php://input");
parse_str($q_string , $pdf_array);
$pdfkeys = array_keys($pdf_array);

The html query string, from the pdf file, is put into the variable $q_string. It is then parsed into an array called $pdf_array. $pdf_array holds all of the keys and values. Then array_keys() is used to put all the keys into $pdfkeys as you wanted.

I had come here looking how to read pdf values to put into a db, and finally after some more poking around came up with the above. Hopefully meets some people's needs. xfdf can also work, but you will need to parse as xml then -- this was simpler for me.

I get a normal post from PDFs submitting to my server, but not in the $_POST array. You just have to parse it from php://input:

$allVars = file_get_contents("php://input");

parse_str($allVars, $myPost);

foreach($myPost as $key => $value) {
 $allKeys[] = $key;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!