rendering a ReportLab pdf built from SimpleDocTemplate

前端 未结 2 677
面向向阳花
面向向阳花 2021-02-08 02:43

I\'ve a got a django app that currently generates pdfs using a canvas that the user can download. I create a StringIO buffer, do some stuff and then send call response.write.

相关标签:
2条回答
  • 2021-02-08 02:57

    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
    
    0 讨论(0)
  • 2021-02-08 03:02

    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.

    0 讨论(0)
提交回复
热议问题