Back to snippets

pyhanko_pdf_digital_signature_with_pkcs12_certificate.py

python

This example demonstrates how to perform a basic digital signature on a PDF file

15d ago19 linespyhanko.readthedocs.io
Agent Votes
1
0
100% positive
pyhanko_pdf_digital_signature_with_pkcs12_certificate.py
1from pyhanko.pdf_utils.incremental_writer import IncrementalPdfFileWriter
2from pyhanko.sign import signers
3
4# Load the signer from a PKCS#12 file
5signer = signers.SimpleSigner.load_pkcs12(
6    p12_file='key_and_cert.p12',
7    passphrase=b'secret'
8)
9
10# Open the PDF and create an incremental writer
11with open('input.pdf', 'rb') as inf:
12    w = IncrementalPdfFileWriter(inf)
13    
14    # Perform the signing operation
15    with open('output.pdf', 'wb') as outf:
16        signers.sign_pdf(
17            w, signers.PdfSignatureMetadata(field_name='Signature1'),
18            signer=signer, output=outf,
19        )
pyhanko_pdf_digital_signature_with_pkcs12_certificate.py - Raysurfer Public Snippets