How to Import Data to PDF Forms with Python

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

Why Use Import Form Data with Python?

The pdfRest Import Form Data API Tool is a powerful resource for automating the process of importing data into PDF forms. This tool allows you to programmatically fill out PDF forms with data from various sources such as XML or FDF files.

This tutorial will guide you through the process of making an API call to the Import Form Data endpoint using Python, demonstrating how to send data to populate a PDF form programmatically.

Consider a real-world scenario where a business needs to generate hundreds of personalized contracts for a new product launch. Manually entering data into each form would be time-consuming and error-prone. By using the Import Form Data API, the business can automate the process, ensuring that each PDF form is accurately filled with the correct data from a structured data file, thus saving time and reducing the potential for human error.

Import Form Data with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

import_form_data_endpoint_url = 'https://api.pdfrest.com/pdf-with-imported-form-data'

mp_encoder_importFormData = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'data_file': ('file_name.xml', open('/path/to/datafile', 'rb'), 'application/xml'), # Update for your data file format
        'output' : 'example_out'
    }
)

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_importFormData.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

print("Sending POST request to pdf-with-imported-form-data endpoint...")
response = requests.post(import_form_data_endpoint_url, data=mp_encoder_importFormData, 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)

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

Breaking Down the Code

The provided code demonstrates how to make a multipart POST request to the pdfRest Import Form Data API endpoint. Here's a breakdown of the key components:

import_form_data_endpoint_url = 'https://api.pdfrest.com/pdf-with-imported-form-data'

This line sets the URL for the API endpoint that will process the form data import.

mp_encoder_importFormData = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'data_file': ('file_name.xml', open('/path/to/datafile', 'rb'), 'application/xml'),
        'output' : 'example_out'
    }
)

The MultipartEncoder constructs the multipart payload for the request. The fields dictionary includes:

  • 'file': The PDF file into which data will be imported.
  • 'data_file': The data file containing the form data to be imported.
  • 'output': The base name for the output file.
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_importFormData.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

Headers specify the expected response format, the content type of the request, and the API key for authentication.

response = requests.post(import_form_data_endpoint_url, data=mp_encoder_importFormData, headers=headers)

This line sends the POST request with the multipart payload and headers.

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

The response is checked for success, and if successful, the JSON response is printed. Otherwise, the error text is displayed.

Beyond the Tutorial

By following this tutorial, you've learned how to use Python to call the pdfRest Import Form Data API to programmatically fill PDF forms with data. This can significantly streamline workflows that involve repetitive form filling and ensure data consistency across documents.

Feel free to demo all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for more detailed information on how to use the various features provided by pdfRest.

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