python-3.x - 用 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,也许是原因?)
解决方案
推荐阅读
- react-native - 反应导航中的 FlatList 渲染问题
- markdown - 编号列表中生成的编号是否可以被另一个标记使用/引用
- typescript - 标头没有发送离子
- javascript - 如何在域上连接前端和后端
- c# - 从错误线程返回结果的函数
- javascript - 具有多个系列的 Highchart.js 的正确 json 格式
- python - 如何遍历字典中的一堆列表?
- c - SetThreadContext 有效,但寄存器没有改变
- regular-language - L= {0^i1^j | 的最小泵送长度 i >= j} 证明 L 是非正则的
- python - 调用 tf.image.rgb_to_grayscale 后无法显示图像