Skip to content

Commit

Permalink
Merge pull request #474 from chinapandaman/PPF-473
Browse files Browse the repository at this point in the history
PPF-473: fix schema for sejda create widget
  • Loading branch information
chinapandaman authored Feb 2, 2024
2 parents 30cd24c + 0841833 commit 4ca0837
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
6 changes: 5 additions & 1 deletion PyPDFForm/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ def create_widget(
).watermarks(self.read())

self.stream = merge_watermarks_with_pdf(self.read(), watermarks)
self.widgets[name] = build_widgets(self.read())[name]
new_widgets = build_widgets(self.read())
for k, v in self.widgets.items():
if k in new_widgets:
new_widgets[k] = v
self.widgets = new_widgets
if widget_type == "text":
self.widgets[name].font = self.global_font
self.widgets[name].font_size = self.global_font_size
Expand Down
Binary file not shown.
Binary file added pdf_samples/widget/create_widget_sejda.pdf
Binary file not shown.
71 changes: 71 additions & 0 deletions tests/test_create_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,74 @@ def test_create_text_complex_filled(template_stream, pdf_samples, request):

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


def test_create_checkbox_persist_old_widgets(template_stream, pdf_samples, request):
expected_path = os.path.join(pdf_samples, "widget", "create_checkbox_persist_old_widgets.pdf")
with open(expected_path, "rb+") as f:
obj = PdfWrapper(template_stream, global_font="Courier")
obj.widgets["test"].font_size = 30
obj.widgets["test"].font_color = (0, 1, 0)
obj.create_widget(
"checkbox",
"foo",
1,
100,
100,
).fill(obj.sample_data)
assert obj.schema["properties"]["foo"]["type"] == "boolean"

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_widget_sejda(sejda_template, pdf_samples, request):
expected_path = os.path.join(pdf_samples, "widget", "create_widget_sejda.pdf")
with open(expected_path, "rb+") as f:
obj = PdfWrapper(sejda_template)
obj.fill(obj.sample_data).create_widget(
widget_type="text",
name="new_text_field_widget",
page_number=1,
x=72,
y=730,
width=120,
height=40,
max_length=6,
font="Times-Roman",
font_size=20,
font_color=(0, 0, 1)
).fill(obj.sample_data)
assert obj.schema["properties"]["new_text_field_widget"]["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_widget_sejda_schema(sejda_template):
schema = PdfWrapper(sejda_template).create_widget(
widget_type="text",
name="new_text_field_widget",
page_number=1,
x=72,
y=730,
width=120,
height=40,
max_length=6,
font="Times-Roman",
font_size=20,
font_color=(0, 0, 1)
).schema

assert schema["properties"]["new_text_field_widget"]
assert len(schema["properties"]) == 1

0 comments on commit 4ca0837

Please sign in to comment.