Question

I'm using Python 2.7.6 and Django 1.5.5. How I can write a line in SimpleDocTemplate?

I'm tryng this:

@login_required
def report(request):
    rep = Report(request.user.username + "_cities.pdf")

    # Title
    rep.add_header("Cities")

    line = Line(0, 100, 500, 100)
    rep.add(line)

    # Body
    columns = ("City")
    cities = [(p.name) for p in City.objects.all()]

    table = Table([columns] + cities, style=GRID_STYLE)
    table.hAlign = "LEFT"
    table.setStyle([('BACKGROUND', (1, 1), (-2, -2), colors.lightgrey)])

    rep.add(table)

    rep.build()
    return rep.response

The Line() is from reportlab.graphics.shapes import Line. The class Report is only a wrapper class to SimpleDocTemplate:

class Report:
    styles = None
    response = None
    document = None
    elements = []

    def __init__(self, report_file_name):
        self.styles = styles.getSampleStyleSheet()
        self.styles.add(ParagraphStyle(name='Title2',
                                       fontName="Helvetica",
                                       fontSize=12,
                                       leading=14,
                                       spaceBefore=12,
                                       spaceAfter=6,
                                       alignment=TA_CENTER),
                        alias='title2')

        self.response = HttpResponse(mimetype="application/pdf")
        self.response["Content-Disposition"] = "attachment; filename=" + report_file_name

        self.document = SimpleDocTemplate(self.response, topMargin=5, leftMargin=2, rightMargin=1, bottomMargin=1)
        self.document.pagesize = portrait(A4)

        return

    def add_header(self, header_text):
        p = Paragraph(header_text, self.styles['Title2'])
        self.elements.append(p)

    def add(self, paragraph):
        self.elements.append(paragraph)

    def build(self):
        self.document.build(self.elements)

When I call report function, I receive the error message:

Line instance has no attribute 'getKeepWithNext'

hen I remove/comment the lines with Line(), the error don't ocurre.

You can help me? How to write that line?

Was it helpful?

Solution

Just adding Line to the list of elements doesn't work: you can only pass Flowables to SimpleDocTemplate.build(). But you can wrap it in a Drawing, which is a Flowable:

d = Drawing(100, 1)
d.add(Line(0, 0, 100, 0))
rep.add(d)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top