开发者

Generate simple pdf report using platypus

开发者 https://www.devze.com 2023-03-29 21:11 出处:网络
I am trying to generate a pdf report using reportlab in django.I can get a simple report started by working directly with the canvas, but it looks like platypus should make things easier.But I can\'t

I am trying to generate a pdf report using reportlab in django. I can get a simple report started by working directly with the canvas, but it looks like platypus should make things easier. But I can't get a simple platypus report to work.

def all_comps_pdf_report(request):

    # Set up HttpResponse object
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=all_competencies.pdf'

    from reportlab.platypus.doctemplate import SimpleDocTemplate
    from reportlab.platypus import Paragraph
    from reportlab.lib import styles

    doc = SimpleDocTemplate(response)
    Elements = []
    p = Paragraph("Hello World", styles['Heading1'])
    Elements.append(p)
    doc.build(Elements)
    return response

I am getting an error 'module' object is unsubscriptable, which is开发者_开发技巧 complaining about the line p = Paragraph("Hello World", styles['Heading1']). What am I doing wrong?


You are getting 'module' object is unsubscriptable because you are treating module as it is an array :)

If you'll browse through the source of reportlab then you'll see that styles is just a module with a lot of stuff in it.

For this example to work, you need to import stylesheets: from reportlab.lib.styles import getSampleStyleSheet and then styles = getSampleStyleSheet().

Or you can create your own stylesheet - look through the reportlab's docs on how to do that :)

0

精彩评论

暂无评论...
验证码 换一张
取 消