Skip to content

Commit 33cc526

Browse files
authored
added encoding ability
Added encoding ability to update_page_form_field_values so functionality uses that encoding to populate from fields. Fixes issue that i had: py-pdf#2035
1 parent 534c7b4 commit 33cc526

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

pypdf/_writer.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ def _get_qualified_field_name(self, parent: DictionaryObject) -> Optional[str]:
831831
return qualified_parent + "." + cast(str, parent["/T"])
832832
return cast(str, parent["/T"])
833833

834-
def _update_text_field(self, field: DictionaryObject) -> None:
834+
def _update_text_field(self, field: DictionaryObject, encoding: Optional[str] = None) -> None:
835835
# Calculate rectangle dimensions
836836
_rct = cast(RectangleObject, field[AA.Rect])
837837
rct = RectangleObject((0, 0, _rct[2] - _rct[0], _rct[3] - _rct[1]))
@@ -859,21 +859,22 @@ def _update_text_field(self, field: DictionaryObject) -> None:
859859
sel = []
860860

861861
# Generate appearance stream
862-
ap_stream = f"q\n/Tx BMC \nq\n1 1 {rct.width - 1} {rct.height - 1} re\nW\nBT\n{da}\n".encode()
862+
ap_stream = f"q\n/Tx BMC \nq\n1 1 {rct.width - 1} {rct.height - 1} re\nW\nBT\n{da}\n"
863863
for line_number, line in enumerate(txt.replace("\n", "\r").split("\r")):
864864
if line in sel:
865865
# may be improved but can not find how get fill working => replaced with lined box
866866
ap_stream += (
867867
f"1 {y_offset - (line_number * font_height * 1.4) - 1} {rct.width - 2} {font_height + 2} re\n"
868868
f"0.5 0.5 0.5 rg s\n{field[AA.DA]}\n"
869-
).encode()
869+
)
870870
if line_number == 0:
871-
ap_stream += f"2 {y_offset} Td\n".encode()
871+
ap_stream += f"2 {y_offset} Td\n"
872872
else:
873873
# Td is a relative translation
874-
ap_stream += f"0 {- font_height * 1.4} Td\n".encode()
875-
ap_stream += b"(" + str(line).encode("UTF-8") + b") Tj\n"
876-
ap_stream += b"ET\nQ\nEMC\nQ\n"
874+
ap_stream += f"0 {- font_height * 1.4} Td\n"
875+
ap_stream += "(" + str(line) + ") Tj\n"
876+
ap_stream += "ET\nQ\nEMC\nQ\n"
877+
ap_stream = ap_stream.encode(encoding) if encoding is not None else ap_stream.encode()
877878

878879
# Create appearance dictionary
879880
dct = DecodedStreamObject.initialize_from_dictionary(
@@ -924,6 +925,7 @@ def update_page_form_field_values(
924925
fields: Dict[str, Any],
925926
flags: FieldFlag = OPTIONAL_READ_WRITE_FIELD,
926927
auto_regenerate: Optional[bool] = True,
928+
encoding: Optional[str] = None
927929
) -> None:
928930
"""
929931
Update the form field values for a given page from a fields dictionary.
@@ -992,7 +994,7 @@ def update_page_form_field_values(
992994
if AA.DA in f:
993995
da = f[AA.DA]
994996
writer_annot[NameObject(AA.DA)] = da
995-
self._update_text_field(writer_annot)
997+
self._update_text_field(writer_annot, encoding)
996998
elif writer_annot.get(FA.FT) == "/Sig":
997999
# signature
9981000
logger_warning("Signature forms not implemented yet", __name__)

0 commit comments

Comments
 (0)