How to Restrict PDF with Python

Learn how to use pdfRest Restrict PDF API Tool to restrict PDF file access using Python
Share this page

Why Use Restrict PDF with Python?

The pdfRest Restrict PDF API Tool offers a convenient way to apply restrictions to PDF documents programmatically. This tutorial will guide you through the process of sending an API call to the Restrict PDF endpoint using Python.

Imagine you have a PDF document that you want to share with others, but you need to limit their ability to print or edit the document. Using the Restrict PDF API, you can enforce these limitations, ensuring your document is used as intended.

Restrict PDF with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

restricted_pdf_endpoint_url = 'https://api.pdfrest.com/restricted-pdf'

# The /restricted-pdf endpoint can take a single PDF file or id as input.
# This sample demonstrates setting the permissions password to 'password' and adding restrictions.
mp_encoder_restrictedPdf = MultipartEncoder(
    fields=[
        ('file', ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf')),
        ('output', 'example_restrictedPdf_out'),
        ('new_permissions_password', 'password'),
        ('restrictions', 'print_high'),
        ('restrictions', 'print_low'),
        ('restrictions', 'edit_content')
    ]
)

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

print("Sending POST request to restricted-pdf endpoint...")
response = requests.post(restricted_pdf_endpoint_url, data=mp_encoder_restrictedPdf, 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-rest-api-samples on GitHub

Breaking Down the Code

The code begins by importing necessary modules and setting up the endpoint URL:

from requests_toolbelt import MultipartEncoder
import requests
import json

restricted_pdf_endpoint_url = 'https://api.pdfrest.com/restricted-pdf'

Next, we create a MultipartEncoder object to prepare the multipart form-data payload:

mp_encoder_restrictedPdf = MultipartEncoder(
    fields=[
        ('file', ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf')),
        ...
    ]
)

Each tuple in the fields list represents a form field. The 'file' field includes the file name, file object, and MIME type. The 'output' field specifies the output file name. The 'new_permissions_password' sets the password required to change permissions, and the 'restrictions' fields add various restrictions to the PDF.

Headers are set to include the 'Accept' type, the 'Content-Type' (determined by the MultipartEncoder), and your API key:

headers = {
    ...
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
}

Finally, a POST request is sent to the endpoint, and the response is handled:

response = requests.post(restricted_pdf_endpoint_url, data=mp_encoder_restrictedPdf, headers=headers)
...

The response is checked for success, and the JSON response is printed if the request was successful.

Beyond the Tutorial

By following this tutorial, you've learned how to make a multipart API call to the Restrict PDF endpoint with Python. This allows you to programmatically apply restrictions to PDF documents. You can demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation for further exploration.

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

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