Report Lab not Downloading PDF

I am using the “Complex PDF” part of this page of the docs, Outputting PDFs with Django | Django documentation | Django

I can get good data in the console when the method runs but the browser will not download the document, I copy/pasted the code as is to just get it working. Is there something I could be missing?

Quite possibly.

What versions of Python, Django, and ReportLab are you using? (Have you verified compatibility among the 3?)

(I think we’d need to see the complete view to provide any more assistance than that.)

Can you be more precise and provide more detail about exactly what’s happening?

Django Version = 3.2.4
Python Version = 3.8.5
ReportLab Version = 3.5.67

The view is exactly as shown in the link of the original post, I wanted to get it working before changing anything.

When I click the button on screen, I can print out the below in the console but nothing else happens. It is not supposed to redirect or anything like that, I want it to stay on the same page but I am expecting the download dialog that never shows.

Printing out the pdf variable from the code in the link I get:
b’%PDF-1.3\n%\x93\x8c\x8b\x9e ReportLab Generated PDF document http://www.reportlab.com\n1 0 obj\n<<\n/F1 2 0 R\n>>\nendobj\n2 0 obj\n<<\n/BaseFont /Helvetica /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font\n>>\nendobj\n3 0 obj\n<<\n/Contents 7 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 6 0 R /Resources <<\n/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]\n>> /Rotate 0 /Trans <<\n\n>> \n /Type /Page\n>>\nendobj\n4 0 obj\n<<\n/PageMode /UseNone /Pages 6 0 R /Type /Catalog\n>>\nendobj\n5 0 obj\n<<\n/Author (anonymous) /CreationDate (D:20210621161821+00’00’) /Creator (ReportLab PDF Library - www.reportlab.com) /Keywords () /ModDate (D:20210621161821+00’00’) /Producer (ReportLab PDF Library - www.reportlab.com) \n /Subject (unspecified) /Title (untitled) /Trapped /False\n>>\nendobj\n6 0 obj\n<<\n/Count 1 /Kids [ 3 0 R ] /Type /Pages\n>>\nendobj\n7 0 obj\n<<\n/Filter [ /ASCII85Decode /FlateDecode ] /Length 101\n>>\nstream\nGapQh0E=F,0U\H3T\pNYT^QKk?tc>IP,;W#U1^23ihPEM_?CW4KISi90EC-p>QkRte=<%VL%>/9S.n+\Kodj>&1ruY"YKd-rtBM~>endstream\nendobj\nxref\n0 8\n0000000000 65535 f \n0000000073 00000 n \n0000000104 00000 n \n0000000211 00000 n \n0000000414 00000 n \n0000000482 00000 n \n0000000778 00000 n \n0000000837 00000 n \ntrailer\n<<\n/ID \n[]\n% ReportLab generated PDF document – digest (http://www.reportlab.com)\n\n/Info 5 0 R\n/Root 4 0 R\n/Size 8\n>>\nstartxref\n1028\n%%EOF\n’

Printing out the response object as implemented in the link:
<HttpResponse status_code=200, “application/pdf”>

The documentation you linked to is for Django 1.10 using Python 2. However, that’s not what you’re using, which is why I want to see the actual view you’re using.

Ugh the documentation version…
I have switched to 3.2 documentation and implemented as described in the doc. I am still not getting the browser dialog.

Full View:


import io
from django.http import FileResponse
from reportlab.pdfgen import canvas


def GenerateDocumentation(request):
    # Create a file-like buffer to receive PDF data.
    buffer = io.BytesIO()

    # Create the PDF object, using the buffer as its "file."
    p = canvas.Canvas(buffer)

    # Draw things on the PDF. Here's where the PDF generation happens.
    # See the ReportLab documentation for the full list of functionality.
    p.drawString(100, 100, "Hello world.")

    # Close the PDF object cleanly, and we're done.
    p.showPage()
    p.save()

    # FileResponse sets the Content-Disposition header so that browsers
    # present the option to save the file.
    buffer.seek(0)
    return FileResponse(buffer, as_attachment=True, filename='hello.pdf')

Is the issue that you’re not getting a file, or is it that you’re getting the file but not getting the dialog asking where you want it to be saved?

Using this view I am not getting either. I assume that this view would save it to my working directory. I would like it to also show the dialog on the client browser to download it as well.

No, it’s going to save it to the downloads directory configured in your browser. Also, whether or not the dialog box is shown is a browser-configuration issue.

Check your download directory for the file, and your browser settings regarding the download dialog box.

No result in my downloads directory, my browser is configured to prompt for file download.

Ok, I can’t recreate the behavior you’re reporting. When I run your view, I get the PDF created and downloaded into my downloads directory.

I guess the next thing to check would be to look at the network tab in your developer tools in the browser to see if you’re getting the full response.

Thank you for your time and attention, I was getting a good response. I then looked at the other options to accomplish this and ended up going with xhtml2pdf. I have no doubt reportlab would have worked. What the issue ended up being is that I was trying to pass pdf generation from a view into another method and back and something was getting lost, I also completely forgot to add the url.py entry. xhtml2pdf lets me input the data just as I would a django template. File creation and download are working.