How to Convert PDF Forms from XFA to Acroforms with PHP

Learn how to convert XFA forms to Acroforms using pdfRest API Tool with PHP
Share this page

Why Use XFA to Acroforms with PHP?

The pdfRest XFA to Acroforms API Tool provides a straightforward way to convert XFA forms into Acroforms using PHP. This tutorial will guide you through the process of sending an API call to the XFA to Acroforms endpoint using PHP, making it easier to integrate this functionality into your applications.

In real-world scenarios, you might encounter XFA forms that need to be converted into Acroforms for better compatibility with various PDF viewers and tools. For instance, if you have a legacy system generating XFA forms, converting them to Acroforms ensures that end-users can easily fill out and submit these forms using standard PDF viewers.

XFA to Acroforms 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(); // Create a new instance of the Guzzle HTTP client.

$headers = [
  'Api-Key' =--> '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' => 'output', // Specify the field name for the output option.
      'contents' => 'pdfrest_acroform' // Set the value for the output option (in this case, 'pdfrest_acroform').
    ]
  ]
];

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

Source: GitHub

Breaking Down the Code

Let's break down the provided code and understand each part:

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

This line includes the Composer autoload file, which loads all the necessary dependencies, including the 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.

These lines import the required classes from the Guzzle HTTP client library and the PSR-7 standard for HTTP messages, which helps in creating and handling HTTP requests and streams.

$client = new Client(); // Create a new instance of the Guzzle HTTP client.

This line creates a new instance of the Guzzle HTTP client, which will be used to send the API request.

$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' // Set the API key in the headers for authentication.
];

The headers array sets the API key required for authentication. Replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual API key.

$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' => 'output', // Specify the field name for the output option.
      'contents' => 'pdfrest_acroform' // Set the value for the output option (in this case, 'pdfrest_acroform').
    ]
  ]
];

The options array specifies the multipart form data. The 'file' part includes the file to be converted, and the 'output' part specifies the desired output format. Adjust the file path and content type as needed.

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

This line creates a new HTTP POST request to the specified API endpoint, including the headers for authentication.

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

The sendAsync method sends the request asynchronously, and the wait method waits for the response. This ensures that the script does not proceed until the response is received.

echo $res->getBody(); // Output the response body, which contains the acroform document.

This line outputs the response body, which contains the converted Acroform document.

Beyond the Tutorial

In this tutorial, we demonstrated how to use PHP to call the pdfRest XFA to Acroforms API endpoint. This allows you to convert XFA forms to Acroforms programmatically, making your applications more versatile and user-friendly.

We encourage you to explore all the pdfRest API Tools in the API Lab. For detailed information on all available endpoints and their parameters, refer to the API Reference Guide.

Note: This example uses a multipart API call. You can find code samples using JSON payloads at GitHub.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.