How to Convert PDF Forms from XFA to Acroforms with Python

Learn how to use pdfRest XFA to Acroforms API Tool to convert XFA forms to Acroforms for expanded compatibility
Share this page

Why Use XFA to Acroforms with Python?

The pdfRest XFA to Acroforms API Tool allows users to convert XFA forms, which are XML-based forms, into Acroforms, which are more widely supported in PDF viewers and editors. This tutorial will guide you through the process of sending an API call to the XFA to Acroforms endpoint using Python, enabling you to automate the conversion of XFA forms to Acroforms.

Imagine you work for a company that has a large archive of XFA-based forms that need to be updated or processed using modern PDF tools. Since many of these tools do not support XFA forms, converting them to Acroforms becomes essential. By using the pdfRest API and Python, you can automate this conversion process, saving time and effort while ensuring compatibility with a broader range of PDF software.

XFA to Acroforms with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

acroform_endpoint_url = 'https://api.pdfrest.com/pdf-with-acroforms'

# The /pdf-with-acroforms endpoint can take a single PDF file or id as input.
# This sample demonstrates converting an XFA PDF to an acroform document.
mp_encoder_acroform = MultipartEncoder(
    fields={
        'file': ('file_name', open('/path/to/file', 'rb'), 'application/pdf'),
        'output' : 'example_acroform_out',
    }
)

# Let's set the headers that the acroform endpoint expects.
# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below.
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_acroform.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

print("Sending POST request to pdf-with-acroforms endpoint...")
response = requests.post(acroform_endpoint_url, data=mp_encoder_acroform, headers=headers)

print("Response status code: " + str(response.status_code))

if response.ok:
    response_json = response.json()
    print(json.dumps(response_json, indent = 2))
else:
    print(response.text)

# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample.

Source: pdf-with-acroforms.py

Breaking Down the Code

Let's take a closer look at how this code works:

acroform_endpoint_url = 'https://api.pdfrest.com/pdf-with-acroforms'

This line sets the URL for the API endpoint that we will be sending our request to. The endpoint is designed to convert XFA PDFs to AcroForms.

mp_encoder_acroform = MultipartEncoder(
    fields={
        'file': ('file_name', open('/path/to/file', 'rb'), 'application/pdf'),
        'output' : 'example_acroform_out',
    }
)

Here, we are using the MultipartEncoder from the requests_toolbelt library to create a multipart form-data payload. The fields dictionary contains:

  • 'file': This is the file to be uploaded. The open('/path/to/file', 'rb') function opens the file in binary read mode.
  • 'output': This specifies the name of the output file.
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_acroform.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

We then set up the headers for our request. The Accept header indicates that we expect a JSON response. The Content-Type header is automatically set to multipart/form-data by the MultipartEncoder. The Api-Key header should contain your API key.

response = requests.post(acroform_endpoint_url, data=mp_encoder_acroform, headers=headers)

This line sends a POST request to the specified endpoint with the multipart form-data payload and headers.

if response.ok:
    response_json = response.json()
    print(json.dumps(response_json, indent = 2))
else:
    print(response.text)

Finally, we check if the response is successful. If it is, we parse the JSON response and print it in a readable format. If not, we print the error message.

Beyond the Tutorial

In this tutorial, you learned how to send an API call to the pdfRest XFA to Acroforms endpoint using Python. This allows you to convert XFA-based forms to Acroforms, which are more widely supported. To explore more functionalities, you can demo all of the pdfRest API Tools in the API Lab.

For more detailed information, refer to the API Reference Guide. Note that this example demonstrates a multipart API call. Code samples using JSON payloads can be found at pdf-with-acroforms.py.

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