I am trying to automatically fill 2-pages long pdf file with personal data from CSV file for my clients. I'm using Python, Reportlab and pdfrw package.
I have already managed to successfully fill the first page of a pdf, but I cannot populate the second page. Below is my code (taken from https://medium.com/@zwinny/filling-pdf-forms-in-python-the-right-way-eb9592e03dba, thanks) and modified a little. I must admit that I do not understand the code completely, but I think that the problem lies in the merge function. And probably also in the get_overlay_canvas function. Just guessing.
def get_overlay_canvas(name) -> io.BytesIO:
data = io.BytesIO()
pdf = canvas.Canvas(data, pagesize=A4)
pdf.setFont('arial', 12)
pdf.drawString(x=484, y=690, text='2018')
pdf.drawString(x=70, y=504, text=name)
pdf.line(193, 413, 205, 432) # izrišemo linijo 1
pdf.line(205, 413, 193, 432) # izrišemo linijo 2
pdf.save()
data.seek(0)
return data
def merge(overlay_canvas: io.BytesIO, template_path: str) -> io.BytesIO:
template_pdf = pdfrw.PdfReader(template_path)
overlay_pdf = pdfrw.PdfReader(overlay_canvas)
for page, data in zip(template_pdf.pages, overlay_pdf.pages):
overlay = pdfrw.PageMerge().add(data)[0]
pdfrw.PageMerge(page).add(overlay).render()
form = io.BytesIO()
pdfrw.PdfWriter().write(form, template_pdf)
form.seek(0)
return form
def save(form: io.BytesIO, filename: str):
with open(filename, 'wb') as f:
f.write(form.read())
import_data(data_file)
So my question is simple: how can I populate the second page with different data? I have tried almost everything that comes to my mind with no success. Thanks in advance, David