Reputation: 59
I am creating pdf using Reportlab and python 3.when a table split automatically it overlap my page header.In first page no issue.From second page onwards header gets overlapped by data. Any solution?
Upvotes: 3
Views: 2677
Reputation: 973
There are probably a few different ways to solve this problem with ReportLab. Frames can be a used to achieve the results you are looking for. The example below draws the same header on each page and restricts the table to the frame. Additionally, it uses the repeatRows table parameter to repeat the table header on each page.
The showBoundary parameter was used to show the location and size of the frame. Delete this argument or set to 0 to remove boundary lines.
The solution was developed based on this answer about text and footers.
from reportlab.platypus import BaseDocTemplate
from reportlab.platypus import Frame, PageTemplate
from reportlab.platypus import Paragraph, Table, TableStyle
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
from reportlab.lib import colors
styles = getSampleStyleSheet()
styleN = styles['Normal']
styleH = styles['Heading1']
def header(canvas, pdf):
# Draw heading
heading = Paragraph("Heading", styleH)
heading.wrap(pdf.width, inch * 0.5)
heading.drawOn(canvas, pdf.leftMargin, pdf.height + inch)
# Draw subheading.
subheading = Paragraph("subeading", styleN)
subheading.wrap(pdf.width, inch * 0.25)
subheading.drawOn(canvas, pdf.leftMargin, pdf.height + inch * 0.75)
pdf = BaseDocTemplate('example.pdf', pagesize=letter)
frame = Frame(
pdf.leftMargin,
pdf.bottomMargin,
pdf.width,
pdf.height - inch * 0.5,
showBoundary=1) # Delete to remove line around the frame.
template = PageTemplate(id='all_pages', frames=frame, onPage=header)
pdf.addPageTemplates([template])
# Data for table
data = [["String", "String", "Number", "Number", "Number"]]
data.extend([["one", "two", i, i, i] for i in range(90)])
# Styles for table
table_style = TableStyle([
('LINEBELOW', (0, 0), (-1, 0), 2, colors.black),
('ALIGN', (2, 0), (4, -1), 'RIGHT'),
])
# Create table and repeat row 1 at every split.
table = Table(data, repeatRows=1, style=table_style)
pdf.build([table])
Upvotes: 5