Anvil.pdf - Add Page Numbers

It would be great to have a feature that adds page numbers to anvil.pdf

I agree.

But I also doubt this feature request will ever be addressed.

The pdf is not generated by Anvil code. Anvil simply asks the browser (one on the server, not yours) to render the pdf with the content of a form. So, the management of headers, footers, page numbers etc., it’s either done by the browser or it doesn’t just show up automagically because Anvil says so.

They could create a library specialized in creating pdf files, but those libraries already exist and you are already free to use them with Anvil.

Here is one attempt at managing footers that I have done, with the documentation of when it doesn’t work: Printing to PDF with good header and footer

1 Like

Hi Stefano,

thank you for the input and explanation.
You example is great :slight_smile:
I looked into the code before posting!
However I faced your mentioned limits.

I will stick with a safe semi-auto solution in order to create a 30 page PDF report:
self.panel_pdf.add_component(PDF_Cover())
self.panel_pdf.add_component(PDF_Header())
self.panel_pdf.add_component(FormPlot_Example())
self.panel_pdf.add_component(PDF_Header())
… and so on … the page break is included in each Header Form

I’m glad it helps.

I hope you don’t get the second problem with PDF generation: by adding many pages, you may end up hitting the 30 second timeout. Even when the PDF is generated by a background task, which itself has no time limit, the PDF generator call still has the 30 second limit, which makes it impossible to generate large PDF files.

Unlike the page footers, this time limit is managed by Anvil. There are good reasons for having it, but that one could be changed.

Hi Stefano,

yes, I will sure end up by hitting the timeout with 30 pages.
I do save the pdf in the table via background task and then calling the pdf in the client code.
I will check if this does the job or of if need an extended time.

Appreciate the solution displayed PDFRenderer(timeout=56, …) and remarks :slight_smile:

BR
Gerhard

If you do run into the timeout, a while ago I built a brute-force solution, but it’s not for the faint of heart. I tried to make it as simple as possible but still…

As long as each page can (at least most of the time) be rendered in 30 seconds, it will build each one and string them together, but you would have to split the forms into pages your self.

It also uses unfinished (at the time) features of the the data files service, that I had to hack together, so ymmv.