Coder Social home page Coder Social logo

Comments (3)

t-houssian avatar t-houssian commented on August 23, 2024 1

@tdbs Thanks for the input and updated code. I incorporated your changes and released a new version. Should be working now.

from fillpdf.

tdbs avatar tdbs commented on August 23, 2024

It seems some files have a Parent and Kids keys inside the annotations, this change made it compatible with the new and old files, although i'm not sure about that target[ANNOT_FIELD_KIDS_KEY][0].update, in my cases the Kids always only had one element, but you can probably figure it out

from fillpdf.

Micboule avatar Micboule commented on August 23, 2024

I had some pdf (1.4) that didn't work with your code and I made the following changes to your write function to make it work:

def write_fillable_pdf_mine(input_pdf_path, output_pdf_path, data_dict):

    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'
    ANNOT_FIELD_PARENT_KEY = '/Parent'  # Parent key for older pdf versions
    ANNOT_FIELD_KIDS_KEY = '/Kids'      # Kids key for older pdf versions

    template_pdf = pdfrw.PdfReader(input_pdf_path)
    for Page in template_pdf.pages:
        if Page[ANNOT_KEY]:
            for annotation in Page[ANNOT_KEY]:
                target = annotation if annotation[ANNOT_FIELD_KEY] else annotation[ANNOT_FIELD_PARENT_KEY]
                if target and annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
                    key = target[ANNOT_FIELD_KEY][1:-1] # Remove parentheses
                    if key in data_dict.keys():
                        if target[ANNOT_FORM_type] == ANNOT_FORM_button:
                            # button field i.e. a checkbox
                            target.update( pdfrw.PdfDict( V=pdfrw.PdfName(data_dict[key]) , AS=pdfrw.PdfName(data_dict[key]) ))
                            if target[ANNOT_FIELD_KIDS_KEY]:
                                target[ANNOT_FIELD_KIDS_KEY][0].update( pdfrw.PdfDict( V=pdfrw.PdfName(data_dict[key]) , AS=pdfrw.PdfName(data_dict[key]) ))
                        elif target[ANNOT_FORM_type] == ANNOT_FORM_text:
                            # regular text field
                            target.update( pdfrw.PdfDict( V=data_dict[key], AP=data_dict[key]) )
                            if target[ANNOT_FIELD_KIDS_KEY]:
                                target[ANNOT_FIELD_KIDS_KEY][0].update( pdfrw.PdfDict( V=data_dict[key], AP=data_dict[key]) )
    template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))
    pdfrw.PdfWriter().write(output_pdf_path, template_pdf)

Wow, you solved my problem, the same field name in the PDF that was repeating on multiple pages wasn't filling the field at all.

Now, it does! Fantastic! 👍

Thank you!

from fillpdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.