How to Export PDF Form Data with PHP

Share this page

Why Export PDF Form Data with PHP?

The pdfRest Export Form Data API Tool allows users to extract form data from PDF forms fields. This can be particularly useful in scenarios where you need to automate the collection of data from a large number of submitted forms, such as applications, surveys, or registrations. In this tutorial, we will show how to send an API call to the Export Form Data endpoint using PHP to programmatically retrieve form data from PDFs.

PHP Code Example

 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication.
];

$options = [
  'multipart' => [
    [
      'name' => 'file', // Specify the field name for the file.
      'contents' => Utils::tryFopen('/path/to/file', 'r'), // Open the file specified by the '/path/to/file' for reading.
      'filename' => '/path/to/file', // Set the filename for the file to be processed, in this case, '/path/to/file'.
      'headers' => [
        'Content-Type' => '' // Set the Content-Type header for the file.
      ]
    ],
    [
      'name' => 'data_format', // Specify the field name for the data file format.
      'contents' => 'xml' // Set the value for the data file format (in this case, 'xml').
    ],
    [
      'name' => 'output', // Specify the field name for the output option.
      'contents' => 'pdfrest_exported_form_data' // Set the value for the output option (in this case, 'pdfrest_exported_form_data').
    ]
  ]
];

$request = new Request('POST', 'https://api.pdfrest.com/exported-form-data', $headers); // Create a new HTTP POST request with the API endpoint and headers.

$res = $client->sendAsync($request, $options)->wait(); // Send the asynchronous request and wait for the response.

echo $res->getBody(); // Output the response body, which contains the exported form data content.
?>

Source: GitHub - datalogics/pdf-rest-api-samples

Breaking Down the PHP

The code starts by loading the necessary classes from the Guzzle HTTP client library, which is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

Next, we set up the headers for the request, including the 'Api-Key' which is used for authentication with the pdfRest API.

$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

The options array is then configured with the 'multipart' content. This includes:

  • 'file': The file to be processed, with its path and content type.
  • 'data_format': The format in which the form data should be exported (e.g., 'xml').
  • 'output': The name of the output option, which determines the naming convention of the exported data.
$options = [
  // ...multipart array configuration...
];

A new HTTP POST request is created with the specified endpoint and headers, and the request is sent asynchronously. The response is then retrieved and the body of the response, which contains the exported form data, is outputted.

$request = new Request('POST', 'https://api.pdfrest.com/exported-form-data', $headers);
$res = $client->sendAsync($request, $options)->wait();
echo $res->getBody();

Documentation, API Labs, and So Much More

In this tutorial, we have explored how to use PHP to call the pdfRest Export Form Data API. By sending a properly constructed API call, we can programmatically extract form data from PDFs and use it for further processing or analysis.

Feel free to demo all of the pdfRest API Tools in the API Lab at https://pdfrest.com/apilab/ and refer to the API Reference documentation at https://pdfrest.com/documentation/ for more information.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub - datalogics/pdf-rest-api-samples.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us