英文:
Why are my ReportLab PageTemplate frames overlapping in my generated PDF?
问题
使用ReportLab Platypus,我正在尝试创建一个由2个帧组成的PageTemplate。每个帧占据整个文档的宽度和一半的高度,它们(理论上)应该叠放在彼此上面。
底部帧被设置为从文档的左下角开始,高度等于文档高度的一半。顶部帧被设置为从文档的左侧开始,高度等于文档高度的一半。
问题是,在实际生成的PDF中,这两个帧在1英寸处重叠(意味着如果我将顶部帧向上平移1英寸,它们就会完美叠放在彼此上面)。有什么线索可以解释为什么会发生这种情况吗?
生成的代码如下:
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])])
# 开始构建PDF文档
doc.build(Elements)
我已经检查了文档的页面大小、高度、边距和帧的大小,它们是对应的。我承认有点困惑,根据这个信息,帧不应该重叠。
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
英文:
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
答案1
得分: 0
简要: 解决方案
frame2 = Frame(doc.leftMargin, (doc.height / 2) + doc.bottomMargin, doc.width, doc.height / 2 , showBoundary = 1, id = 'TopFrame')
解释:
你使用了1英寸(72点)的页边距,正如你所述:
print(doc.topMargin):72.0
print(doc.bottomMargin):72.0
你的BottomFrame从底部边距开始,即72点(print(frame1.y1):72.0
),高度约为349点(print(frame1.height):348.94488188976385
)。你的TopFrame从约349点开始(frame2 = Frame(doc.leftMargin, doc.height / 2, doc.width, doc.height / 2 , showBoundary = 1, id = 'TopFrame')
),没有考虑页边距。
因此,你有一个等于页边距的重叠,可以通过在定义TopFrame的y1
时添加它来解决:
(doc.height / 2) + doc.bottomMargin
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论