Skip to content

Commit 32e19f2

Browse files
Merge pull request #780 from chinapandaman/PPF-779
PPF-779: support description for radio button
2 parents 061e39c + a8e6c32 commit 32e19f2

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

PyPDFForm/middleware/radio.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def __init__(
2222
def schema_definition(self) -> dict:
2323
"""Json schema definition of the radiobutton."""
2424

25-
return {"type": "integer", "maximum": self.number_of_options - 1}
25+
return {"maximum": self.number_of_options - 1,
26+
**super().schema_definition, "type": "integer"}
2627

2728
@property
2829
def sample_value(self) -> int:

PyPDFForm/patterns.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from .constants import (AP, AS, CA, DA, DV, FT, IMAGE_FIELD_IDENTIFIER, JS, MK,
88
MULTILINE, READ_ONLY, A, Btn, Ch, Ff, N, Off, Opt,
9-
Parent, Q, Sig, T, Tx, V, Yes)
9+
Parent, Q, Sig, T, Tx, TU, V, Yes)
1010
from .middleware.checkbox import Checkbox
1111
from .middleware.dropdown import Dropdown
1212
from .middleware.image import Image
@@ -68,6 +68,11 @@
6868
{Parent: {T: True}},
6969
]
7070

71+
WIDGET_DESCRIPTION_PATTERNS = [
72+
{TU: True},
73+
{Parent: {TU: True}}
74+
]
75+
7176
DROPDOWN_CHOICE_PATTERNS = [
7277
{Opt: True},
7378
{Parent: {Opt: True}},

PyPDFForm/template.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from reportlab.pdfbase.pdfmetrics import stringWidth
1212

1313
from .constants import (COMB, DEFAULT_FONT_SIZE, MULTILINE, NEW_LINE_SYMBOL,
14-
TU, WIDGET_TYPES, Annots, MaxLen, Rect)
14+
WIDGET_TYPES, Annots, MaxLen, Rect)
1515
from .font import (adjust_paragraph_font_size, adjust_text_field_font_size,
1616
auto_detect_font, get_text_field_font_color,
1717
get_text_field_font_size, text_field_font_size)
@@ -21,7 +21,7 @@
2121
from .middleware.text import Text
2222
from .patterns import (BUTTON_STYLE_PATTERNS, DROPDOWN_CHOICE_PATTERNS,
2323
TEXT_FIELD_FLAG_PATTERNS, WIDGET_ALIGNMENT_PATTERNS,
24-
WIDGET_KEY_PATTERNS, WIDGET_TYPE_PATTERNS,
24+
WIDGET_KEY_PATTERNS, WIDGET_TYPE_PATTERNS, WIDGET_DESCRIPTION_PATTERNS,
2525
update_annotation_name)
2626
from .utils import find_pattern_match, stream_to_io, traverse_pattern
2727
from .watermark import create_watermarks_and_draw
@@ -226,7 +226,13 @@ def get_text_field_max_length(widget: dict) -> Union[int, None]:
226226
def get_widget_description(widget: dict) -> Union[str, None]:
227227
"""Returns the description of the widget if presented or None."""
228228

229-
return widget.get(TU)
229+
result = None
230+
for pattern in WIDGET_DESCRIPTION_PATTERNS:
231+
value = traverse_pattern(pattern, widget)
232+
if value:
233+
result = str(value)
234+
break
235+
return result
230236

231237

232238
def check_field_flag_bit(widget: dict, bit: int) -> bool:

tests/scenario/test_issues.py

+25-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import os
55

66
from PyPDFForm import PdfWrapper
7+
from PyPDFForm.middleware.radio import Radio
8+
from PyPDFForm.template import get_widgets_by_page, get_widget_key
9+
from PyPDFForm.constants import Parent, TU
710

811

912
def test_pdf_form_with_pages_without_widgets(issue_pdf_directory, request):
@@ -305,13 +308,29 @@ def test_get_desc_in_schema(issue_pdf_directory):
305308

306309
assert (
307310
obj.schema["properties"]["P1_checkbox4[0]"]["description"]
308-
== "Part 1. Information About You. Your Full Name. 4. Has your name legally changed since the issuance of your Permanent Resident Card? Select Yes. (Proceed to Item Numbers 5. A. through 5. C.)."
309-
) # noqa
311+
== "Part 1. Information About You. Your Full Name. 4. Has your name legally changed since the issuance of your Permanent Resident Card? Select Yes. (Proceed to Item Numbers 5. A. through 5. C.)." # noqa
312+
)
310313
assert (
311314
obj.schema["properties"]["P1_checkbox4[1]"]["description"]
312-
== "Part 1. Information About You. Your Full Name. 4. Has your name legally changed since the issuance of your Permanent Resident Card? Select No (Proceed to Item Numbers 6. A. through 6. I.)."
313-
) # noqa
315+
== "Part 1. Information About You. Your Full Name. 4. Has your name legally changed since the issuance of your Permanent Resident Card? Select No (Proceed to Item Numbers 6. A. through 6. I.)." # noqa
316+
)
314317
assert (
315318
obj.schema["properties"]["P1_checkbox4[2]"]["description"]
316-
== "Part 1. Information About You. Your Full Name. 4. Has your name legally changed since the issuance of your Permanent Resident Card? Select Not Applicable - I never received my previous card. (Proceed to Item Numbers 6. A. through 6. I.)."
317-
) # noqa
319+
== "Part 1. Information About You. Your Full Name. 4. Has your name legally changed since the issuance of your Permanent Resident Card? Select Not Applicable - I never received my previous card. (Proceed to Item Numbers 6. A. through 6. I.)." # noqa
320+
)
321+
322+
323+
def test_get_desc_in_schema_radio(issue_pdf_directory):
324+
obj = PdfWrapper(os.path.join(issue_pdf_directory, "PPF-620.pdf"))
325+
326+
keys_to_check = []
327+
for key, value in obj.widgets.items():
328+
if isinstance(value, Radio) and value.desc is not None:
329+
keys_to_check.append(key)
330+
331+
for widgets in get_widgets_by_page(obj.read()).values():
332+
for widget in widgets:
333+
key = get_widget_key(widget)
334+
335+
if key in keys_to_check:
336+
assert widget[Parent][TU] == obj.schema["properties"][key]["description"]

0 commit comments

Comments
 (0)