Re: Patch to support tables with oversize cells

Robin Becker <[email protected]> Fri, 11 Mar 2022 12:33:32 +0000
Newsgroups gmane.comp.python.reportlab.user
Message-ID <[email protected]>
Hi Lennart,

tried the latest patch and it seems better for backgrounds etc etc. See the attached test script which I used to see 
what was happening visually some examples are seen with

python lennart-example.py --split=36 --splitByRow=1 --splitInRow=1 --pdfn=sss.pdf
python lennart-example.py --split=58 --splitByRow=1 --splitInRow=1 --pdfn=sss.pdf
python lennart-example.py --split=58 --splitByRow=0 --splitInRow=1 --pdfn=sss.pdf

I haven't run this version entirely though the tests yet, but I will try over the weekend.

As for small tables:
   widow is end of something alone at the start of a page. A widow has a past, but no future.
   orphan is start of something alone at the end of a page. An orphan has no past, but has a future.
....
thanks again for the effort
-- 
Robin Becker
lennart-example.py (text/x-python, 3.7 KB)
def main():
    from reportlab.lib import colors
    from reportlab.lib.units import inch
    from reportlab.platypus import Paragraph, Table, SimpleDocTemplate, Spacer, HRFlowable
    from reportlab.lib.styles import getSampleStyleSheet
    import os, sys, getopt
    try:
        opts, args = getopt.getopt(sys.argv[1:], '', 'splitByRow= splitInRow= split= pdfn='.split())
    except getopt.GetoptError as err:
        # print help information and exit:
        print(err)
        print(f'Usage {os.path.basename(sys.argv[0])} [--splitByRow=num] [--splitInRow=num] [--split=num] --pdfn=')
        sys.exit(2)
    else:
        splitByRow = 1
        splitInRow = 0
        split = 36
        pdfn = None
        for k, v in opts:
            if k=='--splitByRow':
                splitByRow = int(v)
            elif k=='--splitInRow':
                splitInRow = int(v)
            elif k=='--split':
                split = float(v)
                if int(split)==split: split=int(split)
            elif k=='--pdfn':
                pdfn =  v
            else:
                raise ValueError(f'Unknown option name {k}={v}')

    class IndicatorTable(Table):
        def draw(self):
            Table.draw(self)
            c = self.canv
            x = self._width
            y = self._height - split
            c.setStrokeColor(colors.toColor('red'))
            c.setLineWidth(0.5)
            c.setDash([2,2])
            c.line(0,y,x+13,y)

    story = []
    storyAdd = story.append
    styleSheet = getSampleStyleSheet()
    btStyle = styleSheet['BodyText']
    data=  [['00', '01', '02', '03', '04'],
            ['10', '11', '12', '13', '14'],
            ['20', '21', '22', '23', '24'],
            ['30', '31', '32', '33', '34']]
    data=  [['00\n\naa', '01', '02', '03', '04'],
            ['10', '11\nbb', '12', '13', '14'],
            ['20', '21', '22\ncc', '23', '24'],
            ['30', '31', '32', '33\ndd', '34']]
    def makeTable(klass=Table):
        return klass(data,style=[
                    ('GRID',(0,0),(-1,-1),0.5,colors.grey),
                    ('GRID',(1,1),(-2,-2),1,colors.green),
                    ('BOX',(0,0),(1,-1),2,colors.red),
                    ('BOX',(0,0),(-1,-1),2,colors.black),
                    ('LINEABOVE',(1,2),(-2,2),1,colors.blue),
                    ('LINEBEFORE',(2,1),(2,-2),1,colors.pink),
                    ('BACKGROUND', (0, 0), (0, 1), colors.pink),
                    ('BACKGROUND', (1, 1), (1, 2), colors.lavender),
                    ('BACKGROUND', (2, 2), (2, 3), colors.orange),
                    ('TEXTCOLOR',(0,-1),(-2,-1),colors.green),
                    ],
                    splitInRow=splitInRow,
                    splitByRow=splitByRow,
                    )
    storyAdd(Paragraph("Illustrating splits: nosplit", btStyle))
    storyAdd(makeTable(klass=IndicatorTable))
    storyAdd(Spacer(0,6))
    def addSplitTable(size=30):
        t = makeTable()
        S = t.split(4*inch,size)
        if not S:
            storyAdd(Paragraph(f"<span color=red>Illustrating splits failed</span>: split(4in,{size}) {splitByRow=} {splitInRow=}", btStyle))
            storyAdd(Spacer(0,6))
            #print('!!!!! Failed')
        else:
            #print('##### OK')
            storyAdd(Paragraph(f"Illustrating splits: split(4in,{size}) {splitByRow=} {splitInRow=}", btStyle))
            storyAdd(Spacer(0,6))
            for s in S:
                storyAdd(s)
                storyAdd(Spacer(0,6))
    addSplitTable(split)

    pdfn = pdfn or f'{os.path.basename(os.path.splitext(sys.argv[0])[0])}-{splitByRow!s}-{splitInRow!s}-{split!s}.pdf'
    doc = SimpleDocTemplate(pdfn, showBoundary=0)
    doc.build(story)
    print(f'Saved {pdfn}')

if __name__=='__main__':
    main()
sss.pdf (application/pdf, 2.3 KB) - not displayed