2016-05-03 11 views
0

分割が可能なReportLabのためのFlowableを作成しようとしています。ドキュメンテーションの私の理解に基づいて、私はフローティングを分割する関数split(self, aW, aH)を定義する必要があります。しかし、私は解決できない次のエラーを受けています。ReportLabをフローティングに分割する

シンプルフロアブル:

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>... 

このフロアブルは、固定の高さであるべきであり、それが大きすぎる場合使用可能な高さについては、高さを消費するように分割され、次のフレームで固定高さのアラームが消費されます。

私は間違っていますが、これはあまり役に立たないエラーの原因です。

答えて

0

かなり多くのテストで、私はこれに対する答えを見つけました。私はまだ誰かがいる場合、より良い解決策には開いています。

これは、splitが2回呼び出されるため、使用可能な高さがゼロ(またはゼロに近い)の2回目で、高さがゼロに近いフローライブルを作成しようとしています。解決策はこのケースをチェックし、この場合分割できないようにすることです。

以下の改訂コードでは、コードをより完全なものにするために、わずかな変更が加えられています。

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() 
関連する問題