How to Flatten PDF Forms with Python

Learn how to flatten PDF form fields using Python to call the Flatten Forms API Tool, part of PDF Forms Pro Tools from pdfRest.
Share this page

Why Flatten PDF Forms with Python?

The pdfRest Flatten Forms API Tool provides a simple way to flatten form fields in a PDF, meaning it converts form fields into static text. This is useful in scenarios where you want to finalize a form by preventing any further edits, such as after a user has filled out a form and you want to archive it or send it as a non-editable document.

This tutorial will guide you through the process of sending an API call to the Flatten Forms endpoint using Python.

Flatten PDF Forms with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

flattened_forms_pdf_endpoint_url = 'https://api.pdfrest.com/flattened-forms-pdf'

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

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

print("Sending POST request to flattened-forms-pdf endpoint...")
response = requests.post(flattened_forms_pdf_endpoint_url, data=mp_encoder_flattenedPDF, 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 code reference: GitHub - pdf-rest-api-samples

Breaking Down the Code

The code snippet above demonstrates how to use the requests library in Python to make a multipart/form-data POST request to the pdfRest Flatten Forms API endpoint. Let's break down the code into its components:

from requests_toolbelt import MultipartEncoder
import requests
import json

This section imports the necessary modules. MultipartEncoder from requests_toolbelt is used to encode the files and fields for the multipart request, while requests is the main library used to send HTTP requests, and json is used to work with JSON data.

flattened_forms_pdf_endpoint_url = 'https://api.pdfrest.com/flattened-forms-pdf'

This line sets the URL for the Flatten Forms API endpoint.

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

This section creates a MultipartEncoder object with the fields to be sent in the request. The 'file' field contains the PDF file to be flattened, and 'output' specifies the base name for the output file.

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

Here, the headers for the request are set, including the Content-Type which is automatically set to the correct value by the MultipartEncoder, and the Api-Key which should be replaced with your actual API key from pdfRest.

response = requests.post(flattened_forms_pdf_endpoint_url, data=mp_encoder_flattenedPDF, headers=headers)

This line sends the POST request to the endpoint with the encoded data and headers.

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

Finally, the response is checked. If successful, the JSON response is printed in a formatted manner. Otherwise, the error text is printed.

Beyond the Tutorial

In this tutorial, we walked through how to make an API call to the pdfRest Flatten Forms API endpoint using Python. By sending a PDF file to this endpoint, we can flatten its forms, making it a static document that can no longer be edited. This is particularly useful for preserving the final state of filled-out forms.

I encourage you to demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation for more details.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub - 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.