Home > Enterprise >  Python: How to append a FPDF2 table to an existing pdf page?
Python: How to append a FPDF2 table to an existing pdf page?

Time:10-04

I have an FPDF2 table created using this script. I used to output it to a blank page and merge it to an existing pdf, which works fine.

But now we need to add the table to an existing page in the pdf and then if it doesn't fit, we insert new pages. And that's the problem.

FPDF doesn't seem to be able to draw to an existing page. I know I can use reportlab canvas can.drawString() to draw to an existing page, but I don't know if reportlab can draw an FPDF object.

Also, if I were to ditch FPDF and use only reportlab to draw a table, I don't know how to detect the end of the page and insert a new page if needed. I'm not starting at the start of a page, I'll be starting somewhere in the middle.

I would prefer to be able to use the FPDF2 script I already have and somehow add the output at a specific x,y position in a page though, if possible. Have you ever had this issue?

I also have Pypdf2 installed and used in the same project, but I think that only reportlab can do the job. Maybe I need to detect the end of the page via Pypdf2 and write to the page via reportlab?

CodePudding user response:

Since no answer exists as of now and since I need an answer to finish my task, I did the following:

I added the FPDF table to a blank PDF page, and pushed it down, by using set_y(100), to have a half blank page to work with.

And I took a screenshot of the items which need to be placed above the table and then added them to the same page by using reportlab canvas.drawImage()

If there's a better solution, please post an answer and I'll accept it and refactor my code. For now, I'll accept my answer to close this question.

  • Related