看看pyHanko吧。你可以使用Python添加、编辑和数字签名PDF文件。
https://github.com/MatthiasValvekens/pyHanko
它是完全免费的。如果你遇到任何问题,Matthias非常乐意提供帮助并且很快回复。
# Open an existing PDF
doc = PDFDoc(docpath)
page1 = doc.GetPage(1)
# Create a text field that we can lock using the field permissions feature.
annot1 = TextWidget.Create(doc.GetSDFDoc(), Rect(50, 550, 350, 600), "asdf_test_field")
page1.AnnotPushBack(annot1)
# Create a new signature form field in the PDFDoc. The name argument is optional;
# leaving it empty causes it to be auto-generated. However, you may need the name for later.
# Acrobat doesn't show digsigfield in side panel if it's without a widget. Using a
# Rect with 0 width and 0 height, or setting the NoPrint/Invisible flags makes it invisible.
certification_sig_field = doc.CreateDigitalSignatureField(cert_field_name)
widgetAnnot = SignatureWidget.Create(doc, Rect(0, 100, 200, 150), certification_sig_field)
page1.AnnotPushBack(widgetAnnot)
...
# Save the PDFDoc. Once the method below is called, PDFNet will also sign the document using the information provided.
doc.Save(outpath, 0)
你可以使用https://github.com/mstamy2/PyPDF2来生成PDF文件。
然后使用开源的Java-Digital-Signature
:Java命令行工具,用于使用PKCS#11令牌进行数字签名:https://github.com/AlessioScarfone/Java-Digital-Signature
并在你的Python代码中调用:
import subprocess
subprocess.call(['java', '-jar', 'signer.jar', 'pades', 'test.pdf'])
我使用Python的signpdf库来签署PDF。
阅读此文档以更好地理解https://github.com/yourcelf/signpdf
pip install signpdf
演示:
了解坐标:Github链接Sign the first page of "contract.pdf" with the signature "sig.png": ->
signpdf contract.pdf sig.png --coords 1x100x100x150x40