[Fixed]-Rendering a ReportLab pdf built from SimpleDocTemplate

24👍

Your error is actually a pretty simple one. It’s just a matter of trying to write the wrong thing. In your code, menu_pdf is not a PDF, but a SimpleDocTemplate, and the PDF has been stored in pdf_name, although here I suspect pdf_name is a path name rather than a file object. To fix it, change your code to use a memory file like you did in your original code:

# Set up response
response = HttpResponse(mimetype='application/pdf')
pdf_name = "menu-%s.pdf" % str(menu_id)
response['Content-Disposition'] = 'attachment; filename=%s' % pdf_name

buff = StringIO()

menu_pdf = SimpleDocTemplate(buff, rightMargin=72,
                            leftMargin=72, topMargin=72, bottomMargin=18)

# container for pdf elements
elements = []

styles=getSampleStyleSheet()
styles.add(ParagraphStyle(name='centered', alignment=TA_CENTER))

# Add the content as before then...

menu_pdf.build(elements)
response.write(buff.getvalue())
buff.close()
return response

I’m not sure if using file objects rather than paths with Platypus is mentioned in the documentation, but if you dig into the code you’ll see that it is possible.

5👍

For people who are working with python3 and django 1.7+ some changes to the answer need to be done.

from django.shortcuts import HttpResponse
import io
from reportlab.platypus import SimpleDocTemplate, BaseDocTemplate

def view(request):
    buffer = io.BytesIO()

    doc = # ... create your SimpleDocTemplate / BaseDocTemplate
    # create the usual story
    story = []
    # ...
    doc.build(story)

    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=your_name.pdf'
    response.write(buffer.getvalue())
    buffer.close()

    return response

Leave a comment