Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write_fill_pdf flatten parameter now preserves multiline edit fields #54

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions fillpdf/fillpdfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def convert_dict_values_to_string(dictionary):
return res


def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict, flatten=False):
def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict, flatten=False, fontinfo=None):
"""
Writes the dictionary values to the pdf. Currently supports text and buttons.
Does so by updating each individual annotation with the contents of the dat_dict.
Expand All @@ -192,12 +192,22 @@ def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict, flatten=False
flatten: bool
Default is False meaning it will stay editable. True means the annotations
will be uneditable.
fontinfo: str
Default is None to preserve template font. Example of fontinfo to change text fields: Arial 8.00 Tf 0 g
Returns
---------
"""
data_dict = convert_dict_values_to_string(data_dict)

template_pdf = pdfrw.PdfReader(input_pdf_path)
flatten_mask = 1
if fontinfo:
PDF_TEXT_APPEARANCE = (
pdfrw.objects.pdfstring.PdfString.encode(
'/'+fontinfo
)
)

for Page in template_pdf.pages:
if Page[ANNOT_KEY]:
for annotation in Page[ANNOT_KEY]:
Expand Down Expand Up @@ -290,8 +300,12 @@ def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict, flatten=False
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]) )
if int(target["/Ff"]) > 0:
flatten_mask |= int(target["/Ff"])
if fontinfo:
target.update({"/DA": PDF_TEXT_APPEARANCE})
if flatten == True:
annotation.update(pdfrw.PdfDict(Ff=1))
annotation.update(pdfrw.PdfDict(Ff=flatten_mask))
template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))
pdfrw.PdfWriter().write(output_pdf_path, template_pdf)

Expand Down Expand Up @@ -591,4 +605,4 @@ def get_coordinate_map(input_pdf_path, output_map_path, page_number=1, **kwargs)
page.insert_text(fitz.Point(x , 12), str(x), fontsize=12, fontname="times-bold", color=(1, 0, 0))
page.draw_line(fitz.Point(x , 12), fitz.Point(x , max_y), color=(1, 0, 0))

doc.save(output_map_path, **kwargs)
doc.save(output_map_path, **kwargs)