How to Import Data to PDF Forms with PHP

Learn how to use pdfRest Import Form Data API Tool with PHP to import data into PDF forms.
Share this page

Why Use Import Form Data with PHP?

The pdfRest Import Form Data API Tool is a powerful feature that allows users to automate the process of importing data into PDF forms. This tool is particularly useful when you have a PDF form and a set of data that you want to merge together without manual data entry.

This tutorial will guide you through the process of making an API call to the Import Form Data endpoint using PHP, which is a common server-side scripting language used in web development.

In the real world, you might use the Import Form Data feature to automate the filling of customer information into contract templates, to generate personalized reports from collected data, or to streamline any process where form data needs to be programmatically merged with a PDF. For instance, a healthcare provider could use this tool to populate patient intake forms with data from an electronic health record system, saving time and reducing errors.

Import Form Data with PHP Code Example

require 'vendor/autoload.php'; // Require the autoload file to load Guzzle HTTP client.

use GuzzleHttp\Client; // Import the Guzzle HTTP client namespace.
use GuzzleHttp\Psr7\Request; // Import the PSR-7 Request class.
use GuzzleHttp\Psr7\Utils; // Import the PSR-7 Utils class for working with streams.

$client = new Client();
$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];
$options = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/file', 'r'), // Provide the path to the PDF file with forms.
      'filename' => '/path/to/file',
      'headers'  => [
        'Content-Type' => ''
      ]
    ],
    [
      'name' => 'data_file',
      'contents' => Utils::tryFopen('/path/to/datafile', 'r'), // Provide the path to the file with form data.
      'filename' => '/path/to/datafile',
      'headers'  => [
        'Content-Type' => ''
      ]
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_pdf-with-imported-form-data' // Specify the output file name
    ]
]];

$request = new Request('POST', 'https://api.pdfrest.com/pdf-with-imported-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 PDF with data.

Breaking Down the Code

The provided PHP code uses the Guzzle HTTP client to send a multipart POST request to the pdfRest API. Let's break down the key parts of the code:

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

$client = new Client();
$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

This section initializes the Guzzle HTTP client and sets up the headers for the API request, including the required 'Api-Key' for authentication.

$options = [
  'multipart' => [
    // ...
  ]
];

The $options array contains the multipart form data. Each part represents a different piece of data to be sent with the request:

  • file: The PDF file with forms that you want to populate.
  • data_file: The file containing the form data in FDF, XFDF, XML, or JSON format.
  • output: The desired name for the output PDF file.
$request = new Request('POST', 'https://api.pdfrest.com/pdf-with-imported-form-data', $headers);
$res = $client->sendAsync($request, $options)->wait();
echo $res->getBody();

Here, a new HTTP POST request is created with the API endpoint and headers, and the request is sent asynchronously. The response is then waited on, and the body of the response, which contains the PDF with the imported data, is outputted.

Beyond the Tutorial

By following this tutorial, you've learned how to use PHP to call the pdfRest Import Form Data API to programmatically fill out a PDF form with data. This can greatly improve efficiency and accuracy in various data entry tasks.

To further explore the capabilities of pdfRest and to demo all of the API Tools, visit the API Lab. For more detailed information about the API and its features, refer to the API Reference Guide.

Note: This is an example of a multipart API call. If you are interested in code samples using JSON payloads, you can find them at this GitHub repository.

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