ReportLab:在第一页的“正常”模板'第一'中可流动太大

时间:2011-10-09 02:52:27

标签: python reportlab

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

我该如何处理?

2 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:-2)

已解决。请勿使用Spacer(例如Spacer(1, 0.2*cm))作为Paragraph的分隔符。相反,请在spaceBefore中定义spaceAfterParagraphStyle,例如:

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