Skip to content

Commit

Permalink
Merge pull request #713 from chinapandaman/PPF-711
Browse files Browse the repository at this point in the history
PPF-711: support create multiline text field
  • Loading branch information
chinapandaman authored Aug 23, 2024
2 parents 7c0139e + a712bb2 commit 075bb4c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
16 changes: 14 additions & 2 deletions PyPDFForm/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pypdf.generic import (DictionaryObject, NameObject, NumberObject,
TextStringObject)

from .constants import (AP, AS, CA, DA, DV, FT, IMAGE_FIELD_IDENTIFIER, JS, MK,
from .constants import (AP, AS, CA, DA, DV, FT, IMAGE_FIELD_IDENTIFIER, JS, MK, MULTILINE,
READ_ONLY, A, Btn, Ch, Ff, N, Off, Opt, Parent, Q, Sig,
T, Tx, V, Yes)
from .middleware.checkbox import Checkbox
Expand Down Expand Up @@ -166,4 +166,16 @@ def update_created_text_field_alignment(annot: DictionaryObject, val: int) -> No
annot[NameObject(Q)] = NumberObject(val)


NON_ACRO_FORM_PARAM_TO_FUNC = {"alignment": update_created_text_field_alignment}
def update_created_text_field_multiline(annot: DictionaryObject, val: bool) -> None:
"""Patterns to update to multiline for text annotations created by the library."""

if val:
annot[NameObject(Ff)] = NumberObject(
int(annot[NameObject(Ff)]) | MULTILINE # noqa
)


NON_ACRO_FORM_PARAM_TO_FUNC = {
("TextWidget", "alignment"): update_created_text_field_alignment,
("TextWidget", "multiline"): update_created_text_field_multiline,
}
2 changes: 1 addition & 1 deletion PyPDFForm/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(

for each in self.ALLOWED_NON_ACRO_FORM_PARAMS:
if each in kwargs:
self.non_acro_form_params.append((each, kwargs.get(each)))
self.non_acro_form_params.append(((type(self).__name__, each), kwargs.get(each)))

def watermarks(self, stream: bytes) -> List[bytes]:
"""Returns a list of watermarks after creating the widget."""
Expand Down
2 changes: 1 addition & 1 deletion PyPDFForm/widgets/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ class TextWidget(Widget):
("max_length", "maxlen"),
]
COLOR_PARAMS = ["font_color", "bg_color", "border_color"]
ALLOWED_NON_ACRO_FORM_PARAMS = ["alignment"]
ALLOWED_NON_ACRO_FORM_PARAMS = ["alignment", "multiline"]
NONE_DEFAULTS = ["max_length"]
ACRO_FORM_FUNC = "textfield"
3 changes: 2 additions & 1 deletion docs/prepare.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ new_form = PdfWrapper("dummy.pdf").create_widget(
bg_color=(0, 0, 1), # optional
border_color=(1, 0, 0), # optional
border_width=5, # optional
alignment=0 # optional, 0=left, 1=center, 2=right
alignment=0, # optional, 0=left, 1=center, 2=right
multiline=True # optional
)

with open("output.pdf", "wb+") as output:
Expand Down
Binary file not shown.
22 changes: 22 additions & 0 deletions tests/test_create_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,28 @@ def test_create_text_align_right(template_stream, pdf_samples, request):
assert obj.stream == expected


def test_create_text_align_multiline(template_stream, pdf_samples, request):
expected_path = os.path.join(pdf_samples, "widget", "create_text_align_multiline.pdf")
with open(expected_path, "rb+") as f:
obj = PdfWrapper(template_stream).create_widget(
"text",
"foo",
1,
100,
100,
multiline=True,
)
assert obj.schema["properties"]["foo"]["type"] == "string"

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_create_text_default_filled(template_stream, pdf_samples, request):
expected_path = os.path.join(
pdf_samples, "widget", "create_text_default_filled.pdf"
Expand Down

0 comments on commit 075bb4c

Please sign in to comment.