使用 ReportLab 显示页数
我正在尝试将一个简单的“第 x 页,共 y 页”添加到使用 ReportLab 制作的报告中。我发现 这篇关于它的旧文章,但也许六年后出现了更直接的东西? ^^;
我也找到了这个食谱,但是当我使用它时,生成的PDF缺少图像。 。
I'm trying to add a simple "page x of y" to a report made with ReportLab.. I found this old post about it, but maybe six years later something more straightforward has emerged? ^^;
I found this recipe too, but when I use it, the resulting PDF is missing the images..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我能够从 ActiveState 实现 NumberedCanvas 方法。 这很容易做到,并且没有改变我现有的代码。 我所要做的就是添加 NumberedCanvas 类并在构建文档时添加 canvasmaker 属性。 我还更改了“x of y”显示位置的测量值:
成为
doc 是 BaseDocTemplate,pdf 是我的可流动元素列表。
I was able to implement the NumberedCanvas approach from ActiveState. It was very easy to do and did not change much of my existing code. All I had to do was add that NumberedCanvas class and add the canvasmaker attribute when building my doc. I also changed the measurements of where the "x of y" was displayed:
became
doc is a BaseDocTemplate and pdf is my list of flowable elements.
使用 doc.multiBuild
并在页面标题方法中(由“onLaterPages=”定义):
use doc.multiBuild
and in the page header method (defined by "onLaterPages="):
只是为您挖掘一些代码,我们使用以下代码:
现在
self._on_page
是为每个页面调用的方法,如下所示:Just digging up some code for you, we use this:
Now
self._on_page
is a method that gets called for each page like:我想出了一个针对鸭嘴兽的解决方案,它更容易理解(至少我认为是)。 您可以手动执行两次构建。 在第一个版本中,您可以存储总页数。 在第二个构建中,您已经提前知道了。 我认为它更容易使用和理解,因为它适用于鸭嘴兽级别的事件处理程序,而不是画布级别的事件。
您只需要确保始终渲染原始故事的深层副本即可。 否则就行不通。 (您将得到一个空页面作为输出,或者一个渲染错误,告诉您 Flowable 不适合框架。)
I came up with a solution for platypus, that is easier to understand (at least I think it is). You can manually do two builds. In the first build, you can store the total number of pages. In the second build, you already know it in advance. I think it is easier to use and understand, because it works with platypus level event handlers, instead of canvas level events.
You just need to make sure that you always render a deep copy of your original story. Otherwise it won't work. (You will either get an empty page as the output, or a render error telling that a Flowable doesn't fit in the frame.)
我对结构进行了深度复制,因此在第一次构建后它仍然可用。 然后我获取文档中的总页数并将其保存在参数 pdf.final_pages 中。 在第二个版本中,我使用此参数在文档标题中打印正确的页码:
I make a deep copy of the structure, so it is still available after the first build. Then I get the total number of the pages in the document and save it in parameter pdf.final_pages. In second build I use this parameter to print the correct pages number in the header of the document: