closed table which splited into more than one page on each page

19 views
Skip to first unread message

Ines Rebhi

unread,
Jan 15, 2024, 9:07:49 AMJan 15
to reportlab-users
Hello I have add a table which extended on more than one page but i want close on each page can you help please declose table on the first page, second page if is extended on 3 pages and so this my code:

class InvoiceGenerator: def __init__(self, pdf_filename): self.pdf_filename = pdf_filename self.story = [] def generate_invoice(self): doc = SimpleDocTemplate(self.pdf_filename, pagesize=letter) # Add elements to the story self.add_header(doc) self.add_table(doc) def add_table(self, doc): COL_WIDTHS = [300, 80, 80, 80] # Example table data with random values table_data = [["Désignation", "Qte", "Prix Unitaire", "Montant HT"]] for _ in range(30): # You can adjust the number of rows as needed designation = random.choice(['Product AProduct AProduct AProduct AProduct AProduct AProduct AProduct AProduct AProduct AProduct AProduct AProduct AProduct A', 'Product B', 'Product C']) quantities = random.randint(1, 10) unit_price = random.randint(1, 10) total_price = quantities * unit_price # to return to line if designation is long tbl_row = [Paragraph(str(cell), getSampleStyleSheet()['BodyText']) if isinstance(cell, str) else cell for cell in [designation, quantities, unit_price, total_price]] table_data.append(tbl_row) # Create the table table = LongTable(table_data, colWidths=COL_WIDTHS, repeatRows=1, splitByRow=1) table.setStyle(TableStyle([ ('BOX', (0, 0), (-1, -1), 1, colors.black), # Grid lines for the header and columns ('GRID', (0, 0), (-1, 0), 1, colors.black), ('GRID', (1, 0), (0, -1), 1, colors.black), ('GRID', (2, 0), (0, -1), 1, colors.black), ('GRID', (3, 0), (0, -1), 1, colors.black), ("FONTNAME", (0, 0), (-1, 0), "Times-Italic"), ('TEXTCOLOR', (0, 0), (-1, 0), colors.darkgray), ('ALIGN', (1, 1), (-1, -1), 'CENTER'), ('LEFTPADDING', (0, 0), (1, 0), 0), ('RIGHTPADDING', (0, 0), (1, 0), 0), ('VALIGN', (0, 0), (1, 0), 'TOP'), ('ALIGN', (0, 0), (1, 0), 'CENTRE'), # DataTable ('ALIGN', (0, 1), (1, -1), 'CENTRE'), ('SIZE', (0, 1), (-1, -1), 7), ('LEADING', (0, 1), (-1, -1), 8.4), ('VALIGN', (0, 1), (-1, -1), 'MIDDLE'), ('TOPPADDING', (0, 1), (-1, -1), 2.6), ('BOTTOMPADDING', (0, 1), (-1, -1), 2.6) # ('LEFTPADDING', (0, 0), (-1, -1), 1), # ('RIGHTPADDING', (0, 0), (-1, -1), 1), # ('TOPPADDING', (0, 0), (-1, -1), 0), # ('BOTTOMPADDING', (0, 0), (-1, -1), 0), ])) spacer = Spacer(1, 40) # Add the table to the story self.story.extend([table, spacer]) @staticmethod def table_style(): tab_styl = TableStyle([('ALIGN', (0, 0), (-1, -1), 'LEFT'), ('FONTNAME', (0, 0), (-1, -1), 'Helvetica'), ('FONTSIZE', (0, 0), (-1, -1), 10), ("FONT", (0, 0), (0, -1), "Helvetica-Bold"), ('BACKGROUND', (0, 0), (-1, -1), (1, 1, 1))]) return tab_styl # Example usage pdf_filename = "invoice_example.pdf" invoice_generator = InvoiceGenerator(pdf_filename) invoice_generator.generate_invoice() 


the result of my pdf is as shown in image i want just in the first page close table:  with using grid style to make a grid table :
Reply all
Reply to author
Forward
0 new messages