How to digitally sign PDF documents using Python with an etoken (pen drive)?

久未见 提交于 2019-11-29 23:57:39

问题


How to digitally sign PDF documents using Python? I have an etoken (in pen drive).

Additionally, I have created an excel file using openpyxl and converted it into PDF. Now there is a requirement that I need to add digital signature to that PDF document.

Is there any way I can achieve this in python?


回答1:


Use python module designed for this task, it signs digitally PDF-s. Everything what You should have is p12/pfx file with certificate.

Simple example in: github repository example

#!/usr/bin/env python3
# *-* coding: utf-8 *-*
from oscrypto import asymmetric
from endesive import pdf

def main():
    dct = {
        b'sigflags': 3,
        b'contact': b'me@email.com',
        b'location': b'City',
        b'signingdate': b'20180731082642+02\'00\'',
        b'reason': b'Some descriptive message',
    }
    p12 = asymmetric.load_pkcs12(
        open('demo2_user1.p12', 'rb').read(),
        '1234'
    )
    datau = open('pdf.pdf', 'rb').read()
    datas = pdf.cms.sign(datau, dct, p12[0], p12[1], [], 'sha256')
    with open('pdf-signed-cms.pdf', 'wb') as fp:
        fp.write(datau)
        fp.write(datas)

main()



回答2:


See Sign PDF documents digitally with Python. How you created the file shouldn't make a difference for signing unless the file is encrypted.



来源:https://stackoverflow.com/questions/45058556/how-to-digitally-sign-pdf-documents-using-python-with-an-etoken-pen-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!