py-pdf / pypdf

A pure-python PDF library capable of splitting, merging, cropping, and transforming the pages of PDF files
https://pypdf.readthedocs.io/en/latest/
Other
8.36k stars 1.41k forks source link

Error when filling a value with parentheses #2268

Closed KanorUbu closed 1 year ago

KanorUbu commented 1 year ago

Using parenthesis causes "content stream" to be displayed

Environment

$ python -m platform
Linux-6.2.0-34-generic-x86_64-with-glibc2.35

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==3.16.4, crypt_provider=('cryptography', '41.0.1'), PIL=9.3.0

Code + PDF

from pypdf import PdfReader, PdfWriter
pdf_reader = PdfReader('test_pdf_form.pdf')
pdf_writer = PdfWriter()
pdf_writer.clone_document_from_reader(pdf_reader)
pdf_writer.update_page_form_field_values(pdf_writer.pages[0], {'test_multifield': 'Line1 (test\n brace)Line2'},  auto_regenerate=False)
pdf_writer.write('/tmp/test.pdf')

Example pdf test_pdf_form.pdf

Result

Capture d’écran du 2023-10-25 11-53-39

Solution

Add a escape on parentheses in update_page_form_field_values or _update_text_field

stefan6419846 commented 1 year ago

Do you want to submit a corresponding PR?

KanorUbu commented 1 year ago

Yes, I have just proposed a PR

MartinThoma commented 1 year ago

Fixed in #2269

@KanorUbu In future, please add Fixes #2269 or Closes #2269 to the text of the PR. Then the issue gets automatically linked / closed once the PR is merged :-)

KanorUbu commented 1 year ago

@MartinThoma I'll think about doing it next time