How to Remove PDF Restrictions with Python

Learn how to remove restrictions from PDFs to enable printing, editing content and form fields, copying text, and more with the Restrict PDF API Tool.
Share this page

Why Remove PDF Restrictions with Python?

The pdfRest Restrict PDF API Tool is a powerful service that allows users to manipulate PDF file permissions and restrictions programmatically. This tutorial will demonstrate how to make an API call to the Restrict PDF endpoint using Python. The Restrict PDF API Tool provides a range of functionalities for PDF manipulation including restricting and unrestricting PDFs via API calls.

In a real-world scenario, a user might need to remove restrictions from a PDF document to enable editing, copying, or printing. For instance, if a company receives a restricted PDF report from an external consultant, they may need to unlock it to extract data or incorporate content into other documents. Using the /unrestricted-pdf endpoint, this can be achieved programmatically, allowing for seamless integration into workflows or applications that require automated handling of PDF files.

Removing PDF Restrictions with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

unrestricted_pdf_endpoint_url = 'https://api.pdfrest.com/unrestricted-pdf'

# The /unrestricted-pdf endpoint can take a single PDF file or id as input.
# This sample demonstrates removing security restrictions from a PDF.
mp_encoder_unrestrictedPdf = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'output' : 'example_unrestrictedPdf_out',
        'current_permissions_password': 'password'
    }
)

# Let's set the headers that the unrestricted-pdf 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_unrestrictedPdf.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

print("Sending POST request to unrestricted-pdf endpoint...")
response = requests.post(unrestricted_pdf_endpoint_url, data=mp_encoder_unrestrictedPdf, 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.

This code is sourced from the pdf-rest-api-samples repository on GitHub.

Breaking Down the Code

The provided Python code demonstrates how to use the pdfRest API to remove restrictions from a PDF file. Here's a breakdown of the key components of the code:

from requests_toolbelt import MultipartEncoder
import requests
import json

This imports the necessary libraries. MultipartEncoder from requests_toolbelt is used to encode the file as multipart/form-data, which is required for file uploads.

unrestricted_pdf_endpoint_url = 'https://api.pdfrest.com/unrestricted-pdf'

This sets the API endpoint URL for the unrestricted-pdf service.

mp_encoder_unrestrictedPdf = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'output' : 'example_unrestrictedPdf_out',
        'current_permissions_password': 'password'
    }
)

This creates a MultipartEncoder object with the necessary fields for the API request:

  • 'file': The PDF file to be unrestricted. It should be opened in binary mode.
  • 'output': The desired name for the output file.
  • 'current_permissions_password': The password that unlocks the current restrictions on the PDF file.
headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_unrestrictedPdf.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

Headers are set for the request, including the API key which must be replaced with your own key obtained from pdfRest.

response = requests.post(unrestricted_pdf_endpoint_url, data=mp_encoder_unrestrictedPdf, headers=headers)

The POST request is sent to the API endpoint with the encoded file data and headers.

Beyond the Tutorial

This tutorial has shown how to remove restrictions from a PDF file using the pdfRest API and Python. By sending a properly formatted multipart/form-data request to the pdfRest API, the user can programmatically unrestrict a PDF, which could then be used for further processing or integration into other systems.

Interested users are encouraged to experiment with all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for further details on the available endpoints and their usage.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdf-rest-api-samples repository on GitHub.

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