Skip to content

Commit

Permalink
Merge pull request #555 from chinapandaman/PPF-553
Browse files Browse the repository at this point in the history
PPF-553: support creating dropdown
  • Loading branch information
chinapandaman committed Apr 2, 2024
2 parents ebb36b2 + ee04621 commit d757040
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 1 deletion.
33 changes: 33 additions & 0 deletions PyPDFForm/widgets/dropdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
"""Contains dropdown widget to create."""

from .base import Widget


class DropdownWidget(Widget):
"""Dropdown widget to create."""

USER_PARAMS = [
("width", "width"),
("height", "height"),
("options", "options"),
("font", "fontName"),
("font_size", "fontSize"),
("font_color", "textColor"),
]
COLOR_PARAMS = ["font_color"]
ACRO_FORM_FUNC = "_textfield"

def __init__(
self,
name: str,
page_number: int,
x: float,
y: float,
**kwargs,
) -> None:
"""Sets acro form parameters."""

super().__init__(name, page_number, x, y, **kwargs)
self.acro_form_params["wkind"] = "choice"
self.acro_form_params["value"] = self.acro_form_params["options"][0]
5 changes: 4 additions & 1 deletion PyPDFForm/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
remove_all_widgets)
from .watermark import create_watermarks_and_draw, merge_watermarks_with_pdf
from .widgets.checkbox import CheckBoxWidget
from .widgets.dropdown import DropdownWidget
from .widgets.text import TextWidget


Expand Down Expand Up @@ -218,6 +219,8 @@ def create_widget(
_class = TextWidget
if widget_type == "checkbox":
_class = CheckBoxWidget
if widget_type == "dropdown":
_class = DropdownWidget
if _class is None:
return self

Expand All @@ -231,7 +234,7 @@ def create_widget(
if k in new_widgets:
new_widgets[k] = v
self.widgets = new_widgets
if widget_type == "text":
if widget_type in ("text", "dropdown"):
self.widgets[name].font = self.global_font
self.widgets[name].font_size = self.global_font_size
self.widgets[name].font_color = self.global_font_color
Expand Down
30 changes: 30 additions & 0 deletions docs/prepare.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,33 @@ with open("output.pdf", "wb+") as output:
```

The `button_style` parameter currently supports three options: `check`, `circle`, and `cross`.

## Create a dropdown widget

A dropdown widget shares a similar set of parameters as a text field, with the only significant difference being
a list of `options` needs to be specified:

```python
from PyPDFForm import PdfWrapper

new_form = PdfWrapper("dummy.pdf").create_widget(
widget_type="dropdown",
name="new_dropdown_widget",
page_number=1,
x=57,
y=700,
options=[
"foo",
"bar",
"foobar",
],
width=120,
height=40,
font="Courier",
font_size=15,
font_color=(1, 0, 0)
)

with open("output.pdf", "wb+") as output:
output.write(new_form.read())
```
Binary file added pdf_samples/widget/create_dropdown.pdf
Binary file not shown.
31 changes: 31 additions & 0 deletions tests/test_create_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,34 @@ def test_create_widget_sejda_schema(sejda_template):

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


def test_create_dropdown(template_stream, pdf_samples, request):
expected_path = os.path.join(pdf_samples, "widget", "create_dropdown.pdf")
with open(expected_path, "rb+") as f:
obj = PdfWrapper(template_stream).create_widget(
widget_type="dropdown",
name="new_dropdown_widget",
page_number=1,
x=57,
y=700,
options=[
"foo",
"bar",
"foobar",
],
width=120,
height=40,
font="Courier",
font_size=15,
font_color=(1, 0, 0)
)
assert obj.schema["properties"]["new_dropdown_widget"]["type"] == "integer"

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 d757040

Please sign in to comment.