ReportLab:“正常”框架中第 1 页的 Flowable 太大模板“First”的

发布于 2024-12-08 14:57:39 字数 1122 浏览 1 评论 0原文

我使用 ReportLab 构建 PDF。我的程序有一个 MyDocTemplate(SimpleDocTemplate) 类,它有两个方法:beforePage(self)afterPage(self) ,它们添加页眉和页脚(如PNG 图像)在每一页上。还有一个 MyDocStyle 类描述 ParagraphStyle

主要方法如下所示:

TITLE = Paragraph(Title, MyDocStyle.h1)
TO = Paragraph(To, MyDocStyle.h2)
FROM = Paragraph(From, MyDocStyle.h2)
SUBJECT = Paragraph(Subject, MyDocStyle.h2)
LONG_PARAGRAPH = Paragraph(Text, MyDocStyle.h3)
...

Elements = [TITLE, TO, FROM, SUBJECT, LONG_PARAGRAPH, ...]
doc = MyDocTemplete('output.pdf', pagesize=A4, 
                     leftMargin=2*cm, rightMargin=2*cm,
                     topMargin=4*cm, bottomMargin=4*cm)
doc.build(Elements)

数据来自 CSV 文件和 GUI。有时(取决于数据长度)我收到错误:

Flowable <Spacer at 0x2631120 frame=normal>...(1 x 5.66929133858) too large
on page 1 in frame 'normal'(469.88976378 x 603.118110236) of template 'First'

此异常停止我的程序。对于简短的段落,我在 MyDocStyleh2.keepWithNext = 1 中设置,但这不是完美的解决方案。如果段落结尾与页面结尾(文本区域)不“重合”,ReportLab 会正确拆分长段落。

我该如何处理?

I build PDF using ReportLab. My program has a MyDocTemplate(SimpleDocTemplate) class with two methods: beforePage(self) and afterPage(self) which add header and footer (as PNG image) on every page. There is also a MyDocStyle class which describe ParagraphStyle.

Main method looks like this:

TITLE = Paragraph(Title, MyDocStyle.h1)
TO = Paragraph(To, MyDocStyle.h2)
FROM = Paragraph(From, MyDocStyle.h2)
SUBJECT = Paragraph(Subject, MyDocStyle.h2)
LONG_PARAGRAPH = Paragraph(Text, MyDocStyle.h3)
...

Elements = [TITLE, TO, FROM, SUBJECT, LONG_PARAGRAPH, ...]
doc = MyDocTemplete('output.pdf', pagesize=A4, 
                     leftMargin=2*cm, rightMargin=2*cm,
                     topMargin=4*cm, bottomMargin=4*cm)
doc.build(Elements)

Data comes from CSV files and GUI. From time to time (depends on data length) I receive an error:

Flowable <Spacer at 0x2631120 frame=normal>...(1 x 5.66929133858) too large
on page 1 in frame 'normal'(469.88976378 x 603.118110236) of template 'First'

This exception stop my program. For short Paragraphs I set in MyDocStyle class h2.keepWithNext = 1 however it's not perfect solution. ReportLab split correctly long paragraph if end of paragraph does not "coincide" with end of page (text area).

How can I deal with it?

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

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

发布评论

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

评论(2

玻璃人 2024-12-15 14:57:39

当 ReportLab 尝试将 Spacer 拆分为两页时,会出现此错误。解决此问题的唯一方法似乎是将您的 Spacer 包装到 KeepTogether 元素中:

elements.append(KeepTogether(Spacer(width, height)))

This error occurs when ReportLab try to split a Spacer over two pages. It seems that the only way to workaround this issue is wrap your Spacer into a KeepTogether element:

elements.append(KeepTogether(Spacer(width, height)))
淡莣 2024-12-15 14:57:39

已解决。不要使用Spacer(例如Spacer(1, 0.2*cm))作为Paragraph的分隔符>。相反,请在 ParagraphStyle 中定义 spaceBeforespaceAfter,例如:

ParagraphStyle(name = 'Normal',
               fontName = "Verdana",
               fontSize = 11,
               leading = 15,
               alignment = TA_JUSTIFY,
               allowOrphans = 0,
               spaceBefore = 20,
               spaceAfter = 20,
               wordWrap = 1)

Solved. Don't use Spacer (e.g. Spacer(1, 0.2*cm)) as a separator for Paragraph. Instead, define spaceBefore and spaceAfter in ParagraphStyle, for example:

ParagraphStyle(name = 'Normal',
               fontName = "Verdana",
               fontSize = 11,
               leading = 15,
               alignment = TA_JUSTIFY,
               allowOrphans = 0,
               spaceBefore = 20,
               spaceAfter = 20,
               wordWrap = 1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文