数字签名
from ironpdf import *

# Cryptographically sign an existing PDF in 1 line of code!
PdfSignature(r".\certificates\IronSoftware.p12", "123456").SignPdfFile("any.pdf")

##### Advanced example for more control #####

# Step 1. Create a PDF
renderer = ChromePdfRenderer()
doc = renderer.RenderHtmlAsPdf("<h1>Testing 2048 bit digital security</h1>")

# Step 2. Create a Signature.
# You may create a .pfx or .p12 PDF signing certificate using Adobe Acrobat Reader.
# Read: https://helpx.adobe.com/acrobat/using/digital-ids.html
signature = PdfSignature(r"certificates\IronSoftware.pfx", "123456")

# Step 3. Optional signing options and a handwritten signature graphic
signature.SigningContact = "support@ironsoftware.com"
signature.SigningLocation = "Chicago, USA"
signature.SigningReason = "To show how to sign a PDF"

# Step 4. Sign the PDF with the PdfSignature. Multiple signing certificates may be used
doc.Sign(signature)

# Step 5. The PDF is not signed until saved to file, steam or byte array.
doc.SaveAs("signed.pdf")
pip install ironpdf
数字签名

数字签名

此Python代码演示如何使用PDF库,如PyPDF2,对现有PDF进行密码签名并创建带有数字签名的PDF。

如何在Python中生成数字签名

  1. 下载Python模块以生成数字签名。 您可以使用诸如reportlab来进行数字签名。

  2. 渲染一个新的PDF文档。 使用reportlab来创建新的PDF或修改现有的PDF。

  3. 实例化PdfSignature类并导入数字证书。 下面的代码片段展示了如何使用一个假想的PdfSignature类来实现此功能。 用您选择的库的实际方法替换以添加签名。

  4. 根据需要添加附加信息。 指定元数据、外观或签名的位置。

  5. 使用sign方法签署该文档。 下面是一个使用PyPDF2附加数字签名的Python代码。

注意:使用PyPDF2对PDF进行数字签名是一个复杂过程,可能会涉及到不同的库,具体取决于需求。 在实现时,请确保您拥有必要的证书和密码学原理的理解。

在 GitHub 上查看数字签名 PDF 示例
在 GitHub 上查看

准备开始了吗?

版本:2026.11刚刚发布