How to use canvas and Simpledoctemplate to each other? (reportlab)

Asked

Viewed 69 times

-1

I’m doing the exporting of reports through the library reportlab. To insert the text I used the canvas module, and wrote this code that works well.

from reportlab.pdfgen import canvas

pdf = canvas.Canvas('exemple3.pdf', pagesize=A4)

pdf.setFillColor(aColor='blue')
pdf.setFont('Helvetica', 14)
pdf.drawString(55, 740, 'RELATÓRIO FEVEREIRO 2021')

pdf.setFillColor(aColor='black')
pdf.setFont('Helvetica', 22)
pdf.drawString(55, 715, 'PROJECTOS')
pdf.drawString(55, 695, 'MINOR HOTEL GROUP')
pdf.drawString(55, 675, 'HOTÉIS TIVOLI PORTUGAL')
pdf.save()

Next I will need to insert a table in the PDF and for that I discovered that the Simpledoctemplate module could help me and so I created the following code that works well separate from the previous one:

from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle

elements = []

data= [['Periodo a que diz respeito', startdate + ' a ' + enddate],
       [' ', ' '],
       ['Horas despendidas no período de 01/01/2021 a 20/01/2021', '271,04'],
       ['Por rubrica:', ' ']]

t=Table(data)
t.setStyle(TableStyle([('BOX', (0,0), (-1,-1), 0.25, colors.black),
                       ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                       ('ALIGN',(1,1),(-3,-3),'RIGHT'),
                       ('VALIGN',(0,-1),(-1,-1),'MIDDLE'),
                       ('TEXTCOLOR',(0,0),(1,-1),colors.black)]))
elements.append(t)
pfd.build(elements)

My question is, how do I put these two codes together? If you have another solution, I’d appreciate it :)

1 answer

0


Let’s not combine the Canvas method with Simpledoctemplate, it doesn’t make sense. After several searches what ententi is that Canvas is great for generating text, simpler documents and to initially realize a bit of the library reportlab

The solution I came up with was this::

I imported these two variants more from reportlab and kept the same code to generate the table.

from reportlab.platypus import Paragraph
from reportlab.lib.styles import ParagraphStyle

elements = []

data= [['Periodo a que diz respeito', startdate + ' a ' + enddate],
       [' ', ' '],
       ['Horas despendidas no período de 01/01/2021 a 20/01/2021', '271,04'],
       ['Por rubrica:', ' ']]

t=Table(data)
t.setStyle(TableStyle([('BOX', (0,0), (-1,-1), 0.25, colors.black),
                       ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
                       ('ALIGN',(1,1),(-3,-3),'RIGHT'),
                       ('VALIGN',(0,-1),(-1,-1),'MIDDLE'),
                       ('TEXTCOLOR',(0,0),(1,-1),colors.black)]))

Text is being added to the array Elements through the variable Paragraph reportlab. The style variable is the one that will give this text another aspect.

style = ParagraphStyle('heading1',
                       fontName = 'Helvetica-Bold',
                       fontSize = 20,
                       textColor = colors.black,
                       leading = 20)

elements.append(Paragraph('RELATÓRIO FEVEREIRO 2021', style))
elements.append(Paragraph('PROJECTOS', style))
elements.append(Paragraph('TIVOLI HOTEIS', style))

elements.append(t)#adiciona a tabela à variavel elements

pdf = SimpleDocTemplate('exemple3.pdf', pagesize=A4)
pfd.build(elements)

Finally, pdf.build(Elements)' will add the text and table to the PDF report, with the configured formatting.

I’m new around here, sorry if I didn’t explain it well. Feel free to ask questions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.