Exception: PDF generation failed: Components did not load within allowed time

I’m trying to generate a pdf on Anvil using a background task. Unfortunately, it takes sometimes to generate the pdf, so it raised this error:

Exception: PDF generation failed: Components did not load within allowed time.
at /downlink/anvil/pdf.py, line 17
  called from /downlink/anvil/pdf.py, line 43

I replicate the issue in this simple app:
https://anvil.works/build#clone:ZVAIWDIC3FAEIQOZ=BG5CIC4K4FIYRDQUSKGXXBNE

I’m having the same issue. Have you discovered a solution to this yet?

I was having the same error and I solved it by making sure all the data required for the rendering is collected before starting the rendering.

The form was making many server calls to collect info and was taking longer than 30 seconds.

I changed it so the background task spends time collecting data and pass it to the form, where the rendering is fast.

The pdf generation sometimes still takes longer than 30 seconds, but the form has been rendered, so it doesn’t time out.

If your pdf is so long that the simple rendering causes it to time out, then I guess you are out of luck.

If you did have the special use case like @stefano.menci described you would be out of luck getting the users browser or anvil itself to render your super long PDF.
However if this was your special case, you could have a special solution, using anvil to render multiple pdfs in smaller files and then sending them over uplink if you are on the free plan, or using PyPDF2 in a server module if you are on the paid plan.

Once you sent all the separately generated PDF’s to an uplink or a server module they could be combined using PyPDF2.PdfFileMerger() as described in this stackoverflow thread:

It also says it takes file handlers, so I suggest sending the files as anvil.media objects and creating a filestream using io.BytesIO( anvil.media.get_bytes() ) for each file to be recombined, keeping everything in memory instead of writing anything to disk.

2 Likes