ReportLab:“正常”框架中第 1 页的 Flowable 太大模板“First”的
我使用 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'
此异常停止我的程序。对于简短的段落,我在 MyDocStyle
类 h2.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当 ReportLab 尝试将 Spacer 拆分为两页时,会出现此错误。解决此问题的唯一方法似乎是将您的 Spacer 包装到 KeepTogether 元素中:
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:
已解决。不要使用
Spacer
(例如Spacer(1, 0.2*cm)
)作为Paragraph
的分隔符>。相反,请在ParagraphStyle
中定义spaceBefore
和spaceAfter
,例如:Solved. Don't use
Spacer
(e.g.Spacer(1, 0.2*cm)
) as a separator forParagraph
. Instead, definespaceBefore
andspaceAfter
inParagraphStyle
, for example: