How to Encrypt PDF with Python
Why Encrypt PDF with Python?
The pdfRest Encrypt PDF API Tool is a powerful resource for securing PDF documents by adding password protection. This tutorial will guide you through the process of sending an API call to encrypt a PDF using Python.
Encrypting PDFs is essential in scenarios where sensitive information needs to be shared securely, such as sending confidential business reports, legal documents, or personal information that requires protection from unauthorized access.
Encrypt PDF with Python Code Example
from requests_toolbelt import MultipartEncoder import requests import json encrypted_pdf_endpoint_url = 'https://api.pdfrest.com/encrypted-pdf' # The /encrypted-pdf endpoint can take a single PDF file or id as input. # This sample demonstrates encryption of a PDF with the password 'password'. mp_encoder_encryptedPdf = MultipartEncoder( fields={ 'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'), 'output' : 'example_encryptedPdf_out', 'new_open_password': 'password', } ) # Let's set the headers that the encrypted-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_encryptedPdf.content_type, 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here } print("Sending POST request to encrypted-pdf endpoint...") response = requests.post(encrypted_pdf_endpoint_url, data=mp_encoder_encryptedPdf, 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 code reference: pdf-rest-api-samples
Breaking Down the Code
The provided Python code demonstrates how to encrypt a PDF using the pdfRest API. Let's break down the key parts of the code:
from requests_toolbelt import MultipartEncoder import requests import json
This imports the necessary libraries. MultipartEncoder
is used for creating a multipart/form-data payload, requests
for making HTTP requests, and json
for handling JSON data.
mp_encoder_encryptedPdf = MultipartEncoder( fields={ 'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'), 'output' : 'example_encryptedPdf_out', 'new_open_password': 'password', } )
This sets up the multipart/form-data payload with the file to be encrypted, the desired output name, and the new password for opening the PDF.
headers = { 'Accept': 'application/json', 'Content-Type': mp_encoder_encryptedPdf.content_type, 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }
These are the HTTP headers required by the API, including the API key for authentication.
response = requests.post(encrypted_pdf_endpoint_url, data=mp_encoder_encryptedPdf, headers=headers)
This sends the POST request to the API endpoint with the data and headers.
if response.ok: response_json = response.json() print(json.dumps(response_json, indent = 2)) else: print(response.text)
This checks if the request was successful and prints the JSON response; otherwise, it prints the error text.
Beyond the Tutorial
By following the steps above, we've successfully encrypted a PDF using the pdfRest API and Python. You can now explore and demo all of the pdfRest API Tools in the API Lab at API Lab and refer to the API Reference documentation at Cloud API Reference Guide for further details and capabilities.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at pdf-rest-api-samples.