-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #450 from chinapandaman/PPF-449
PPF-449: implement create widget method
- Loading branch information
Showing
16 changed files
with
357 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
[MESSAGES CONTROL] | ||
disable=C0103, R0913, R0902, R0914, C0209 | ||
disable=C0103, R0913, R0902, R0903, R0914, C0209 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Contains base class for all widgets to create.""" | ||
|
||
from io import BytesIO | ||
from typing import List | ||
|
||
from pypdf import PdfReader | ||
from reportlab.lib.colors import Color | ||
from reportlab.pdfgen.canvas import Canvas | ||
|
||
from ..core.utils import stream_to_io | ||
|
||
|
||
class Widget: | ||
"""Base class for all widgets to create.""" | ||
|
||
USER_PARAMS = [] | ||
COLOR_PARAMS = [] | ||
NONE_DEFAULTS = [] | ||
ACRO_FORM_FUNC = "" | ||
|
||
def __init__( | ||
self, | ||
name: str, | ||
page_number: int, | ||
x: float, | ||
y: float, | ||
**kwargs, | ||
) -> None: | ||
"""Sets acro form parameters.""" | ||
|
||
self.page_number = page_number | ||
self.acro_form_params = { | ||
"name": name, | ||
"x": x, | ||
"y": y, | ||
} | ||
|
||
for each in self.USER_PARAMS: | ||
user_input, param = each | ||
if user_input in kwargs: | ||
value = kwargs[user_input] | ||
if user_input in self.COLOR_PARAMS: | ||
value = Color( | ||
value[0], | ||
value[1], | ||
value[2], | ||
) | ||
self.acro_form_params[param] = value | ||
elif user_input in self.NONE_DEFAULTS: | ||
self.acro_form_params[param] = None | ||
|
||
def watermarks(self, stream: bytes) -> List[bytes]: | ||
"""Returns a list of watermarks after creating the widget.""" | ||
|
||
pdf = PdfReader(stream_to_io(stream)) | ||
page_count = len(pdf.pages) | ||
watermark = BytesIO() | ||
|
||
canvas = Canvas( | ||
watermark, | ||
pagesize=( | ||
float(pdf.pages[self.page_number - 1].mediabox[2]), | ||
float(pdf.pages[self.page_number - 1].mediabox[3]), | ||
), | ||
) | ||
|
||
getattr(canvas.acroForm, self.ACRO_FORM_FUNC)(**self.acro_form_params) | ||
|
||
canvas.showPage() | ||
canvas.save() | ||
watermark.seek(0) | ||
|
||
result = [] | ||
for i in range(page_count): | ||
result.append(watermark.read() if i == self.page_number - 1 else b"") | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Contains checkbox widget to create.""" | ||
|
||
from .base import Widget | ||
|
||
|
||
class CheckBoxWidget(Widget): | ||
"""Checkbox widget to create.""" | ||
|
||
USER_PARAMS = [("size", "size")] | ||
ACRO_FORM_FUNC = "checkbox" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Contains text field widget to create.""" | ||
|
||
from .base import Widget | ||
|
||
|
||
class TextWidget(Widget): | ||
"""Text field widget to create.""" | ||
|
||
USER_PARAMS = [("width", "width"), ("height", "height"), | ||
("max_length", "maxlen"), ("font", "fontName"), | ||
("font_size", "fontSize"), ("font_color", "textColor")] | ||
COLOR_PARAMS = ["font_color"] | ||
NONE_DEFAULTS = ["max_length"] | ||
ACRO_FORM_FUNC = "textfield" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.