PyPDFForm 2.0.1__py3-none-any.whl → 2.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of PyPDFForm might be problematic. Click here for more details.
- PyPDFForm/__init__.py +6 -2
- PyPDFForm/adapter.py +37 -3
- PyPDFForm/constants.py +12 -1
- PyPDFForm/coordinate.py +218 -59
- PyPDFForm/filler.py +104 -9
- PyPDFForm/font.py +80 -16
- PyPDFForm/image.py +32 -3
- PyPDFForm/middleware/base.py +57 -8
- PyPDFForm/middleware/checkbox.py +49 -7
- PyPDFForm/middleware/dropdown.py +41 -5
- PyPDFForm/middleware/image.py +26 -2
- PyPDFForm/middleware/radio.py +41 -5
- PyPDFForm/middleware/signature.py +49 -6
- PyPDFForm/middleware/text.py +55 -7
- PyPDFForm/patterns.py +108 -10
- PyPDFForm/template.py +181 -29
- PyPDFForm/utils.py +108 -12
- PyPDFForm/watermark.py +163 -11
- PyPDFForm/widgets/base.py +65 -9
- PyPDFForm/widgets/bedrock.py +3 -0
- PyPDFForm/widgets/checkbox.py +22 -2
- PyPDFForm/widgets/dropdown.py +31 -3
- PyPDFForm/widgets/image.py +24 -0
- PyPDFForm/widgets/radio.py +78 -0
- PyPDFForm/widgets/signature.py +133 -0
- PyPDFForm/widgets/text.py +19 -2
- PyPDFForm/wrapper.py +351 -27
- {pypdfform-2.0.1.dist-info → pypdfform-2.2.0.dist-info}/METADATA +2 -2
- pypdfform-2.2.0.dist-info/RECORD +34 -0
- pypdfform-2.0.1.dist-info/RECORD +0 -30
- {pypdfform-2.0.1.dist-info → pypdfform-2.2.0.dist-info}/WHEEL +0 -0
- {pypdfform-2.0.1.dist-info → pypdfform-2.2.0.dist-info}/licenses/LICENSE +0 -0
- {pypdfform-2.0.1.dist-info → pypdfform-2.2.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
This module provides the SignatureWidget class for creating and customizing
|
|
4
|
+
signature fields in PDF forms. It enables placement of signature widgets
|
|
5
|
+
on specified pages and coordinates, and generates watermark overlays for
|
|
6
|
+
PDF documents to visually represent signature fields.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from io import BytesIO
|
|
10
|
+
from typing import List
|
|
11
|
+
|
|
12
|
+
from pypdf import PdfReader, PdfWriter
|
|
13
|
+
from pypdf.generic import (ArrayObject, FloatObject, NameObject,
|
|
14
|
+
TextStringObject)
|
|
15
|
+
|
|
16
|
+
from ..constants import Annots, Rect, T
|
|
17
|
+
from ..patterns import WIDGET_KEY_PATTERNS
|
|
18
|
+
from ..utils import extract_widget_property, stream_to_io
|
|
19
|
+
from .bedrock import BEDROCK_PDF
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class SignatureWidget:
|
|
23
|
+
"""
|
|
24
|
+
A widget for adding a digital signature field to a PDF form.
|
|
25
|
+
|
|
26
|
+
This class allows you to specify the name, page number, position (x, y),
|
|
27
|
+
and size (width, height) of a signature field to be placed on a PDF page.
|
|
28
|
+
The widget is based on a bedrock template and can generate a watermark
|
|
29
|
+
overlay for the specified page, updating the annotation's name and rectangle
|
|
30
|
+
to match the provided parameters.
|
|
31
|
+
|
|
32
|
+
Attributes:
|
|
33
|
+
BEDROCK_WIDGET_TO_COPY (str): The widget type to copy from the bedrock template.
|
|
34
|
+
name (str): The name of the signature field.
|
|
35
|
+
page_number (int): The 1-based page number where the signature field will appear.
|
|
36
|
+
x (float): The x-coordinate of the lower-left corner of the signature field.
|
|
37
|
+
y (float): The y-coordinate of the lower-left corner of the signature field.
|
|
38
|
+
optional_params (dict): Optional parameters for widget customization (e.g., width, height).
|
|
39
|
+
|
|
40
|
+
Methods:
|
|
41
|
+
watermarks(stream: bytes) -> List[bytes]:
|
|
42
|
+
Generates a list of watermark overlays, with the signature field
|
|
43
|
+
applied to the specified page.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
OPTIONAL_PARAMS = [
|
|
47
|
+
("width", 160),
|
|
48
|
+
("height", 90),
|
|
49
|
+
]
|
|
50
|
+
BEDROCK_WIDGET_TO_COPY = "signature"
|
|
51
|
+
|
|
52
|
+
def __init__(
|
|
53
|
+
self,
|
|
54
|
+
name: str,
|
|
55
|
+
page_number: int,
|
|
56
|
+
x: float,
|
|
57
|
+
y: float,
|
|
58
|
+
**kwargs,
|
|
59
|
+
) -> None:
|
|
60
|
+
"""
|
|
61
|
+
Initialize a SignatureWidget.
|
|
62
|
+
|
|
63
|
+
Args:
|
|
64
|
+
name (str): The name of the signature widget.
|
|
65
|
+
page_number (int): The page number where the widget will be placed (1-based).
|
|
66
|
+
x (float): The x-coordinate of the widget's lower-left corner.
|
|
67
|
+
y (float): The y-coordinate of the widget's lower-left corner.
|
|
68
|
+
width (float, optional): The width of the widget. Defaults to 160 if not provided.
|
|
69
|
+
height (float, optional): The height of the widget. Defaults to 90 if not provided.
|
|
70
|
+
**kwargs: Additional optional parameters for widget customization.
|
|
71
|
+
"""
|
|
72
|
+
|
|
73
|
+
super().__init__()
|
|
74
|
+
self.non_acro_form_params = []
|
|
75
|
+
|
|
76
|
+
self.page_number = page_number
|
|
77
|
+
self.name = name
|
|
78
|
+
self.x = x
|
|
79
|
+
self.y = y
|
|
80
|
+
self.optional_params = {
|
|
81
|
+
each[0]: kwargs.get(each[0], each[1]) for each in self.OPTIONAL_PARAMS
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
def watermarks(self, stream: bytes) -> List[bytes]:
|
|
85
|
+
"""
|
|
86
|
+
Generate watermark overlays for the signature widget on the specified PDF page.
|
|
87
|
+
|
|
88
|
+
This method creates a list of watermark overlays for each page in the input PDF.
|
|
89
|
+
Only the page corresponding to `page_number` contains the signature widget overlay,
|
|
90
|
+
which is generated by copying and customizing a signature annotation from a bedrock
|
|
91
|
+
template PDF. All other pages in the returned list are empty byte strings.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
stream (bytes): The PDF file as a byte stream.
|
|
95
|
+
|
|
96
|
+
Returns:
|
|
97
|
+
List[bytes]: A list of byte streams, one for each page in the input PDF.
|
|
98
|
+
Only the page corresponding to `page_number` contains the
|
|
99
|
+
signature widget overlay; other pages are empty byte strings.
|
|
100
|
+
"""
|
|
101
|
+
|
|
102
|
+
input_pdf = PdfReader(stream_to_io(stream))
|
|
103
|
+
page_count = len(input_pdf.pages)
|
|
104
|
+
pdf = PdfReader(stream_to_io(BEDROCK_PDF))
|
|
105
|
+
out = PdfWriter()
|
|
106
|
+
out.append(pdf)
|
|
107
|
+
|
|
108
|
+
for page in out.pages:
|
|
109
|
+
for annot in page.get(Annots, []): # noqa
|
|
110
|
+
key = extract_widget_property(
|
|
111
|
+
annot.get_object(), WIDGET_KEY_PATTERNS, None, str
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
if key != self.BEDROCK_WIDGET_TO_COPY:
|
|
115
|
+
continue
|
|
116
|
+
|
|
117
|
+
annot.get_object()[NameObject(T)] = TextStringObject(self.name)
|
|
118
|
+
annot.get_object()[NameObject(Rect)] = ArrayObject(
|
|
119
|
+
[
|
|
120
|
+
FloatObject(self.x),
|
|
121
|
+
FloatObject(self.y),
|
|
122
|
+
FloatObject(self.x + self.optional_params.get("width")),
|
|
123
|
+
FloatObject(self.y + self.optional_params.get("height")),
|
|
124
|
+
]
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
with BytesIO() as f:
|
|
128
|
+
out.write(f)
|
|
129
|
+
f.seek(0)
|
|
130
|
+
return [
|
|
131
|
+
f.read() if i == self.page_number - 1 else b""
|
|
132
|
+
for i in range(page_count)
|
|
133
|
+
]
|
PyPDFForm/widgets/text.py
CHANGED
|
@@ -1,11 +1,28 @@
|
|
|
1
1
|
# -*- coding: utf-8 -*-
|
|
2
|
-
"""
|
|
2
|
+
"""Provides text field widget creation functionality for PDF forms.
|
|
3
|
+
|
|
4
|
+
This module contains the TextWidget class which handles creation of:
|
|
5
|
+
- Standard text input fields
|
|
6
|
+
- Multiline text fields
|
|
7
|
+
- Font and color styling
|
|
8
|
+
- Field size and length constraints
|
|
9
|
+
"""
|
|
3
10
|
|
|
4
11
|
from .base import Widget
|
|
5
12
|
|
|
6
13
|
|
|
7
14
|
class TextWidget(Widget):
|
|
8
|
-
"""
|
|
15
|
+
"""Creates and configures PDF text field widgets.
|
|
16
|
+
|
|
17
|
+
Supports all standard text field properties including:
|
|
18
|
+
- Font styling (family, size, color)
|
|
19
|
+
- Background and border colors
|
|
20
|
+
- Width/height dimensions
|
|
21
|
+
- Maximum length constraints
|
|
22
|
+
- Alignment and multiline options
|
|
23
|
+
|
|
24
|
+
Inherits from Widget base class adding text-specific parameters.
|
|
25
|
+
"""
|
|
9
26
|
|
|
10
27
|
USER_PARAMS = [
|
|
11
28
|
("width", "width"),
|