I have a question on generating PDF with/without background tasks.
My PDF is a A4 page with 25-30 labels to be filled.
All the data are searched before from table as the code below(as the tutorial)
If I run this function from Anvil(IDE) it works perfectly, even without background tasks, but if I run with function from browser(as a client) I always get an error “Timeout server” or when using Background tasks the error “Exception: PDF generation failed: Components did not load within allowed time” .
Is this the right way to collect data to generate PDF-s and not having errors when clients run the function?
I think that in this way the data are collected in PDF_form before the form is rendered.
Code Sample:
Client side
for row in app_tables.table_name.search(client=user):
name = row['name']
age = row['age']
......
etc
pdf = anvil.server.call('create_pdf',name,age.....)
Server side
@anvil.server.callable
def create_pdf(name,age,.....)
pdf = PDFRenderer(page_size='A4',filename=f'{name}.pdf').render_form('PDF_form',name,age.....)
app_tables.table_name.get(Name=name,client=user).update(PDF_report=pdf)
The actual anvil render PDF function itself has a time limit on the server, regardless of payment plan.
There are some tricks to get around this, like having the form rendered first, then passed to the PDF generator.
The best way to find all of these tips and tricks is to search the forum:
Thank you for your suggestion although I have read a lot on forum and what I found is that the best way is to collect all data before rendering the PDF, but I don’t understand something. In my code as is written, am I not collecting the data to the pdf_form before rendering it?
Thank you for your suggestion very useful to identify a problem.
After some attempts when running the function as a client from browser(because from Anvil everything works fine, I don’t understand why this difference) the function stops in the code line below with the error “Server is taking too long”
pdf = PDFRenderer(page_size='A4',filename=f'{name}.pdf').render_form('PDF_form',name,age.....)
When using Anvil the whole process to create one PDF is 14 seconds according the timestamps registered and on browser(as client) it stops in the code rendering PDF.
What am I missing here? Should I consider changing the structure of PDF in Design because is heavy?
Thank you for your support. I have created a blank page PDF to test and check the timestamps.
The error persist as a Client. I have added the timestamps as showed below
After many attempts without results I tried something else. I have copied my app(as clone) and published it(with a default name that Anvil generates). The strange thing is that it works perfectly as a client, while in my “main” app the error code “Server code took too long” persists as I try to do the same process.
I am very confused why, Does it mean that my app has in memory something that does not work?
I cannot use the clone app because of the custom domain in my “main” app
When do you think you initially created the “main” app? If only the clone works and the original does not, you may be beyond the help of the forum and might need Anvil staff to look at it. Either here or by contacting support@anvil.works
Please can you post links to both your “main” app and your clone, along with instructions to reproduce the error in the “main” app, and instructions to generate the same PDF without error in the clone? If you’re happy for us to log into your account to investigate, we will do our best to work out what’s going on!
Thanks for sending through the app links via email. I can confirm that we’ve reproduced the problem, and narrowed down the issue quite a lot:
The PDF always renders correctly in your cloned app
The PDF always renders correctly when running your main app in the Anvil Editor
The PDF fails to render when accessing your app via your custom domain name
The PDF always renders correctly when accessing your app via a private URL (https://APP-ID.anvil.app/ACCESS-KEY)
So, that is fairly conclusive evidence that the issue is with the interaction between your custom domain name and the PDF renderer. We will continue to investigate and post to this thread when we have an update.
It appears that your custom domain name isn’t pointing to Anvil, but is instead pointing to somewhere else (possibly Cloudflare) that intercepts requests and decides whether to redirect to Anvil. When accessed by our PDF renderer, that redirection never happens, so your app never loads.
If you set up your custom domain DNS as instructed in our DNS Guide, that should resolve the issue! Please post here to let us know if that works.
Thank you, to be honest my IT made some changes due to some heavy traffic loadings in my app not able to access the app. I fw it as above and will return with a response.