How to Digitally Sign PDF Files with Python

Learn how to apply digital signatures to PDFs using pdfRest's Sign PDF API Tool with Python.
Share this page

Why Digitally Sign PDFs with Python?

The pdfRest Sign PDF API Tool is a powerful resource for developers looking to automate the process of digitally signing PDF documents. By integrating this tool with Python, developers can streamline workflows, enhance document security, and ensure compliance with digital signature standards. This tutorial will guide you through the process of sending an API call to the Sign PDF endpoint using Python, making it easy to incorporate digital signatures into your applications.

Digital signatures are essential for businesses and individuals who need to verify the authenticity and integrity of their documents. For example, a company may use the Sign PDF API to automatically sign contracts or agreements before sending them to clients, ensuring that the documents are tamper-proof and legally binding. This not only saves time but also reduces the risk of human error in the document signing process.

Sign PDF with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

signed_pdf_endpoint_url = 'https://api.pdfrest.com/signed-pdf'

signature_config = {
    "type": "new",
    "name": "esignature",
    "logo_opacity": "0.5",
    "location": {
        "bottom_left": { "x": "0", "y": "0" },
        "top_right": { "x": "216", "y": "72" },
        "page": 1
    },
    "display": {
        "include_distinguished_name": "true",
        "include_datetime": "true",
        "contact": "My contact information",
        "location": "My signing location",
        "name": "John Doe",
        "reason": "My reason for signing"
    }
}

mp_encoder_signPDF = MultipartEncoder(
    fields={
        'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
        'pfx_credential_file': ('file_name.pfx', open('/path/to/file_name.pfx', 'rb'), 'application/x-pkcs12'),
        'pfx_passphrase_file': ('file_name.txt', open('/path/to/file_name.txt', 'rb'), 'text/plain'),
        'logo_file': ('file_name.jpg', open('/path/to/file_name.jpg', 'rb'), 'image/jpeg'),
        'signature_configuration': json.dumps(signature_config),
        'output' : 'example_out'
    }
)

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

print("Sending POST request to signed-pdf endpoint...")
response = requests.post(signed_pdf_endpoint_url, data=mp_encoder_signPDF, 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 Repository

Breaking Down the Code

The code begins by importing the necessary libraries: requests_toolbelt, requests, and json. The signed_pdf_endpoint_url is defined as the URL for the Sign PDF API endpoint.

signature_config = {
    "type": "new",
    "name": "esignature",
    ...
}

The signature_config dictionary configures the signature's appearance and metadata. It includes the signature type, name, logo opacity, and display options such as contact information and signing reason.

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

The MultipartEncoder is used to construct the multipart form data payload. It includes the PDF file, PFX credential file, passphrase file, logo file, and the JSON string of the signature configuration.

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_signPDF.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

The headers dictionary specifies the request headers, including the Content-Type set to the multipart encoder's content type and the Api-Key for authentication.

response = requests.post(signed_pdf_endpoint_url, data=mp_encoder_signPDF, headers=headers)

The requests.post method sends the POST request to the API endpoint with the multipart data and headers. The response is then checked for success, and the JSON response is printed if successful.

Beyond the Tutorial

In this tutorial, you learned how to use Python to send a request to the pdfRest Sign PDF API, allowing you to digitally sign PDF documents programmatically. You can explore other pdfRest API Tools in the API Lab and refer to the API Reference Guide for more detailed information.

Note: This example demonstrates a multipart API call. For code samples using JSON payloads, visit the GitHub Repository.

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