PyPDFForm 1.4.25__py3-none-any.whl → 1.4.27__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 +1 -1
- PyPDFForm/constants.py +5 -0
- PyPDFForm/filler.py +14 -2
- PyPDFForm/font.py +38 -2
- PyPDFForm/template.py +10 -20
- PyPDFForm/wrapper.py +4 -1
- {PyPDFForm-1.4.25.dist-info → PyPDFForm-1.4.27.dist-info}/METADATA +1 -1
- {PyPDFForm-1.4.25.dist-info → PyPDFForm-1.4.27.dist-info}/RECORD +11 -11
- {PyPDFForm-1.4.25.dist-info → PyPDFForm-1.4.27.dist-info}/LICENSE +0 -0
- {PyPDFForm-1.4.25.dist-info → PyPDFForm-1.4.27.dist-info}/WHEEL +0 -0
- {PyPDFForm-1.4.25.dist-info → PyPDFForm-1.4.27.dist-info}/top_level.txt +0 -0
PyPDFForm/__init__.py
CHANGED
PyPDFForm/constants.py
CHANGED
PyPDFForm/filler.py
CHANGED
|
@@ -5,9 +5,9 @@ from io import BytesIO
|
|
|
5
5
|
from typing import Dict, Tuple, Union, cast
|
|
6
6
|
|
|
7
7
|
from pypdf import PdfReader, PdfWriter
|
|
8
|
-
from pypdf.generic import DictionaryObject
|
|
8
|
+
from pypdf.generic import BooleanObject, DictionaryObject, NameObject
|
|
9
9
|
|
|
10
|
-
from .constants import WIDGET_TYPES, Annots
|
|
10
|
+
from .constants import WIDGET_TYPES, AcroForm, Annots, NeedAppearances, Root
|
|
11
11
|
from .coordinate import (get_draw_checkbox_radio_coordinates,
|
|
12
12
|
get_draw_image_coordinates_resolutions,
|
|
13
13
|
get_draw_text_coordinates,
|
|
@@ -160,14 +160,26 @@ def fill(
|
|
|
160
160
|
return result
|
|
161
161
|
|
|
162
162
|
|
|
163
|
+
def enable_adobe_mode(pdf: PdfReader, adobe_mode: bool) -> None:
|
|
164
|
+
"""Enables Adobe mode so that texts filled can show up in Acrobat."""
|
|
165
|
+
|
|
166
|
+
if adobe_mode and AcroForm in pdf.trailer[Root]:
|
|
167
|
+
pdf.trailer[Root][AcroForm].update(
|
|
168
|
+
{NameObject(NeedAppearances): BooleanObject(True)}
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
|
|
163
172
|
def simple_fill(
|
|
164
173
|
template: bytes,
|
|
165
174
|
widgets: Dict[str, WIDGET_TYPES],
|
|
166
175
|
flatten: bool = False,
|
|
176
|
+
adobe_mode: bool = False,
|
|
167
177
|
) -> bytes:
|
|
168
178
|
"""Fills a PDF form in place."""
|
|
169
179
|
|
|
180
|
+
# pylint: disable=too-many-branches
|
|
170
181
|
pdf = PdfReader(stream_to_io(template))
|
|
182
|
+
enable_adobe_mode(pdf, adobe_mode)
|
|
171
183
|
out = PdfWriter()
|
|
172
184
|
out.append(pdf)
|
|
173
185
|
|
PyPDFForm/font.py
CHANGED
|
@@ -7,11 +7,14 @@ from re import findall
|
|
|
7
7
|
from typing import Tuple, Union
|
|
8
8
|
|
|
9
9
|
from reportlab.pdfbase.acroform import AcroForm
|
|
10
|
-
from reportlab.pdfbase.pdfmetrics import registerFont, standardFonts
|
|
10
|
+
from reportlab.pdfbase.pdfmetrics import (registerFont, standardFonts,
|
|
11
|
+
stringWidth)
|
|
11
12
|
from reportlab.pdfbase.ttfonts import TTFError, TTFont
|
|
12
13
|
|
|
13
14
|
from .constants import (DEFAULT_FONT, FONT_COLOR_IDENTIFIER,
|
|
14
|
-
FONT_SIZE_IDENTIFIER,
|
|
15
|
+
FONT_SIZE_IDENTIFIER, FONT_SIZE_REDUCE_STEP,
|
|
16
|
+
MARGIN_BETWEEN_LINES, Rect)
|
|
17
|
+
from .middleware.text import Text
|
|
15
18
|
from .patterns import TEXT_FIELD_APPEARANCE_PATTERNS
|
|
16
19
|
from .utils import traverse_pattern
|
|
17
20
|
|
|
@@ -146,3 +149,36 @@ def get_text_field_font_color(
|
|
|
146
149
|
break
|
|
147
150
|
|
|
148
151
|
return result
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def adjust_paragraph_font_size(widget: dict, widget_middleware: Text) -> None:
|
|
155
|
+
"""Reduces the font size of a paragraph field until texts fits."""
|
|
156
|
+
|
|
157
|
+
# pylint: disable=C0415, R0401
|
|
158
|
+
from .template import get_paragraph_lines
|
|
159
|
+
|
|
160
|
+
height = abs(float(widget[Rect][1]) - float(widget[Rect][3]))
|
|
161
|
+
|
|
162
|
+
while (
|
|
163
|
+
widget_middleware.font_size > FONT_SIZE_REDUCE_STEP
|
|
164
|
+
and len(widget_middleware.text_lines)
|
|
165
|
+
* (widget_middleware.font_size + MARGIN_BETWEEN_LINES)
|
|
166
|
+
> height
|
|
167
|
+
):
|
|
168
|
+
widget_middleware.font_size -= FONT_SIZE_REDUCE_STEP
|
|
169
|
+
widget_middleware.text_lines = get_paragraph_lines(widget, widget_middleware)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def adjust_text_field_font_size(widget: dict, widget_middleware: Text) -> None:
|
|
173
|
+
"""Reduces the font size of a text field until texts fits."""
|
|
174
|
+
|
|
175
|
+
width = abs(float(widget[Rect][0]) - float(widget[Rect][2]))
|
|
176
|
+
|
|
177
|
+
while (
|
|
178
|
+
widget_middleware.font_size > FONT_SIZE_REDUCE_STEP
|
|
179
|
+
and stringWidth(
|
|
180
|
+
widget_middleware.value, widget_middleware.font, widget_middleware.font_size
|
|
181
|
+
)
|
|
182
|
+
> width
|
|
183
|
+
):
|
|
184
|
+
widget_middleware.font_size -= FONT_SIZE_REDUCE_STEP
|
PyPDFForm/template.py
CHANGED
|
@@ -8,10 +8,10 @@ from typing import Dict, List, Tuple, Union
|
|
|
8
8
|
from pypdf import PdfReader
|
|
9
9
|
from reportlab.pdfbase.pdfmetrics import stringWidth
|
|
10
10
|
|
|
11
|
-
from .constants import (COMB, DEFAULT_FONT_SIZE,
|
|
12
|
-
MARGIN_BETWEEN_LINES, MULTILINE, NEW_LINE_SYMBOL,
|
|
11
|
+
from .constants import (COMB, DEFAULT_FONT_SIZE, MULTILINE, NEW_LINE_SYMBOL,
|
|
13
12
|
WIDGET_TYPES, MaxLen, Rect)
|
|
14
|
-
from .font import (
|
|
13
|
+
from .font import (adjust_paragraph_font_size, adjust_text_field_font_size,
|
|
14
|
+
auto_detect_font, get_text_field_font_color,
|
|
15
15
|
get_text_field_font_size, text_field_font_size)
|
|
16
16
|
from .middleware.checkbox import Checkbox
|
|
17
17
|
from .middleware.dropdown import Dropdown
|
|
@@ -134,7 +134,9 @@ def update_text_field_attributes(
|
|
|
134
134
|
if not is_paragraph
|
|
135
135
|
else DEFAULT_FONT_SIZE
|
|
136
136
|
)
|
|
137
|
-
should_adjust_font_size =
|
|
137
|
+
should_adjust_font_size = (
|
|
138
|
+
not template_font_size and widgets[key].max_length is None
|
|
139
|
+
)
|
|
138
140
|
if widgets[key].font_color is None:
|
|
139
141
|
widgets[key].font_color = get_text_field_font_color(_widget)
|
|
140
142
|
if is_paragraph and widgets[key].text_wrap_length is None:
|
|
@@ -142,8 +144,11 @@ def update_text_field_attributes(
|
|
|
142
144
|
widgets[key].text_wrap_length = get_paragraph_auto_wrap_length(
|
|
143
145
|
widgets[key]
|
|
144
146
|
)
|
|
145
|
-
|
|
147
|
+
if widgets[key].value and should_adjust_font_size:
|
|
148
|
+
if is_paragraph:
|
|
146
149
|
adjust_paragraph_font_size(_widget, widgets[key])
|
|
150
|
+
else:
|
|
151
|
+
adjust_text_field_font_size(_widget, widgets[key])
|
|
147
152
|
|
|
148
153
|
|
|
149
154
|
@lru_cache()
|
|
@@ -398,18 +403,3 @@ def get_paragraph_auto_wrap_length(widget_middleware: Text) -> int:
|
|
|
398
403
|
result = min(result, len(line))
|
|
399
404
|
|
|
400
405
|
return result
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
def adjust_paragraph_font_size(widget: dict, widget_middleware: Text) -> None:
|
|
404
|
-
"""Reduces the font size of a paragraph field until texts fits."""
|
|
405
|
-
|
|
406
|
-
height = abs(float(widget[Rect][1]) - float(widget[Rect][3]))
|
|
407
|
-
|
|
408
|
-
while (
|
|
409
|
-
widget_middleware.font_size > FONT_SIZE_REDUCE_STEP
|
|
410
|
-
and len(widget_middleware.text_lines)
|
|
411
|
-
* (widget_middleware.font_size + MARGIN_BETWEEN_LINES)
|
|
412
|
-
> height
|
|
413
|
-
):
|
|
414
|
-
widget_middleware.font_size -= FONT_SIZE_REDUCE_STEP
|
|
415
|
-
widget_middleware.text_lines = get_paragraph_lines(widget, widget_middleware)
|
PyPDFForm/wrapper.py
CHANGED
|
@@ -58,7 +58,10 @@ class FormWrapper:
|
|
|
58
58
|
widgets[key].value = value
|
|
59
59
|
|
|
60
60
|
self.stream = simple_fill(
|
|
61
|
-
self.read(),
|
|
61
|
+
self.read(),
|
|
62
|
+
widgets,
|
|
63
|
+
flatten=kwargs.get("flatten", False),
|
|
64
|
+
adobe_mode=kwargs.get("adobe_mode", False),
|
|
62
65
|
)
|
|
63
66
|
|
|
64
67
|
return self
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
PyPDFForm/__init__.py,sha256=
|
|
1
|
+
PyPDFForm/__init__.py,sha256=grkJLpy2Zjzo37fkYqaZIE7_w_Gsdbat72pwcl_KRko,149
|
|
2
2
|
PyPDFForm/adapter.py,sha256=OgAIwcmukpBqHFBLymd3I7wA7wIdipRqgURZA2Y0Hgo,885
|
|
3
|
-
PyPDFForm/constants.py,sha256=
|
|
3
|
+
PyPDFForm/constants.py,sha256=Y-WINNe5GoOPfBFQroWuzzLELhAl-Fj_d7_3b1QodW0,1767
|
|
4
4
|
PyPDFForm/coordinate.py,sha256=V_ZZQ1pXtVPAyfMJCX7fKoEimK7XwX6bU3VQ0nKOhQA,7314
|
|
5
|
-
PyPDFForm/filler.py,sha256=
|
|
6
|
-
PyPDFForm/font.py,sha256=
|
|
5
|
+
PyPDFForm/filler.py,sha256=ph2FvhfJsWGmv_NFWBq2UHJGcW1Dg-ITj7QlvB4lTP8,7526
|
|
6
|
+
PyPDFForm/font.py,sha256=DCaKN6WIfNeMpP6ud17q1FOVRQXqrdevLycIQ9kItXQ,5711
|
|
7
7
|
PyPDFForm/image.py,sha256=0GV8PFgY5oLJFHmhPD1fG-_5SBEO7jVLLtxCc_Uodfo,1143
|
|
8
8
|
PyPDFForm/patterns.py,sha256=Ylfe6s9UV2X7cRAFQjaGoEa4nVSpEn-MLrH7IROp_aY,4332
|
|
9
|
-
PyPDFForm/template.py,sha256=
|
|
9
|
+
PyPDFForm/template.py,sha256=u3QxYdsT8HhoUBlhmChYaTJeFWI-SSrhICtUYsTTrow,13141
|
|
10
10
|
PyPDFForm/utils.py,sha256=gDeRDszZ5Q59RjZjJYnikS2pQ_hc78osQH2Oy1TuLsY,4269
|
|
11
11
|
PyPDFForm/watermark.py,sha256=clYdRqCuWEE25IL-BUHnSo4HgLkHPSOl7FUJLhbAfGg,4755
|
|
12
|
-
PyPDFForm/wrapper.py,sha256=
|
|
12
|
+
PyPDFForm/wrapper.py,sha256=Q4TKCmXLmO_oP3k2UQPFYhDCmDkGocBq7cVXFBVoloA,10587
|
|
13
13
|
PyPDFForm/middleware/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
14
14
|
PyPDFForm/middleware/base.py,sha256=TmoO8XpjFa5NerAumrAGm_kUt2eFP3DyyF-Vx0uXloM,724
|
|
15
15
|
PyPDFForm/middleware/checkbox.py,sha256=Z92awlKjceMd_ryb9Fhrtyd4fLkU84Ii6fSum60rHcA,1305
|
|
@@ -23,8 +23,8 @@ PyPDFForm/widgets/base.py,sha256=Stq-oQt-cfwQ6nilQ4NqpO5udFSKDNb1mAwryrWggZA,207
|
|
|
23
23
|
PyPDFForm/widgets/checkbox.py,sha256=MGB6NGI-XMBz4j2erykgYOCd1pswvthuQ6UFowQOd4o,503
|
|
24
24
|
PyPDFForm/widgets/dropdown.py,sha256=2_R5xcLUWAL0G4raVgWWtE7TJdiWJITay-Gtl8YI2QI,705
|
|
25
25
|
PyPDFForm/widgets/text.py,sha256=x8EglZLYoI9osN9wORLRYoR7lebrpj-wx38QiRH7H1A,629
|
|
26
|
-
PyPDFForm-1.4.
|
|
27
|
-
PyPDFForm-1.4.
|
|
28
|
-
PyPDFForm-1.4.
|
|
29
|
-
PyPDFForm-1.4.
|
|
30
|
-
PyPDFForm-1.4.
|
|
26
|
+
PyPDFForm-1.4.27.dist-info/LICENSE,sha256=43awmYkI6opyTpg19me731iO1WfXZwViqb67oWtCsFY,1065
|
|
27
|
+
PyPDFForm-1.4.27.dist-info/METADATA,sha256=hcvKA9BAmZIFlVvNjFwis5Zkjqf8tl8-xV0rf6SyCVQ,5367
|
|
28
|
+
PyPDFForm-1.4.27.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
|
29
|
+
PyPDFForm-1.4.27.dist-info/top_level.txt,sha256=GQQKuWqPUjT9YZqwK95NlAQzxjwoQrsxQ8ureM8lWOY,10
|
|
30
|
+
PyPDFForm-1.4.27.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|