1

i want to extract the available fields as an array from a fillable pdf.

an array like: array('firstname','secondname','address');

i do not need the values for those fields, if they are filled.

what is easiest way to do that using PHP?

Confidence
  • 2,283
  • 3
  • 33
  • 57

4 Answers4

2

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";
}
?>
Murray McDonald
  • 631
  • 4
  • 5
  • 1
    This answer was correct up to php 5.2 but pecl fdf support was removed php 5.3. Let us all have a brief moment of silence for the lost library................ – ftrotter Jan 17 '13 at 11:31
1

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

Community
  • 1
  • 1
ftrotter
  • 3,066
  • 2
  • 38
  • 52
  • 1
    The [libreoffice to make it a pdf form with named fields.](http://www.ehow.com/how_7513242_create-fillable-form-pdf-linux.html) link is dead. The [archive.org](https://web.archive.org/web/20150110073421/http://www.ehow.com/how_7513242_create-fillable-form-pdf-linux.html) link still works. Maybe duplicate (recover) the steps here? – luckydonald Sep 15 '17 at 17:32
1

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. enter image description here

$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.

Community
  • 1
  • 1
Sterner
  • 96
  • 4
-1

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;
}
BarryDevSF
  • 395
  • 3
  • 12