【问题标题】:How to edit editable pdf using the pdfrw library?如何使用 pdfrw 库编辑可编辑的 pdf?
【发布时间】:2019-06-14 06:02:33
【问题描述】:

我一直在研究如何使用 Python 编辑 PDF,我发现了这篇文章:
How to Populate Fillable PDF's with Python

但是,一旦程序运行并打开 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)

【问题讨论】:

  • 同样的问题,你找到解决办法了吗?

标签: python python-3.x pdf pdfrw


【解决方案1】:

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

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

【讨论】:

  • 不知道为什么它被否决了。您的解决方案为我解决了上述问题@Sergio Sanchez。谢谢!这也是 TLK3 在这里发布的 github.com/pmaupin/pdfrw/issues/84
【解决方案2】:

更新写入功能以使键 AP 和 V 在预览中为我解决了问题

pdfrw.PdfDict(AP=data_dict[key], V=data_dict[key])

【讨论】:

    【解决方案3】:

    该错误是因为没有外观流与该字段相关联,但您以错误的方式创建它。您刚刚分配并流式传输到 AP 字典。您需要做的是在 /AP 字典中为 /N 分配一个间接 Xobject;你需要从头开始创建 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、/Resorces 是可选的(强烈建议使用 /Resources)。

    【讨论】:

      【解决方案4】:

      为了扩展上面塞尔吉奥的回答,下面一行:

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

      应该在 OP 的示例代码中这一行之后放置:

      template_pdf = pdfrw.PdfReader(input_pdf_path)
      

      【讨论】:

      • 答案应该是独立的,而不是附加到其他答案。此外,您尝试在此处添加的信息几乎已经在原始答案中。由于该原始答案已经得到了广泛认可,这意味着它的意图可能一开始就很明确,因此您的添加不仅错位,而且有些多余。
      【解决方案5】:

      如果有人在您想要填充数据的表单上有下拉字段,您可以使用下面的代码。 (可能会省去我经历的麻烦)

      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]
      

      【讨论】:

        猜你喜欢
        • 2019-11-17
        • 2020-05-21
        • 1970-01-01
        • 1970-01-01
        • 2020-11-24
        • 1970-01-01
        • 2020-04-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多