我正在自动生成一个包含动态内容的Platypus的PDF文件。
这意味着可能会发生文本内容的长度(直接位于pdf文件底部)可能会有所不同。
但是,如果内容太长,可能会发生分页。 这是因为我使用了“静态”间隔符:
s = Spacer(width=0, height=23.5*cm)
因为我总是希望只有一个页面,所以我需要动态设置Spacer的高度,以便页面上留下的空间的“其余”由Spacer作为其高度。< / p>
现在,我如何得到留在页面上的“休息”高度?
答案 0 :(得分:2)
我在reportlab库中嗅了一下,发现了以下内容: 基本上,我决定使用一个框架,打印出流动装置。 f._aH返回Frame的高度(我们也可以手动计算)。减去我们通过包裹得到的其他两个可流动物的高度,我们得到剩余高度,即间隔物的高度。
elements.append(Flowable1)
elements.append(Flowable2)
c = Canvas(path)
f = Frame(fx, fy,fw,fh,showBoundary=0)
# compute the available height for the spacer
sheight = f._aH - (Flowable1.wrap(f._aW,f._aH)[1] + Flowable2.wrap(f._aW,f._aH)[1])
# create spacer
s = Spacer(width=0, height=sheight)
# insert the spacer between the two flowables
elements.insert(1,s)
# create a frame from the list of elements
f.addFromList(elements,c)
c.save()
测试并且工作正常。
答案 1 :(得分:0)
据我所知,你想要有页脚,对吗?
然后你应该这样做:
def _laterPages(canvas, doc):
canvas.drawImage(os.path.join(settings.PROJECT_ROOT, 'templates/documents/pics/footer.png'), left_margin, bottom_margin - 0.5*cm, frame_width, 0.5*cm)
doc = BaseDocTemplate(filename,showBoundary=False)
doc.multiBuild(flowble elements, _firstPage, _laterPages)