You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用pyHanko创建可见签名字段?签名及字段不可见问题咨询

pyHanko生成不可见签名的问题排查

你的代码里导致签名不可见的核心问题是签名字段的box参数设置错误:

box=(100, 100, 100, 100)

这个参数的格式是(x1, y1, x2, y2),分别代表签名框左下角的x/y坐标,以及右上角的x/y坐标。你设置的x1=x2、y1=y2,相当于把签名框定义成了一个没有宽度和高度的点,自然无法显示任何内容。

修复方案

把box参数改成有实际尺寸的范围,比如设置一个宽200、高100的签名框:

def sign_ac(path):
    path_cert = "path to my cert.pfx"
    signer = signers.SimpleSigner.load_pkcs12(pfx_file=path_cert, passphrase=b'mdp')
    with open(path, 'rb') as doc:
        w = IncrementalPdfFileWriter(doc, strict=False)
        # 修改box参数,设置合理的宽高(x1, y1, x2, y2)
        append_signature_field(w,
                               sig_field_spec=SigFieldSpec(sig_field_name="Signa1", box=(100, 100, 300, 200),
                                                           visible_sig_settings=VisibleSigSettings(rotate_with_page=True)),
                               )
        meta = signers.PdfSignatureMetadata(field_name='Signa1')
        pdf_signer = signers.PdfSigner(
            meta, signer=signer, stamp_style=stamp.TextStampStyle(
                stamp_text='This is custom text!\nSigned by: %(signer)s\nTime: %(ts)s',
                text_box_style=text.TextBoxStyle(
                    font=opentype.GlyphAccumulatorFactory('path to font police.ttf'),
                ),
                background=images.PdfImage('path to img.jpg')
            )
        )
        with open('path to signed doc.pdf', 'wb') as outf:
            pdf_signer.sign_pdf(w, output=outf)

额外注意点

  • 注意PDF的坐标体系:原点在页面左下角,x轴向右,y轴向上,设置box时要确保坐标在页面范围内,避免签名框超出页面显示区域。
  • 检查字体文件和背景图片的路径是否正确,路径错误可能导致签名内容显示异常。
  • 代码里的Thus is custom text是拼写错误,建议改成This is custom text,避免签名文本显示错误。

内容的提问来源于stack exchange,提问作者Marvin ROSILLETTE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 03:27:57