Forums

Serve Dynamically generated PDFs from site.

I have a django site and am successfully converting some templates to PDF and providing them to users in our test environment but I cannot seem to get it to work when hosted in production. When I try, I get an internal server error Please advise on how someone could use xhtml2pdf to provide a file dynamically to a user. Thanks

It's hard to tell without more information. Have you checked your logs for more details? You can find links to them on your "Web" page. Could you share the code snippet that is failing?

Yes, it was an incomplete stack error. It turns out with some more testing that it is an issue with the encryption. When I add encryption to the pisa.CreatePDF, it gives that server error. I am using the standard encryption option that is provided via the ReportLab lib. Is there something specific needed within the pythonanywhere config for me to be able to use this option?

# Encrypt the PDF
enc=pdfencrypt.StandardEncryption(
    userPassword="mypswd",
    ownerPassword="mypswd",
    canPrint=0,
    canModify=0,
    canCopy=0,
    canAnnotate=0,
    strength=128
)

Could you show us the exact error you're getting?

It appears to be incomplete... I investigated the watermarks component but that doesn't appear to be the issue.. Even completely removing all images (for xhtml2pdf a bg image would be treated as a watermark) and have a basic text based PDF still resulted in error as long as I was using the encryption. Soon as I remove the encryption, the pdf export works. Here is the message from the log file: 2024-09-29 13:26:53 29/09/2024 13:26:53 ERROR Internal Server Error: /analysis/analysis/pdf/2eebcd07-a90e-4af7-a343-fb107d8c4f5d/#012Traceback (most recent call last):#012 File "/.virtualenvs/dj5_hdk/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner#012 response = get_response(request)#012 File "/.virtualenvs/dj5_hdk/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response#012 response = wrapped_callback(request, callback_args, *callback_kwargs)#012 File "/hdk/analysis/views.py", line 1199, in export_pdf#012 pisa_status = pisa.CreatePDF(#012 File "/.virtualenvs/dj5_hdk/lib/python3.10/site-packages/xhtml2pdf/document.py", line 200, in pisaDocument#012 output, has_bg = WaterMarks.process_doc(context, out, output)#012 File "/.virtualenvs/dj5_hdk/lib/python3.10/site-packages/xhtml2pdf/builders/watermarks.py", line 121, in proces

That looks like you have truncated the traceback so we can't tell what is happening.

We do not have any knowledge of the PDF library you're using. If you are having issues with it, you may be able to get help from the authors or an associated forum site.