I've been trying for hours to insert a multiline text into a PDF with Python but none of the potential solutions worked. I've looked at multiple libraries and other solutions but none of them were using PdfWriter or helped solve my problem Here are a few of my tries:
The pdf can be found here
Here is my code:
from pypdf import PdfReader, PdfWriter
file_path = os.path.join(script_directory, 'folder/', 'sample.pdf')
reader = PdfReader(file_path)
page_0 = reader.pages[0]
writer = PdfWriter()
writer.add_page(page_0)
name = current_user.name
address = session['address']
writer.update_page_form_field_values(writer.pages[0], {"1": f'{name}\r\n{address}'})
with open(os.path.join(script_directory, 'folder/', 'sample_filled_out.pdf'), "wb") as output_stream:
writer.write(output_stream)
Any help or guidance would be appreciated, thank you!
There's no straightforward way to have the library write multiline variables, so the better solution was to separate the two fields into unique fields with different id's and then individually access each like so:
writer.update_page_form_field_values(writer.pages[0], {"1": name})
writer.update_page_form_field_values(writer.pages[0], {"2": address})
Thanks for everyone who commented on the post trying to help.