Skip to content

arnaullfe/python-pdf-digital-signature

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to Sign PDFs Using Digital Certificates in Python

PDFSignify dashboard

A complete Python tutorial showing how to digitally sign PDF files using .pfx or .p12 certificates with the PDFSignify API.

This guide covers:

  • Sign PDFs in Python
  • Upload .pfx / .p12 certificates
  • Use the PDFSignify REST API
  • Customize PDF signatures
  • Add metadata to signed PDFs
  • Save signed PDFs automatically

Table of Contents


Introduction

This tutorial explains how to digitally sign PDF documents using Python and PDFSignify.

Digital signatures help verify:

  • document authenticity
  • document integrity
  • signer identity

You need:

  • a PDF file
  • a .pfx or .p12 certificate
  • the certificate password
  • PDFSignify API credentials

1. Create the Python Project

mkdir python-pdf-signing
cd python-pdf-signing

2. Create a Virtual Environment

Create the virtual environment:

python3 -m venv venv

Activate it on Linux/macOS:

source venv/bin/activate

Activate it on Windows:

venv\Scripts\activate

3. Install Dependencies

Install requests:

pip install requests

Optional:

pip freeze > requirements.txt

4. Create a PDFSignify Account

Go to:

https://pdfsignify.com/

Create an account or log in.

PDFSignify register

PDFSignify login

Verify your email:

Verify email

Confirm email

If needed, resend the verification email:

Resend email

Create a project:

Pricing plans

Create project


5. Create API Credentials

Open the API credentials section.

API credentials

Create credentials:

Create API credentials

Save:

  • AccessKey
  • SecretKey

6. Prepare the PDF and Certificate

Project structure:

python-pdf-signing/
├── sign_pdf.py
├── filepdf.pdf
├── certificate.pfx
└── venv/

Supported certificate formats:

  • .pfx
  • .p12

7. Sign PDFs in Python

Create sign_pdf.py:

import requests

API_URL = "https://api.pdfsignify.com/api/v1/sign-pdf"

ACCESS_KEY = "MY_ACCESS_KEY"
SECRET_KEY = "MY_SECRET_KEY"

CERTIFICATE_PATH = "certificate.pfx"
PDF_PATH = "filepdf.pdf"

CERTIFICATE_PASSWORD = "YOUR_CERTIFICATE_PASSWORD"

headers = {
    "AccessKey": ACCESS_KEY,
    "SecretKey": SECRET_KEY,
}

data = {
    "certificatePassword": CERTIFICATE_PASSWORD,
}

with open(CERTIFICATE_PATH, "rb") as certificate_file, open(PDF_PATH, "rb") as pdf_file:

    files = {
        "certificate": (
            "certificate.pfx",
            certificate_file,
            "application/x-pkcs12"
        ),

        "pdf": (
            "filepdf.pdf",
            pdf_file,
            "application/pdf"
        ),
    }

    response = requests.post(
        API_URL,
        headers=headers,
        files=files,
        data=data
    )

if response.status_code != 200:
    print("Error signing PDF")
    print(response.text)
    exit(1)

with open("signed_filepdf.pdf", "wb") as signed_pdf:
    signed_pdf.write(response.content)

print("PDF signed successfully")

Run the script:

python sign_pdf.py

8. Customize the Signature

Example signature configuration:

data = {
    "certificatePassword": CERTIFICATE_PASSWORD,

    "signaturePageAppearance": "-1",
    "timezone": "UTC",

    "signatureMessage": "Digitally signed by the user",

    "signatureDateFormat": "Y-m-d H:i:s",

    "signatureHeight": "100",
    "signatureWidth": "150",

    "signatureYPosition": "100",
    "signatureXPosition": "180",
}

9. Add PDF Metadata

data = {
    "certificatePassword": CERTIFICATE_PASSWORD,

    "pdfMetadataAuthor": "AUTHOR",
    "pdfMetadataKeywords": "keywords,keyword2",

    "pdfMetadataTitle": "MY DOCUMENT",
    "pdfMetadataSubject": "EXAMPLE DOCUMENT",

    "pdfMetadataCreator": "PDFSIGNIFY",
    "pdfMetadataProducer": "PDFSIGNIFY",
}

10. Complete Example

import requests
from datetime import datetime

API_URL = "https://api.pdfsignify.com/api/v1/sign-pdf"

ACCESS_KEY = "MY_ACCESS_KEY"
SECRET_KEY = "MY_SECRET_KEY"

CERTIFICATE_PATH = "certificate.pfx"
PDF_PATH = "filepdf.pdf"

CERTIFICATE_PASSWORD = "YOUR_CERTIFICATE_PASSWORD"

OUTPUT_PATH = "signed_filepdf.pdf"

headers = {
    "AccessKey": ACCESS_KEY,
    "SecretKey": SECRET_KEY,
}

current_datetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

data = {
    "certificatePassword": CERTIFICATE_PASSWORD,

    "signaturePageAppearance": "-1",
    "timezone": "UTC",

    "signatureMessage": "Digitally signed by the user",

    "signatureDateFormat": "Y-m-d H:i:s",

    "signatureHeight": "100",
    "signatureWidth": "150",

    "signatureYPosition": "100",
    "signatureXPosition": "180",

    "pdfMetadataAuthor": "AUTHOR",
    "pdfMetadataKeywords": "keywords,keyword2",

    "pdfMetadataTitle": "MY DOCUMENT",
    "pdfMetadataSubject": "EXAMPLE DOCUMENT",

    "pdfMetadataCreator": "PDFSIGNIFY",
    "pdfMetadataProducer": "PDFSIGNIFY",

    "pdfMetadataCreationDate": current_datetime,
    "pdfMetadataModificationDate": current_datetime,
}

with open(CERTIFICATE_PATH, "rb") as certificate_file, open(PDF_PATH, "rb") as pdf_file:

    files = {
        "certificate": (
            "certificate.pfx",
            certificate_file,
            "application/x-pkcs12"
        ),

        "pdf": (
            "filepdf.pdf",
            pdf_file,
            "application/pdf"
        ),
    }

    response = requests.post(
        API_URL,
        headers=headers,
        files=files,
        data=data
    )

if response.status_code != 200:
    print("Error signing PDF")
    print(response.text)
    exit(1)

with open(OUTPUT_PATH, "wb") as signed_pdf:
    signed_pdf.write(response.content)

print(f"PDF signed successfully: {OUTPUT_PATH}")

Useful Links


License

MIT

About

Sign PDF documents in Python using PFX/P12 digital certificates with the PDFSignify API. Complete example for digitally signing PDFs, customizing signatures, and adding PDF metadata.

Topics

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages