首页 > 解决方案 > 用 pdfrw 填充 PDF 不显示值

问题描述

我希望能够用 Python 填写 PDF。PDF 正在使用 pdfrw 库创建。当我在创建它后尝试打开它时,没有显示表单字段。

# Read the source
template_pdf = pdfrw.PdfReader(pdfSrc)

# Adding the default pages
out_pdf = pdfrw.PdfWriter()
out_pdf.addPage(template_pdf.getPage(0))
out_pdf.addPage(template_pdf.getPage(1))

# Add third or more pages (as needed)
if (nbPagesMore > 0) :
    for j in range(nbPagesMore) :
        out_pdf.addPage(template_pdf.getPage(2))
else : 
    out_pdf.addPage(template_pdf.getPage(2))

f = tempfile.NamedTemporaryFile(delete=False, suffix='.pdf')
f.close()

# Write PDF
out_pdf.write(f.name)

# Inspired from [https://stackoverflow.com/a/61627477/12452515]

ANNOT_KEY = '/Annots'           # key for all annotations within a page
ANNOT_FIELD_KEY = '/T'          # Name of field. i.e. given ID of field
ANNOT_FORM_type = '/FT'         # Form type (e.g. text/button)
ANNOT_FORM_button = '/Btn'      # ID for buttons, i.e. a checkbox
ANNOT_FORM_text = '/Tx'         # ID for textbox
SUBTYPE_KEY = '/Subtype'
WIDGET_SUBTYPE_KEY = '/Widget'

k = 0

# Re-read the source with pages added
# dictPdf contain for each page, fields 
pdf = pdfrw.PdfReader(f.name)
for page in pdf.pages:
    if page[ANNOT_KEY]:
        for annotation in page[ANNOT_KEY]:
            if annotation[ANNOT_FIELD_KEY] and annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY :
                key = annotation[ANNOT_FIELD_KEY][1:-1] # Remove parentheses
                if key in dictPdf[k].keys():
                    if annotation[ANNOT_FORM_type] == ANNOT_FORM_button:
                        # button field i.e. a checkbox
                        annotation.update( pdfrw.PdfDict( V=pdfrw.PdfName(dictPdf[k][key]) , AS=pdfrw.PdfName(dictPdf[k][key]) ))
                    elif annotation[ANNOT_FORM_type] == ANNOT_FORM_text:
                        # regular text field
                        annotation.update( pdfrw.PdfDict( V=dictPdf[k][key], AP=dictPdf[k][key]) )
    k += 1

#pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))

pdfrw.PdfWriter().write(f.name, pdf)

如果我取消注释此行pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true'))),我会收到错误

pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))
AttributeError: 'NoneType' object has no attribute 'update'

如果我尝试使用默认 PDF 来填写表格,它可以工作(但我没有添加页面): : pdf = pdfrw.PdfReader(pdfSrc)

最后,我用文本编辑器打开了 PDF,如果我在其中搜索一个值,我会找到它!(生成的 PDF 似乎是 1.3,也许是原因?)

标签: python-3.xpdfpdfrw

解决方案


推荐阅读