How to edit editable pdf using the pdfrw library?

前端 未结 4 1602
说谎
说谎 2020-12-18 12:05

I been doing research on how to edit PDF using Python and i have found this article:
How to Populate Fillable PDF\'s with Python

However there is a problem on

4条回答
  •  孤城傲影
    2020-12-18 12:48

    The error is because no appearance stream is associated with the field, but you've created it in a wrong way. You've just assigned and stream to AP dictionary. What you need to do is to assign an indirect Xobject to /N in /AP dictionary; and you need to crate Xobject from scratch. The code should be something like the following:

    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)
    

    Note: FYI: /Type, /FormType, /Resorces are optional (/Resources is strongly recomended).

提交回复
热议问题