如何使用pdfrw库编辑可编辑的pdf文件?

8
我一直在研究如何使用Python编辑PDF文件,我找到了这篇文章:
如何使用Python填充可填写的PDF文档
然而,程序运行后打开PDF文档时会出现问题,文档中没有填充任何内容,只有当您单击标签时才会显示数据,当您单击其他位置时数据又会消失。这段代码是从网上找到的,是别人写的。
#! /usr/bin/python

import os
import pdfrw


INVOICE_TEMPLATE_PATH = 'invoice_template.pdf'
INVOICE_OUTPUT_PATH = 'invoice.pdf'


ANNOT_KEY = '/Annots'
ANNOT_FIELD_KEY = '/T'
ANNOT_VAL_KEY = '/V'
ANNOT_RECT_KEY = '/Rect'
SUBTYPE_KEY = '/Subtype'
WIDGET_SUBTYPE_KEY = '/Widget'


def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict):
    template_pdf = pdfrw.PdfReader(input_pdf_path)
    annotations = template_pdf.pages[0][ANNOT_KEY]
    for annotation in annotations:
        if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
            if annotation[ANNOT_FIELD_KEY]:
                key = annotation[ANNOT_FIELD_KEY][1:-1]
                if key in data_dict.keys():
                    annotation.update(
                        pdfrw.PdfDict(V='{}'.format(data_dict[key]))
                    )
    pdfrw.PdfWriter().write(output_pdf_path, template_pdf)


data_dict = {
   'business_name_1': 'Bostata',
   'customer_name': 'company.io',
   'customer_email': 'joe@company.io',
   'invoice_number': '102394',
   'send_date': '2018-02-13',
   'due_date': '2018-03-13',
   'note_contents': 'Thank you for your business, Joe',
   'item_1': 'Data consulting services',
   'item_1_quantity': '10 hours',
   'item_1_price': '$200/hr',
   'item_1_amount': '$2000',
   'subtotal': '$2000',
   'tax': '0',
   'discounts': '0',
   'total': '$2000',
   'business_name_2': 'Bostata LLC',
   'business_email_address': 'hi@bostata.com',
   'business_phone_number': '(617) 930-4294'
}

if __name__ == '__main__':
    write_fillable_pdf(INVOICE_TEMPLATE_PATH, INVOICE_OUTPUT_PATH, data_dict)

我也遇到了同样的问题,你找到解决方案了吗? - gustavz
原始文章链接已经失效,但我在archive.org中找到了一份副本。请点击此链接查看:http://web.archive.org/web/20190220050925/https://bostata.com/post/how_to_populate_fillable_pdfs_with_python/ - abu
6个回答

16

我发现如果您添加NeedAppearances参数,您将解决您的问题:

template_pdf = pdfrw.PdfReader(TEMPLATE_PATH)
template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true'))) 

1
不确定为什么会被踩。你的解决方案对我来说解决了上述问题,@Sergio Sanchez。谢谢!这也是由TLK3在此处发布的https://github.com/pmaupin/pdfrw/issues/84 - Gopinath S

7
更新写函数,将键AP和V固定后,我的预览问题被解决了。
pdfrw.PdfDict(AP=data_dict[key], V=data_dict[key])

3

出现错误是因为该字段未与外观流相对应,而您的创建方式有误。您只是将流分配给了AP字典。您需要做的是将间接Xobject分配给/AP字典中的/N;并且需要从头开始创建Xobject。 代码应该像下面这样:

from pdfrw import PdfWriter, PdfReader, IndirectPdfDict, PdfName, PdfDict

INVOICE_TEMPLATE_PATH = 'untitled.pdf'
INVOICE_OUTPUT_PATH = 'untitled-output.pdf'

field1value = 'im field_1 value'

template_pdf = PdfReader(INVOICE_TEMPLATE_PATH)
template_pdf.Root.AcroForm.Fields[0].V = field1value

#this depends on page orientation
rct = template_pdf.Root.AcroForm.Fields[0].Rect
hight = round(float(rct[3]) - float(rct[1]),2)
width =(round(float(rct[2]) - float(rct[0]),2)

#create Xobject
xobj = IndirectPdfDict(
            BBox = [0, 0, width, hight],
            FormType = 1,
            Resources = PdfDict(ProcSet = [PdfName.PDF, PdfName.Text]),
            Subtype = PdfName.Form,
            Type = PdfName.XObject
            )

#assign a stream to it
xobj.stream = '''/Tx BMC
BT
 /Helvetica 8.0 Tf
 1.0 5.0 Td
 0 g
 (''' + field1value + ''') Tj
ET EMC'''

#put all together
template_pdf.Root.AcroForm.Fields[0].AP = PdfDict(N = xobj)

#output to new file
PdfWriter().write(INVOICE_OUTPUT_PATH, template_pdf)

注意:仅供参考:/Type, /FormType, /Resources 是可选的(/Resources 强烈推荐使用)。

1
如果表单上有下拉菜单需要填充数据,您可以使用以下代码。 (可能会为某些人节省我经历的麻烦)
if key in data_dict.keys():
    #see if its a dropdown
    if('/I' in annotation.keys()):
        #field is a dropdown
        #Check if value is in preset list of dropdown, and at what value
        if data_dict[key] in annotation['/Opt']:
            #Value is in dropdown list,select value from list
            annotation.update(pdfrw.PdfDict(I='[{}]'.format(annotation['/Opt'].index(data_dict[key]))))
        else:
            #Value is not in dropdown list, add as 'free input'
            annotation.update(pdfrw.PdfDict(I='{}'.format(None)))
            annotation.update(pdfrw.PdfDict(V='{}'.format(data_dict[key])))
    else:
        #update the textfieldvalue
        annotation.update(pdfrw.PdfDict(V='{}'.format(data_dict[key])))

请注意,OP代码仅适用于第一页,由于...
template_pdf.pages[0]

1
为了进一步说明Sergio上面的答案,以下是该行代码:
template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))

在原始问题的示例代码中,应该将此行放在后面。
template_pdf = pdfrw.PdfReader(input_pdf_path)

1
答案应该是自成一体的,不要添加到其他答案中。此外,您试图在这里添加的信息基本上已经包含在原始答案中。由于原始答案已经得到了重要的认可,这意味着它的意图可能一开始就很清楚,您的添加不仅位置不当,而且有些多余。 - Amitai Irron

0

我曾经遇到这样的问题,即如果你在Acrobat中打开它,字段值会出现,但是如果你在Chrome或预览中打开它,许多字段将是不可见的,除非你点击它们。并且不能通过编程方式(使用pdfplumber)访问它们。

在Acrobat中将文件转换为PDF/A格式解决了大多数文件的问题(而不是在预览中进行转换)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接