The best pratice to parse PDF forms fields with PHP5

I have PDFs with filled out form fields from a customer, which have to be parsed with PHP5 and written into a MySQL-DB.

With Google I only find libraries, like Zend_Pdf, which are helpful for creating or manipulating PDFs but not for parsing for form fields. Do you know any libraries or classes for this task?

Check out Pdftk. It allows you to manipulate PDF files in many ways.

Take a look at this project on github… php-pdftk I was looking for the same thing and found this project which appears to provide a nice wrapper to pdftk for use in PHP.

EDIT:
The originaly posted solution only returns all form fields, without their filled values. To retrieve field values the approach of @max-matti in the comments can be used.


This solution works without any additional dependencies/requirements:

Use the FPDM class: https://github.com/codeshell/fpdm.

Then define your own class that extends FPDM and retrieves the protected value_entries after calling merge() on your PDF file.

Your class:

use FPDM;

class FPDMHelper extends FPDM {
    public function getFormFields() {
        $this->merge();
        return $this->value_entries;
    }
}

And call it like this:

$pathToYourFile = __DIR__.'your-file.pdf';

$pdf = new FPDMHelper($pathToYourFile);

$formFields = $pdf->getFormFields();
print_r($formFields);

Leave a Comment