Search code examples
reportlab

reportlab set left table position


How can I set the left position of a table?

response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=%s' % pdf_name
buffer = StringIO()
PAGESIZE = pagesizes.portrait(pagesizes.A4)
doc = SimpleDocTemplate(buffer, pagesize=PAGESIZE, leftMargin=1*cm)
story = []

story.append(Paragraph(header_part2, styleN))
table_row = ['Total Score:','']
data.append(table_row)
ts = [
    #header style    
    ('LINEABOVE', (0,0), (-1,0), 1, colors.gray),
    ('LINEBELOW', (0,0), (-1,0), 1, colors.gray)]
t = Table(data, (6*cm,6*cm), None, style=ts)
    story.append(t)
    doc.build(story)
    pdf = buffer.getvalue()
buffer.close()
response.write(pdf)

Whereas the paragraph is printed one cm from the left, table is printed with almost no distance to the left page border.

Where do I have to set the leftMargin for the table position?

The same is valid for images I add. They seem to be printed somewhere.

story.append(Image(path,35,10))

Solution

  • Found the magic hAlign keyword:

    t = Table(data, (6*cm,6*cm,2*cm,2*cm,2*cm), None, style=ts, hAlign='LEFT')