예제 #1
0
 def subStory(texts,fbg=0):
     style = [
         ('VALIGN',(0,0),(-1,-1),'TOP'),
         ('INNERGRID', (0,0), (-1,-1), 0.25, black),
         ('BOX', (0,0), (-1,-1), 0.25, black),
         ]
     if fbg:
         fbg1 = [FrameBG(start=0,color=toColor('limegreen'))]
         fbg0 = [FrameBG(start=0)]
     else:
         fbg0 = fbg1 = []
     return ([Paragraph(t,bt) for t in texts]
             +fbg1+[Table([('alignment', a.lower())],style = style,hAlign=a)
                 for a in ('LEFT','RIGHT','CENTER')]+fbg0)
def run():
    from reportlab.platypus import  BaseDocTemplate, PageTemplate, Image, Frame, PageTemplate, \
                                    ShowBoundaryValue, SimpleDocTemplate, FrameBG, Paragraph, \
                                    FrameBreak
    from reportlab.lib.colors import toColor
    from reportlab.lib.utils import haveImages, _RL_DIR, rl_isfile, open_for_read, fileName2FSEnc, asNative
    from reportlab.lib.styles import getSampleStyleSheet
    styleSheet = getSampleStyleSheet()
    if haveImages:
        _GIF = os.path.join(testsFolder, 'pythonpowered.gif')
        if not rl_isfile(_GIF): _GIF = None
        _GAPNG = os.path.join(testsFolder, 'gray-alpha.png')
        if not rl_isfile(_GAPNG): _GAPNG = None
    else:
        _GIF = None
    if _GIF: _GIFFSEnc = fileName2FSEnc(_GIF)
    if _GAPNG: _GAPNGFSEnc = fileName2FSEnc(_GAPNG)

    _JPG = os.path.join(testsFolder, '..', 'docs', 'images', 'lj8100.jpg')
    if not rl_isfile(_JPG): _JPG = None

    doc = SimpleDocTemplate(outputfile('test_platypus_images.pdf'))
    story = [
        FrameBG(color=toColor('lightblue'), start='frame-permanent'),
    ]
    if _GIF:
        story.append(
            Paragraph(
                "Here is an Image flowable obtained from a string GIF filename.",
                styleSheet['Italic']))
        story.append(Image(_GIF))
        story.append(
            Paragraph(
                "Here is an Image flowable obtained from a utf8 GIF filename.",
                styleSheet['Italic']))
        #story.append(Image(fileName2FSEnc(_GIF)))
        story.append(
            Paragraph(
                "Here is an Image flowable obtained from a string GIF file url.",
                styleSheet['Italic']))
        story.append(Image(getFurl(_GIF)))
        story.append(
            Paragraph(
                "Here is an Image flowable obtained from an open GIF file.",
                styleSheet['Italic']))
        story.append(Image(open_for_read(_GIF, 'b')))
        story.append(FrameBreak())
        img = Image('http://www.reportlab.com/rsrc/encryption.gif')
        story.append(
            Paragraph(
                "Here is an Image flowable obtained from a string GIF http url.",
                styleSheet['Italic']))
        story.append(img)
        story.append(FrameBreak())

    if _GAPNG:
        story.append(
            Paragraph(
                "Here is an Image flowable obtained from a string PNGA filename.",
                styleSheet['Italic']))
        story.append(Image('rltw-icon-tr.png'))
        story.append(
            Paragraph(
                "Here is an Image flowable obtained from a string PNG filename.",
                styleSheet['Italic']))
        story.append(Image(_GAPNG))
        story.append(
            Paragraph(
                "Here is an Image flowable obtained from a utf8 PNG filename.",
                styleSheet['Italic']))
        #story.append(Image(fileName2FSEnc(_GAPNG)))
        story.append(
            Paragraph(
                "Here is an Image flowable obtained from a string file PNG url.",
                styleSheet['Italic']))
        story.append(Image(getFurl(_GAPNG)))
        story.append(
            Paragraph(
                "Here is an Image flowable obtained from an open PNG file.",
                styleSheet['Italic']))
        story.append(Image(open_for_read(_GAPNG, 'b')))
        story.append(FrameBreak())

    if _JPG:
        img = Image(_JPG)
        story.append(
            Paragraph(
                "Here is an JPEG Image flowable obtained from a JPEG filename.",
                styleSheet['Italic']))
        story.append(img)
        story.append(
            Paragraph(
                "Here is an JPEG Image flowable obtained from an open JPEG file.",
                styleSheet['Italic']))
        img = Image(open_for_read(_JPG, 'b'))
        story.append(img)
        story.append(FrameBreak())
    doc.build(story)
예제 #3
0
def run():
    doc = SimpleDocTemplate(outputfile('test_platypus_tables.pdf'), pagesize=(8.5*inch, 11*inch), showBoundary=1)
    lst = []
    from reportlab import Version
    styNormal = styleSheet['Normal']
    styBackground = ParagraphStyle('background', parent=styNormal, backColor=colors.pink)
    styH1 = styleSheet['Heading1']
    lst.append(FrameBG(color=colors.red))
    lst.append(Paragraph("First, a test of how tables align their content...", styH1))
    lst.append(Paragraph("""Generated with version %s""" % Version,
                        styNormal))
    lst.append(Paragraph("""In release 2.3, cells with plain text positioned their
                         text differently to cells with Paragraphs using the
                         same font.  Hopefully now they are back on the same baseline""",
                        styNormal))
    lst.append(FrameBG(color=colors.blue))
    ts1 = TableStyle([
                ('ALIGN', (0,0), (-1,0), 'RIGHT'),
                ('BACKGROUND', (0,0), (-1,0), colors.lightgrey),
                ('VALIGN', (0,0), (-1,-1), 'TOP'),
                ('GRID', (0,0), (-1,-1), 0.25, colors.black),
                    ])
    t1 = Table([
        ('plain text','plain text','shortpara','plain text', 'long para'),
        ('Text','more text', Paragraph('Is this para level?', styBackground), 'Back to text', Paragraph('Short para again', styBackground)),
        ('Text',
            'more text',
            Paragraph('Is this level?', styBackground),
            'This is plain\ntext with line breaks\nto compare against\nthe para on right',
            Paragraph('Long paragraph we expect to wrap over several lines accurately', styBackground)),

        ])
    t1.setStyle(ts1)
    lst.append(t1)
    lst.append(FrameBG(start=False))
    lst.append(Spacer(0,10))
    lst.append(Paragraph("Now we make a table with just one cell containing a string...note how the text sits low", styNormal))
    lst.append(FrameBG(start=False))

    tsGrid = TableStyle([
                ('GRID', (0,0), (-1,-1), 0.25, colors.black),
                    ])
    lst.append(Table([['One cell of plain text']], style=tsGrid, colWidths=[200]))

    lst.append(Spacer(0,10))
    lst.append(Paragraph("Now we make a table with just one cell containing a para...should be same position.  Note that the overall bounding box is an approximation and lies - it always did.", styNormal))
    lst.append(Table([[Paragraph('One cell containing a paragraph.  ÄÉ∫', styBackground)]], style=tsGrid, colWidths=[200]))

    lst.append(Spacer(0,10))
    lst.append(Paragraph("Paragraphs jumped up post 2.1.  Ideally they should align the same.", styNormal))


    lst.append(Spacer(0,30))
    lst.append(Paragraph("Now for all the tests we had before.  See also the much longer test_platypus_tables_2.pdf, which for reasons unknown was split into a separate file generated by the same script", styNormal))

    styles = makeStyles()
    for style in styles:
        t = getTable()
        t.setStyle(style)
##        print '--------------'
##        for rowstyle in t._cellstyles:
##            for s in rowstyle:
##                print s.alignment
        lst.append(t)
        lst.append(Spacer(0,12))

    t=Table([['VERTICAL Gradient Red top, grey bottom','Horizontal Gradient Blue left, green right'],
             ['HORIZONTAL Gradient Span grey left red right', ''],
             ['VERTICAL Gradiant Span Blue top green bottom',''],
             ['','CLEAR']],[3.5*inch, 2.7*inch])
    style=TableStyle([
    ('SPAN', (0,1),(1,1)),
    ('SPAN', (0,2),(0,3)),
    ('BACKGROUND',(0,0), (0,0),['VERTICAL', colors.grey, colors.red]),
    ('BACKGROUND',(1,0), (1,0),['HORIZONTAL', colors.blue, colors.green]),
    ('BACKGROUND',(0,1), (1,1),['HORIZONTAL', colors.grey, colors.red]),
    ('BACKGROUND',(0,2), (0,3),['VERTICAL', colors.blue, colors.green])
    ])
    t.setStyle(style)
    lst.append(t)

    #illustrate usage of minRowHeights idea from Jon Hinton inivatajon @ bitbucket.org
    t=Table([['VERTICAL Red --> grey  minRowHeights[0]=30','Horizontal Gradient Blue left, green right'],
             ['HORIZONTAL Gradient Span grey left red right', ''],
             ['VERTICAL Gradiant Span Blue top green bottom',''],
             ['','CLEAR']],[3.5*inch, 2.7*inch],minRowHeights=(30,), spaceBefore=15)
    style=TableStyle([
    ('SPAN', (0,1),(1,1)),
    ('SPAN', (0,2),(0,3)),
    ('VALIGN', (0,0),(-1,0),'MIDDLE'),
    ('BACKGROUND',(0,0), (0,0),['VERTICAL', colors.grey, colors.red]),
    ('BACKGROUND',(1,0), (1,0),['HORIZONTAL', colors.blue, colors.green]),
    ('BACKGROUND',(0,1), (1,1),['HORIZONTAL', colors.grey, colors.red]),
    ('BACKGROUND',(0,2), (0,3),['VERTICAL', colors.blue, colors.green])
    ])
    t.setStyle(style)

    t=Table([],[3.5*inch, 2.7*inch],minRowHeights=(30,), spaceBefore=15, style=style, emptyTableAction='ignore')
    lst.append(t)
    doc.build(lst)
예제 #4
0
    def test0(self):
        "IndentTestCase test0"
        if rl_invariant: random.seed(1479316371)

        # Build story.
        story = []
        doc = MyDocTemplate(outputfile('test_platypus_indents.pdf'))
        storyAdd = story.append

        styleSheet = getSampleStyleSheet()
        h1 = styleSheet['Heading1']
        h1.spaceBefore = 18
        bt = styleSheet['BodyText']
        bt.spaceBefore = 6

        storyAdd(Paragraph('Test of context-relative indentation', h1))

        storyAdd(Spacer(18, 18))

        storyAdd(Indenter(0, 0))
        storyAdd(
            Paragraph(
                "This should be indented 0 points at each edge. " +
                ("spam " * 25), bt))
        storyAdd(Indenter(0, 0))

        storyAdd(Indenter(36, 0))
        storyAdd(
            Paragraph(
                "This should be indented 36 points at the left. " +
                ("spam " * 25), bt))
        storyAdd(Indenter(-36, 0))

        storyAdd(Indenter(0, 36))
        storyAdd(
            Paragraph(
                "This should be indented 36 points at the right. " +
                ("spam " * 25), bt))
        storyAdd(Indenter(0, -36))

        storyAdd(Indenter(36, 36))
        storyAdd(
            Paragraph(
                "This should be indented 36 points at each edge. " +
                ("spam " * 25), bt))
        storyAdd(Indenter(36, 36))
        storyAdd(
            Paragraph(
                "This should be indented a FURTHER 36 points at each edge. " +
                ("spam " * 25), bt))
        storyAdd(Indenter(-72, -72))

        storyAdd(
            Paragraph(
                "This should be back to normal at each edge. " +
                ("spam " * 25), bt))

        storyAdd(Indenter(36, 36))
        storyAdd(
            Paragraph(("""This should be indented 36 points at the left
        and right.  It should run over more than one page and the indent should
        continue on the next page. """ + (random.randint(0, 10) * 'x') + ' ') *
                      20, bt))
        storyAdd(Indenter(-36, -36))

        storyAdd(NextPageTemplate('updown'))
        storyAdd(FrameBreak())
        storyAdd(Paragraph('Another test of context-relative indentation', h1))
        storyAdd(NextPageTemplate(
            'normal'))  # so NEXT page is different template...
        storyAdd(
            Paragraph(
                """This time we see if the indent level is continued across
            frames...this page has 2 frames, let's see if it carries top to bottom. Then
            onto a totally different template.""", bt))

        storyAdd(Indenter(0, 0))
        storyAdd(
            Paragraph(
                "This should be indented 0 points at each edge. " +
                ("spam " * 25), bt))
        storyAdd(Indenter(0, 0))
        storyAdd(Indenter(36, 72))
        storyAdd(
            Paragraph(("""This should be indented 36 points at the left
        and 72 at the right.  It should run over more than one frame and one page, and the indent should
        continue on the next page. """ + (random.randint(0, 10) * 'x') + ' ') *
                      35, bt))

        storyAdd(Indenter(-36, -72))
        storyAdd(
            Paragraph(
                "This should be back to normal at each edge. " +
                ("spam " * 25), bt))
        storyAdd(PageBreak())

        storyAdd(PageBreak())
        storyAdd(
            Paragraph(
                "Below we should colour the background lightgreen and have a red border",
                bt))
        storyAdd(
            FrameBG(start=True,
                    color=lightgreen,
                    strokeColor=toColor('red'),
                    strokeWidth=1))
        storyAdd(Paragraph("We should have a light green background here", bt))
        storyAdd(Paragraph("We should have a light green background here", bt))
        storyAdd(Paragraph("We should have a light green background here", bt))
        storyAdd(Spacer(6, 6))
        storyAdd(FrameBG(start=False))

        storyAdd(
            Paragraph("Below we should colour the background lightgreen", bt))
        storyAdd(
            FrameBG(start=True,
                    color=lightgreen,
                    strokeColor=toColor('red'),
                    strokeWidth=None))
        storyAdd(Paragraph("We should have a light green background here", bt))
        storyAdd(Paragraph("We should have a light green background here", bt))
        storyAdd(Paragraph("We should have a light green background here", bt))
        storyAdd(Spacer(6, 6))
        storyAdd(FrameBG(start=False))

        storyAdd(
            Paragraph(
                "Below we split to two new frames with dark green borders",
                bt))
        storyAdd(FrameSplitter('templateX', ['XF4', 'XF5'],
                               adjustHeight=False))
        storyAdd(
            FrameBG(start=True,
                    color=lightgreen,
                    strokeColor=toColor('red'),
                    strokeWidth=1))
        for i in range(15):
            storyAdd(
                Paragraph(
                    "We should have a light green background here %d" % i, bt))
        storyAdd(Spacer(6, 6))
        storyAdd(FrameBG(start=False))
        storyAdd(NextPageTemplate('normal'))

        storyAdd(PageBreak())
        storyAdd(
            Paragraph("Below we should colour the background lightgreen", bt))
        storyAdd(FrameBG(start="frame", color=lightgreen))
        storyAdd(Paragraph("We should have a light green background here", bt))

        storyAdd(PageBreak())
        storyAdd(Paragraph("Here we should have no background.", bt))

        storyAdd(PageBreak())
        storyAdd(FrameBG(start="frame", color=lightblue))
        storyAdd(
            Paragraph(
                "We should have a light blue background here and the whole frame should be filled in.",
                bt))

        storyAdd(PageBreak())
        storyAdd(Paragraph("Here we should have no background again.", bt))

        storyAdd(
            Paragraph("Below we should colour the background lightgreen", bt))
        storyAdd(FrameBG(start="frame-permanent", color=lightgreen))
        storyAdd(Paragraph("We should have a light green background here", bt))

        storyAdd(PageBreak())
        storyAdd(
            Paragraph("Here we should still have a lightgreen background.",
                      bt))

        storyAdd(PageBreak())
        storyAdd(FrameBG(start="frame", color=lightblue, left=36, right=36))
        storyAdd(
            Paragraph("We should have a lighgreen/lightblue background.", bt))

        storyAdd(PageBreak())
        storyAdd(
            Paragraph("Here we should have only light green background.", bt))

        doc.multiBuild(story)
def run():
    doc = SimpleDocTemplate(outputfile('test_platypus_tables.pdf'),
                            pagesize=(8.5 * inch, 11 * inch),
                            showBoundary=1)
    lst = []
    from reportlab import Version
    styNormal = styleSheet['Normal']
    styBackground = ParagraphStyle('background',
                                   parent=styNormal,
                                   backColor=colors.pink)
    styH1 = styleSheet['Heading1']
    lst.append(FrameBG(color=colors.red))
    lst.append(
        Paragraph("First, a test of how tables align their content...", styH1))
    lst.append(Paragraph("""Generated with version %s""" % Version, styNormal))
    lst.append(
        Paragraph(
            """In release 2.3, cells with plain text positioned their
                         text differently to cells with Paragraphs using the
                         same font.  Hopefully now they are back on the same baseline""",
            styNormal))
    lst.append(FrameBG(color=colors.blue))
    ts1 = TableStyle([
        ('ALIGN', (0, 0), (-1, 0), 'RIGHT'),
        ('BACKGROUND', (0, 0), (-1, 0), colors.lightgrey),
        ('VALIGN', (0, 0), (-1, -1), 'TOP'),
        ('GRID', (0, 0), (-1, -1), 0.25, colors.black),
    ])
    t1 = Table([
        ('plain text', 'plain text', 'shortpara', 'plain text', 'long para'),
        ('Text', 'more text', Paragraph('Is this para level?',
                                        styBackground), 'Back to text',
         Paragraph('Short para again', styBackground)),
        ('Text', 'more text', Paragraph('Is this level?', styBackground),
         'This is plain\ntext with line breaks\nto compare against\nthe para on right',
         Paragraph(
             'Long paragraph we expect to wrap over several lines accurately',
             styBackground)),
    ])
    t1.setStyle(ts1)
    lst.append(t1)
    lst.append(FrameBG(start=False))
    lst.append(Spacer(0, 10))
    lst.append(
        Paragraph(
            "Now we make a table with just one cell containing a string...note how the text sits low",
            styNormal))
    lst.append(FrameBG(start=False))

    tsGrid = TableStyle([
        ('GRID', (0, 0), (-1, -1), 0.25, colors.black),
    ])
    lst.append(
        Table([['One cell of plain text']], style=tsGrid, colWidths=[200]))

    lst.append(Spacer(0, 10))
    lst.append(
        Paragraph(
            "Now we make a table with just one cell containing a para...should be same position.  Note that the overall bounding box is an approximation and lies - it always did.",
            styNormal))
    lst.append(
        Table([[
            Paragraph('One cell containing a paragraph.  ÄÉ∫',
                      styBackground)
        ]],
              style=tsGrid,
              colWidths=[200]))

    lst.append(Spacer(0, 10))
    lst.append(
        Paragraph(
            "Paragraphs jumped up post 2.1.  Ideally they should align the same.",
            styNormal))

    lst.append(Spacer(0, 30))
    lst.append(
        Paragraph(
            "Now for all the tests we had before.  See also the much longer test_platypus_tables_2.pdf, which for reasons unknown was split into a separate file generated by the same script",
            styNormal))

    styles = makeStyles()
    for style in styles:
        t = getTable()
        t.setStyle(style)
        ##        print '--------------'
        ##        for rowstyle in t._cellstyles:
        ##            for s in rowstyle:
        ##                print s.alignment
        lst.append(t)
        lst.append(Spacer(0, 12))
    doc.build(lst)
    def test0(self):
        "This makes one long multi-page paragraph."

        # Build story.
        story = []

        styleSheet = getSampleStyleSheet()
        h1 = styleSheet['Heading1']
        h1.spaceBefore = 18
        bt = styleSheet['BodyText']
        bt.spaceBefore = 6

        story.append(Paragraph('Test of context-relative indentation', h1))

        story.append(Spacer(18, 18))

        story.append(Indenter(0, 0))
        story.append(
            Paragraph(
                "This should be indented 0 points at each edge. " +
                ("spam " * 25), bt))
        story.append(Indenter(0, 0))

        story.append(Indenter(36, 0))
        story.append(
            Paragraph(
                "This should be indented 36 points at the left. " +
                ("spam " * 25), bt))
        story.append(Indenter(-36, 0))

        story.append(Indenter(0, 36))
        story.append(
            Paragraph(
                "This should be indented 36 points at the right. " +
                ("spam " * 25), bt))
        story.append(Indenter(0, -36))

        story.append(Indenter(36, 36))
        story.append(
            Paragraph(
                "This should be indented 36 points at each edge. " +
                ("spam " * 25), bt))
        story.append(Indenter(36, 36))
        story.append(
            Paragraph(
                "This should be indented a FURTHER 36 points at each edge. " +
                ("spam " * 25), bt))
        story.append(Indenter(-72, -72))

        story.append(
            Paragraph(
                "This should be back to normal at each edge. " +
                ("spam " * 25), bt))

        story.append(Indenter(36, 36))
        story.append(
            Paragraph(("""This should be indented 36 points at the left
        and right.  It should run over more than one page and the indent should
        continue on the next page. """ + (random.randint(0, 10) * 'x') + ' ') *
                      20, bt))
        story.append(Indenter(-36, -36))

        story.append(NextPageTemplate('updown'))
        story.append(FrameBreak())
        story.append(
            Paragraph('Another test of context-relative indentation', h1))
        story.append(NextPageTemplate(
            'normal'))  # so NEXT page is different template...
        story.append(
            Paragraph(
                """This time we see if the indent level is continued across
            frames...this page has 2 frames, let's see if it carries top to bottom. Then
            onto a totally different template.""", bt))

        story.append(Indenter(0, 0))
        story.append(
            Paragraph(
                "This should be indented 0 points at each edge. " +
                ("spam " * 25), bt))
        story.append(Indenter(0, 0))
        story.append(Indenter(36, 72))
        story.append(
            Paragraph(("""This should be indented 36 points at the left
        and 72 at the right.  It should run over more than one frame and one page, and the indent should
        continue on the next page. """ + (random.randint(0, 10) * 'x') + ' ') *
                      35, bt))

        story.append(Indenter(-36, -72))
        story.append(
            Paragraph(
                "This should be back to normal at each edge. " +
                ("spam " * 25), bt))
        story.append(PageBreak())

        story.append(PageBreak())
        story.append(
            Paragraph("Below we should colour the background lightgreen", bt))
        story.append(FrameBG(start=True, color=lightgreen))
        story.append(
            Paragraph("We should have a light green background here", bt))
        story.append(
            Paragraph("We should have a light green background here", bt))
        story.append(
            Paragraph("We should have a light green background here", bt))
        story.append(FrameBG(start=False))

        story.append(PageBreak())
        story.append(
            Paragraph("Below we should colour the background lightgreen", bt))
        story.append(FrameBG(start="frame", color=lightgreen))
        story.append(
            Paragraph("We should have a light green background here", bt))

        story.append(PageBreak())
        story.append(Paragraph("Here we should have no background.", bt))

        story.append(PageBreak())
        story.append(FrameBG(start="frame", color=lightblue))
        story.append(
            Paragraph(
                "We should have a light blue background here and the whole frame should be filled in.",
                bt))

        story.append(PageBreak())
        story.append(Paragraph("Here we should have no background again.", bt))

        story.append(
            Paragraph("Below we should colour the background lightgreen", bt))
        story.append(FrameBG(start="frame-permanent", color=lightgreen))
        story.append(
            Paragraph("We should have a light green background here", bt))

        story.append(PageBreak())
        story.append(
            Paragraph("Here we should still have a lightgreen background.",
                      bt))

        story.append(PageBreak())
        story.append(FrameBG(start="frame", color=lightblue, left=36,
                             right=36))
        story.append(
            Paragraph("We should have a lighgreen/lightblue background.", bt))

        story.append(PageBreak())
        story.append(
            Paragraph("Here we should have only light green background.", bt))

        doc = MyDocTemplate(outputfile('test_platypus_indents.pdf'))
        doc.multiBuild(story)