Skip to content

Commit

Permalink
Merge pull request #540 from chinapandaman/PPF-539
Browse files Browse the repository at this point in the history
PPF-539: fix simple fill for comb template
  • Loading branch information
chinapandaman authored Mar 28, 2024
2 parents 9b0bcc2 + 0f953f3 commit 3523d55
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 7 deletions.
5 changes: 3 additions & 2 deletions PyPDFForm/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
Tx = "/Tx"
V = "/V"
AP = "/AP"
D = "/D"
Sig = "/Sig"
DA = "/DA"
Btn = "/Btn"
Expand All @@ -47,6 +48,8 @@
MK = "/MK"
CA = "/CA"
AS = "/AS"
Yes = "/Yes"
Off = "/Off"

# Field flag bits
READ_ONLY = 1 << 0
Expand All @@ -70,6 +73,4 @@
"l": "\u25CF", # circle
}

Yes = "/Yes"

COORDINATE_GRID_FONT_SIZE_MARGIN_RATIO = DEFAULT_FONT_SIZE / 100
2 changes: 1 addition & 1 deletion PyPDFForm/filler.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def simple_fill(
radio_button_tracker[key] = 0
radio_button_tracker[key] += 1
if widget.value == radio_button_tracker[key] - 1:
simple_update_radio_value(annot, widget)
simple_update_radio_value(annot)
elif isinstance(widget, Dropdown) and widget.value is not None:
simple_update_dropdown_value(annot, widget)
elif isinstance(widget, Text) and widget.value:
Expand Down
11 changes: 7 additions & 4 deletions PyPDFForm/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from pypdf.generic import (DictionaryObject, NameObject, NumberObject,
TextStringObject)

from .constants import (AP, AS, CA, DA, FT, MK, READ_ONLY, Btn, Ch, Ff, Opt,
Parent, Q, Sig, Subtype, T, Tx, V, Widget, Yes)
from .constants import (AP, AS, CA, D, DA, FT, MK, READ_ONLY, Btn, Ch, Ff, Opt,
Parent, Q, Sig, Subtype, T, Tx, V, Widget, Yes, Off)
from .middleware.checkbox import Checkbox
from .middleware.dropdown import Dropdown
from .middleware.radio import Radio
Expand Down Expand Up @@ -86,10 +86,13 @@ def simple_update_checkbox_value(annot: DictionaryObject) -> None:
annot[NameObject(AS)] = NameObject(Yes)


def simple_update_radio_value(annot: DictionaryObject, widget: Radio) -> None:
def simple_update_radio_value(annot: DictionaryObject) -> None:
"""Patterns to update values for radio annotations."""

annot[NameObject(AS)] = NameObject(f"/{widget.value}")
for each in annot[AP][D]: # noqa
if str(each) != Off:
annot[NameObject(AS)] = NameObject(each)
break


def simple_update_dropdown_value(annot: DictionaryObject, widget: Dropdown) -> None:
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
116 changes: 116 additions & 0 deletions tests/test_fill_max_length_text_field_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,119 @@ def test_fill_max_length_text_field_even_chars(

assert len(obj.stream) == len(expected)
assert obj.stream == expected


def test_fill_comb_text_field_all_chars(
sample_template_with_comb_text_field, pdf_samples, request
):
expected_path = os.path.join(
pdf_samples,
"simple",
"max_length_text_field_related",
"comb_text_field_all_chars.pdf",
)
with open(expected_path, "rb+") as f:
obj = FormWrapper(sample_template_with_comb_text_field).fill(
{
"FirstName": "John",
"MiddleName": "Joe",
"LastName": "XXXXXXXXXX",
"Awesomeness": True,
"Gender": 0,
}
)

request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()

expected = f.read()

assert len(obj.stream) == len(expected)
assert obj.stream == expected


def test_fill_comb_text_field_odd_chars(
sample_template_with_comb_text_field, pdf_samples, request
):
expected_path = os.path.join(
pdf_samples,
"simple",
"max_length_text_field_related",
"comb_text_field_odd_chars.pdf",
)
with open(expected_path, "rb+") as f:
obj = FormWrapper(sample_template_with_comb_text_field).fill(
{
"FirstName": "John",
"MiddleName": "Joe",
"LastName": "XXX",
"Awesomeness": True,
"Gender": 0,
}
)

request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()

expected = f.read()

assert len(obj.stream) == len(expected)
assert obj.stream == expected


def test_fill_comb_text_field_even_chars(
sample_template_with_comb_text_field, pdf_samples, request
):
expected_path = os.path.join(
pdf_samples,
"simple",
"max_length_text_field_related",
"comb_text_field_even_chars.pdf",
)
with open(expected_path, "rb+") as f:
obj = FormWrapper(sample_template_with_comb_text_field).fill(
{
"FirstName": "John",
"MiddleName": "Joe",
"LastName": "XXXXXX",
"Awesomeness": True,
"Gender": 0,
}
)

request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()

expected = f.read()

assert len(obj.stream) == len(expected)
assert obj.stream == expected


def test_fill_comb_text_field_even_chars_right_aligned(
sample_template_with_comb_text_field_right_aligned, pdf_samples, request
):
expected_path = os.path.join(
pdf_samples,
"simple",
"max_length_text_field_related",
"comb_text_field_even_chars_right_aligned.pdf",
)
with open(expected_path, "rb+") as f:
obj = FormWrapper(sample_template_with_comb_text_field_right_aligned).fill(
{
"FirstName": "John",
"MiddleName": "Joe",
"LastName": "XXXXXX",
"Awesomeness": True,
"Gender": 0,
}
)

request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()

expected = f.read()

assert len(obj.stream) == len(expected)
assert obj.stream == expected

0 comments on commit 3523d55

Please sign in to comment.