Using ReportLab Platypus, I am trying to make a PageTemplate composed of 2 Frames. Each Frame takes the entire width of the document and half its height, and they are (supposedly) stacked on top of one another.
The bottom frame is made to start on the bottom-left corner of the document, and has height equals to half of the document's height. The top frame is made to start at half the height of the document on the left side, and has height equals to half of the document's height.
The problem is that in the actual generated PDF, the two frames overlap on 1 inch (meaning if I translate the top frame up by 1 inch, they are perfectly stacked on top of one another). Any clue as to why that might happen ?
The generating code is as follows :
from reportlab.platypus import BaseDocTemplate, Frame, FrameBreak, Paragraph, NextPageTemplate, PageBreak, PageTemplate
from reportlab.lib.units import inch
from reportlab.lib.styles import getSampleStyleSheet
styles=getSampleStyleSheet()
Elements=[]
doc = BaseDocTemplate('testdoc.pdf', showBoundary = 1)
frame1 = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height / 2, showBoundary = 1, id = 'BottomFrame')
frame2 = Frame(doc.leftMargin, doc.height / 2, doc.width, doc.height / 2 , showBoundary = 1, id = 'TopFrame')
Elements.append(Paragraph("Top-bottom frames, " * 100, styles['Normal']))
Elements.append(FrameBreak())
Elements.append(Paragraph("Top-bottom frames, " * 100, styles['Normal']))
doc.addPageTemplates([PageTemplate(id = 'TwoRows', frames = [frame2, frame1])])
#start the construction of the pdf
doc.build(Elements)
I have checked that document's pagesize, height, margins and the frames' sizes correspond, and they do. I admit to be a bit stumped, according to this there should not be any way for the Frames to overlap.
print(doc.pagesize) : (595.2755905511812, 841.8897637795277)
print(doc.height) : 697.8897637795277
print(doc.topMargin) : 72.0
print(doc.bottomMargin) : 72.0
print(frame1.y1) : 72.0
print(frame1.height) : 348.94488188976385
print(frame1.y1) : 348.94488188976385
print(frame2.height) : 348.94488188976385
TLDR: Solution
frame2 = Frame(doc.leftMargin, (doc.height / 2) + doc.bottomMargin, doc.width, doc.height / 2 , showBoundary = 1, id = 'TopFrame')
Explanation:
You are using margins of 1 inch (72 points) as you stated:
print(doc.topMargin) : 72.0
print(doc.bottomMargin) : 72.0
Your BottomFrame starts at the bottom margin, i.e. at 72 points (print(frame1.y1) : 72.0
) and has a height of ~349 points (print(frame1.height) : 348.94488188976385
). Your TopFrame starts at ~349 points (frame2 = Frame(doc.leftMargin, doc.height / 2, doc.width, doc.height / 2 , showBoundary = 1, id = 'TopFrame')
), not accounting for the margin.
Thus, you have an overlap equivalent to the margin, which can be solved by adding it when defining y1
of the TopFrame:
(doc.height / 2) + doc.bottomMargin