【问题标题】:Splitting a ReportLab Flowable拆分 ReportLab Flowable
【发布时间】:2016-05-03 13:05:35
【问题描述】:

我正在尝试为 ReportLab 编写一个 Flowable,它需要能够拆分。根据我对文档的理解,我需要定义一个函数 split(self, aW, aH) 来拆分可流动的。但是,我遇到了以下我无法解决的错误。

一个简单的Flowable:

class MPGrid (Flowable):
def __init__(self, height=1*cm, width=None):
    self.height = height
    self.width = None

def wrap(self, aW, aH):
    self.width = aW
    return (aW, self.height)

def split(self, aW, aH):
    if aH >= self.height:
        return [self]
    else:
        return [MPGrid(aH, aW), MPGrid(self.height - aH, None)]

def draw(self):
    if not self.width:
        from reportlab.platypus.doctemplate import LayoutError
        raise LayoutError('No Width Defined')

    c = self.canv
    c.saveState()
    c.rect(0, 0, self.width, self.height)
    c.restoreState()

在文档中使用并需要拆分时,会产生以下错误:

reportlab.platypus.doctemplate.LayoutError: Splitting error(n==2) on page 4 in
<MPGrid at 0x102051c68 frame=col1>...
S[0]=<MPGrid at 0x102043ef0 frame=col1>...

这个flowable应该是固定高度的,如果对于可用高度来说太大了,就拆分消耗掉这个高度,然后在下一帧提醒固定高度。

我做错了什么导致这个不太有用的错误?

【问题讨论】:

    标签: python reportlab


    【解决方案1】:

    经过相当多的测试,我已经找到了答案。如果有人有更好的解决方案,我仍然愿意接受。

    发生这种情况是因为 split 被调用了两次,第二次是当可用高度为零(或接近零)时,您尝试创建一个高度(接近)为零的流动对象。解决办法是检查这种情况,在这种情况下不能拆分。

    下面的修改后的代码还有一些其他的小改动,以使代码更“完整”。

    class MPGrid (Flowable):
    def __init__(self, height=None, width=None):
        self.height = height
        self.width = width
    
    def wrap(self, aW, aH):
        if not self.width:  self.width = aW
        if not self.height: self.height = aH
    
        return (self.width, self.height)
    
    def split(self, aW, aH):
        if aH >= self.height:
            return [self]
        else:
            # if not aH == 0.0: (https://www.python.org/dev/peps/pep-0485)
            if not abs(aH - 0.0) <= max(1e-09 * max(abs(aH), abs(0.0)), 0.0):
                return [MPGrid(aH), MPGrid(self.height - aH, None)]
            else:
                return []   # Flowable Not Splittable
    
    def draw(self):
        if not self.width or not self.height:
            from reportlab.platypus.doctemplate import LayoutError
            raise LayoutError('Invalid Dimensions')
    
        c = self.canv
        c.saveState()
        c.rect(0, 0, self.width, self.height)
        c.restoreState()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多