ReportLab 中的动态垫片

发布于 2024-08-30 20:24:38 字数 294 浏览 3 评论 0原文

我使用 Platypus 自动生成一个包含动态内容的 PDF 文件。

这意味着文本内容的长度(直接位于 pdf 文件的底部)可能会发生变化。

但是,如果内容太长,可能会出现分页的情况。 这是因为我使用“静态”间隔器:

s = Spacer(width=0, height=23.5*cm) 

因为我总是只想只有一页,所以我需要以某种方式动态设置间隔器的高度,以便占用页面上剩余的空间的“其余”部分由垫片作为其高度。

现在,我如何获得页面上剩余的“其余”高度?

I'm automatically generating a PDF-file with Platypus that has dynamic content.

This means that it might happen that the length of the text content (which is directly at the bottom of the pdf-file) may vary.

However, it might happen that a page break is done in cases where the content is too long.
This is because i use a "static" spacer:

s = Spacer(width=0, height=23.5*cm) 

as i always want to have only one page, I somehow need to dynamically set the height of the Spacer, so that the "rest" of the space that is left on the page is taken by the Spacer as its height.

Now, how do i get the "rest" of height that is left on my page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-09-06 20:24:38

我在reportlab库中嗅了一下,发现了以下内容:
基本上,我决定使用一个框架来打印可流动的内容。 f._aH 返回框架的高度(我们也可以手动计算)。减去通过包装获得的其他两个可流动物体的高度,我们得到剩余的高度,即间隔物的高度。

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()

经过测试并且工作正常。

I sniffed around in the reportlab library a bit and found the following:
Basically, I decided to use a frame into which the flowables will be printed. f._aH returns the height of the Frame (we could also calculate this by hand). Subtracting the heights of the other two flowables, which we get through wrap, we get the remaining height which is the height of the Spacer.

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()

tested and works fine.

听,心雨的声音 2024-09-06 20:24:38

据我所知,您想要页脚,对吗?

那么你应该这样做:

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)

As far as i can see you want to have footer, right?

Then you should do it like:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文