Creating a PDF does not work

Hello all,
I am trying to create a PDF copy of my app but I get this error:

AnvilWrappedError: Invalid print session
at /libanvil/anvil/pdf.py, line 17
called from /libanvil/anvil/pdf.py, line 26
called from C:/Users/3dslocal/AppData/Local/Programs/Python/Python38-32/lib/site-packages/anvil/pdf.py, line 12

Here is my code :
client code:
media_object = anvil.server.call(‘create_weekly_report_pdf’)
anvil.media.download(media_object)
server code:
@anvil.server.callable
def create_weekly_report_pdf():
pdf = PdfRenderer(page_size=‘A4’).render_form(‘Weekly_Report’)
return pdf

Thank you for your help

Please see the Tip here on how to format Python code to make it easier to read: How to ask a good question

Otherwise, I don’t have any immediate ideas to help. Sharing a clone link to your app (or, ideally, a simplified version–being sure to remove anything confidential) would be helpful to see what you’re trying to render.

1 Like

Hi

The error traceback looks like anvil app server is running on windows. I’m not sure that the built-in pdf render works on windows.

Hi @norimene.jamaia.pro and welcome to the Forum!

Not sure if the code you’ve included is exactly the same as in your app but the class should be PDFRenderer not PdfRenderer. Also, what is 'Weekly_Report' that you are passing into render_form?

To render a form as a PDF, you can add an argument to create_weekly_report_pdf, and then when you call it, pass in the form you want to render. Your server function should then pass in that form to render_form.

@brooke, I was expecting that render_form would work as you describe, but I was surprised when I actually went to use it recently that it doesn’t seem to work quite that way. In the example app (and the working one I’ve created: https://anvil.works/build#clone:KSCPA4HKNRDBYSAW=4FBCHZMZIWGZBJSWUYAF6FLZ), what actually gets passed to render_form (in server-side code that doesn’t import the form) is the name of form, as a string.

Not sure if I’m using this term right, but it seems like “magic” in not necessarily a good way.

You’re right! I even wrote a tutorial on the subject :woman_facepalming:. It’s been awhile since I’ve used render_form, and I suppose I was overly confident. Hopefully your example and/or the tutorial can help point @norimene.jamaia.pro in the right direction.

1 Like

Thank you all for your help, I have created the PDF. Thank you very much.

In fact, I’m a bit stuck on the next step
I want to create the PDF at the state when I generate my report by clicking on ‘Fetch report’ button but it creates the PDF in the initial state before creating the report? (when I open the form )
Please, what I need to add in the code to be able to create my PDF with the end view after clicking on the button.



Thank you for you help again

1 Like

So it’s not quite as simple as just “create a PDF from how the form looks in the browser in the end view.” (You could do that manually with your browser’s print function, saving to PDF. But this is something different–and more powerful/general ultimately.)

The PDF is rendered server-side, so it doesn’t actually “know” how the currently open form looks in the browser. What you need to do is explicitly pass the information input by the user via the form to the server function–and then to the render_form method. The tutorial (and docs) show you how.

1 Like