【问题标题】:ReportLab: Flowable too large on page 1 in frame 'normal' of template 'First'ReportLab:模板“First”的“正常”框架中的第 1 页上的 Flowable 太大
【发布时间】:2015-03-13 19:11:14
【问题描述】:

我使用 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 会正确拆分长段落。

我该如何处理?

【问题讨论】:

    标签: python reportlab


    【解决方案1】:

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

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

    【讨论】:

    • 对于未来的读者:完整的模块路径是:reportlab.platypus.KeepTogether
    • 快速更新 Moshe 的评论,截至 2021 年 7 月(Reportlab 版本 3.5.20)导入来自 reportlab.platypus.flowables import KeepTogether
    【解决方案2】:

    已解决。 不要使用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)
    

    【讨论】:

    • 我面临同样的问题,很少有测试表明 Spacer 似乎不是问题所在。请不要让用户害怕使用它,除非您的假设背后有其他依据。
    猜你喜欢
    • 2010-12-22
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 2016-01-02
    相关资源
    最近更新 更多