Generate PDF without errors

Hello,

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) 

Thank you

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:

https://anvil.works/forum/search?q=PDF%20takes%20too%20long

2 Likes

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

It looks like you are.

Try to put a bunch of prints with the timestamp on the client and on the server functions and on the form __init__.

This will give you an idea of what’s going on and when, and will help you finding the bottleneck.

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

Try to put more timestamps:

    print('A1', time.time())
    renderer = PDFRenderer(page_size='A4',filename=f'{name}.pdf')
    print('A2', time.time())
    pdf = renderer.render_form('PDF_form',name,age.....)
    print('A3', time.time())

And inside PDF_form:

class PDF_form(PDF_formTemplate):
    def __init__(self, **properties):
        print('B1', time.time())
        self.init_components(**properties)
        print('B2', time.time())
        # do your stuff
        print('B3', time.time())

    def form_show(self, **event_args):
        print('C1', time.time())
        # do your stuff
        print('C2', time.time())

Even considering that time.time() on the server is not synchronized with the client, this should give some useful information.

You can also try to return before B2 and before C1, to compare the results of the rendering with the same form that does nothing.

The prints from the renderer may not be visible on the console, but should be visible on the app log (not sure).

Thank you, great idea. I will start with creating a blank PDF page to check if error is in the pdf_form.

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

Hello @ianb @stefano.menci thank you for your help so far, an update,

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

Thank you,
Besar

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

Thanks for your prompt response. How can I reach out the Anvil staff from here, tag someone?

Hi @besarberdica1,

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!

Hi,

Can I send all the required information by email?

Thank you

1 Like

Sure - support@anvil.works - but we will reply on this thread so everyone can benefit from the answer.

1 Like

Hi @besarberdica1,

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.

Regards

Hello and happy new year to all.

I have reviewed the DNS configuration with the instructions for the custom domain as the photos below:

Am I missing something in order to use my domain corretly?

Thank you,
Besar