Comments (3)
@tdbs Thanks for the input and updated code. I incorporated your changes and released a new version. Should be working now.
from fillpdf.
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.
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)
- i-9 autofill HOT 2
- Form content gets removed by CUPS due to some error in the PDF
- Place text and Greek characters HOT 2
- 'get_form_fields' not getting all form fields HOT 1
- How to set font-size when using write_fillable_pdf()?
- 'Check Box': 'Yes' does not check boxes in PDF. HOT 1
- How to automatically save the file after it was filled?
- How to rename form fields
- Direct one or more filled fields to be RTL
- Multiline does not wrap to size of the field HOT 1
- Error after 0.7.2 update "TypeError: 'NoneType' object is not subscriptable"
- Skipping certain fields when trying to get the data_dict fillpdfs.get_form_fields(pdf, sort=True, page_number=1) HOT 2
- Unsure if I am doing something wrong HOT 2
- Identical Fields cause crash!!
- How to add another page to the PDF?
- One Field is not updated - non-editable field probably?
- write_fillable_pdf does not properly flatten combed fields.
- Does not mark a radio checkbox that has child checkboxes
- radio groups can not be fill HOT 4
- Check box selection using fillpdf HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fillpdf.