سؤال

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 :)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top