I'm trying to create an invoice with the python reportlab library in a Django view.
But now my question:
How can I write/draw something at the bottom (that's important!) of the last page? (Which may also be the first one, depending on how long the table is.)
My idea was to just get the last page's canvas and draw the footer on it (I added a Spacer so I can be sure that there's enough space for it. This would work, but I can't get the last page's canvas.)
buffer = BytesIO()
doc = SimpleDocTemplate(buffer, rightMargin=10*mm, leftMargin=10*mm, topMargin=10*mm, bottomMargin=0*mm)
elements = [Spacer(1,75*mm),get_table(),Spacer(1,108*mm)]
# get_table() returns a Table object
doc.build(elements, onFirstPage=draw_header)
# draw_header draws the header on the canvas
draw_invoice(CANVAS) # here's my problem
buffer.seek(0)
return FileResponse(buffer, as_attachment=False, filename='invoice.pdf')
Is there a way to get the canvas of the last page after building the doc and modify it? Or is there an other way to solve my problem?
Here's a scratch of what I'm trying to do: (The table could also go over more than 2 pages, the footer just has to be on the bottom of the earliest possible page, but after the table.)
I was able to find my own solution:
I created my own Flowable
and added it with a TopPadder
to the other Flowables
. I had to put a Spacer
between because otherways my Flowable
was sometimes overlapping with the Table
.