9
9
Adobe-specific settings for improved rendering in Adobe Reader.
10
10
"""
11
11
12
- from functools import lru_cache
13
12
from io import BytesIO
14
13
from typing import Dict , Union , cast
15
14
16
15
from pypdf import PdfReader , PdfWriter
17
- from pypdf .generic import BooleanObject , DictionaryObject , NameObject
16
+ from pypdf .generic import DictionaryObject
18
17
19
- from .constants import WIDGET_TYPES , AcroForm , Annots , NeedAppearances , Root
18
+ from .constants import WIDGET_TYPES , Annots
20
19
from .image import get_draw_image_resolutions , get_image_dimensions
21
20
from .middleware .checkbox import Checkbox
22
21
from .middleware .dropdown import Dropdown
23
22
from .middleware .image import Image
24
23
from .middleware .radio import Radio
25
24
from .middleware .signature import Signature
26
25
from .middleware .text import Text
27
- from .patterns import (simple_flatten_generic , simple_flatten_radio ,
28
- simple_update_checkbox_value ,
29
- simple_update_dropdown_value , simple_update_radio_value ,
30
- simple_update_text_value )
26
+ from .patterns import (flatten_generic , flatten_radio , update_checkbox_value ,
27
+ update_dropdown_value , update_radio_value ,
28
+ update_text_value )
31
29
from .template import get_widget_key
32
30
from .utils import stream_to_io
33
31
from .watermark import create_watermarks_and_draw , merge_watermarks_with_pdf
@@ -101,42 +99,7 @@ def get_drawn_stream(to_draw: dict, stream: bytes, action: str) -> bytes:
101
99
return merge_watermarks_with_pdf (stream , watermark_list )
102
100
103
101
104
- @lru_cache
105
- def enable_adobe_mode (pdf : bytes ) -> bytes :
106
- """Enables Adobe-specific settings in the PDF to ensure proper rendering of form fields.
107
-
108
- This function modifies the PDF's AcroForm dictionary to include the `NeedAppearances` flag,
109
- which forces Adobe Reader to generate appearance streams for form fields. This ensures that
110
- the form fields are rendered correctly in Adobe Reader, especially when the form is filled
111
- programmatically.
112
-
113
- Args:
114
- pdf (bytes): The PDF content as bytes.
115
-
116
- Returns:
117
- bytes: The modified PDF content with Adobe mode enabled.
118
- """
119
- reader = PdfReader (stream_to_io (pdf ))
120
- writer = PdfWriter ()
121
-
122
- # https://stackoverflow.com/questions/47288578/pdf-form-filled-with-pypdf2-does-not-show-in-print
123
- if AcroForm in reader .trailer [Root ]:
124
- if NeedAppearances in reader .trailer [Root ][AcroForm ]:
125
- return pdf
126
- else :
127
- reader .trailer [Root ].update ({NameObject (AcroForm ): DictionaryObject ()})
128
- reader .trailer [Root ][AcroForm ].update (
129
- {NameObject (NeedAppearances ): BooleanObject (True )}
130
- )
131
- writer .append (reader )
132
-
133
- with BytesIO () as f :
134
- writer .write (f )
135
- f .seek (0 )
136
- return f .read ()
137
-
138
-
139
- def simple_fill (
102
+ def fill (
140
103
template : bytes ,
141
104
widgets : Dict [str , WIDGET_TYPES ],
142
105
use_full_widget_name : bool ,
@@ -185,9 +148,9 @@ def simple_fill(
185
148
# flatten all
186
149
if flatten :
187
150
if isinstance (widget , Radio ):
188
- simple_flatten_radio (annot )
151
+ flatten_radio (annot )
189
152
else :
190
- simple_flatten_generic (annot )
153
+ flatten_generic (annot )
191
154
if widget .value is None :
192
155
continue
193
156
@@ -196,17 +159,17 @@ def simple_fill(
196
159
annot , widgets [key ], images_to_draw [page_num + 1 ]
197
160
)
198
161
elif type (widget ) is Checkbox :
199
- simple_update_checkbox_value (annot , widget .value )
162
+ update_checkbox_value (annot , widget .value )
200
163
elif isinstance (widget , Radio ):
201
164
if key not in radio_button_tracker :
202
165
radio_button_tracker [key ] = 0
203
166
radio_button_tracker [key ] += 1
204
167
if widget .value == radio_button_tracker [key ] - 1 :
205
- simple_update_radio_value (annot )
168
+ update_radio_value (annot )
206
169
elif isinstance (widget , Dropdown ):
207
- simple_update_dropdown_value (annot , widget )
170
+ update_dropdown_value (annot , widget )
208
171
elif isinstance (widget , Text ):
209
- simple_update_text_value (annot , widget )
172
+ update_text_value (annot , widget )
210
173
211
174
with BytesIO () as f :
212
175
out .write (f )
0 commit comments