Example #1
0
 def _print_reference(self):
     index = SimpleIndex()
     index.setup(self._styles['root'])
     
     self._story.append(NextPageTemplate('SimpleIndex'))
     self._story.append(PageBreak())
     self._story.append(RLParagraph(self.TITLE_INDEX, self._styles['h3']))
     self._story.append(index)
     
     return index.getCanvasMaker()
Example #2
0
    def _print_reference(self):
        index = SimpleIndex()
        index.setup(self._styles['root'])

        self._story.append(NextPageTemplate('SimpleIndex'))
        self._story.append(PageBreak())
        self._story.append(RLParagraph(self.TITLE_INDEX, self._styles['h3']))
        self._story.append(index)

        return index.getCanvasMaker()
Example #3
0
    def test0(self):
        '''
        Test case for Indexes. This will draw an index %sat the end of the
        document with dots seperating the indexing terms from the page numbers.
        Index terms are grouped by their first 2, and first 3 characters.
        The page numbers should be clickable and link to the indexed word.
        '''
        # Build story.
        
        for headers in False, True:
            path = outputfile('test_platypus_index%s.pdf' % (headers and '_headers' or ''))
            doc = MyDocTemplate(path)
            story = []
            styleSheet = getSampleStyleSheet()
            bt = styleSheet['BodyText']
    
            description = '<font color=red>%s</font>' % (self.test0.__doc__  % (headers and 'with alphabetic headers ' or ''))
            story.append(Paragraph(description, bt))
            index = SimpleIndex(dot=' . ', headers=headers)

            def addParas(words):
                words = [asUnicode(w) for w in words]
                txt = u' '.join([(len(w) > 5 and u'<index item=%s/>%s' % (quoteattr(commajoin([w[:2], w[:3], w])), w) or w) for w in words])
                para = Paragraph(txt, makeBodyStyle())
                story.append(para)
    
            for i in xrange(20):
                addParas(randomtext.randomText(randomtext.PYTHON, 5).split(' '))
            addParas([u+w for u in u'E\xc8\xc9\xca\xcb' for w in (u'erily',u'asily')])
            addParas([u+w for u in u'A\xc0\xc4\xc1\xc3\xc2' for w in (u'dvance',u'ttend')])
            addParas([u+w for u in u'O\xd2\xd6\xd3\xd2' for w in (u'rdinary',u'verflow')])
            addParas([u+w for u in u'U\xd9\xdc\xdb' for w in (u'ndertow',u'nbeliever')])
            addParas([u+w for u in u'e\xe8\xea\xeb\xe9' for w in (u'ventide',u'lision')])
            addParas([u+w for u in u'o\xf2\xf5\xf3\xf4' for w in (u'verture',u'ntology')])

            #test ampersand in index term
            txt = '\nMarks &amp; Spencer - purveyors of fine groceries, underwear and ampersands - should have their initials displayed however they were input.\n<index item="M&amp;S,groceries"/><index item="M&amp;S,underwear"/><index item="M&amp;S,ampersands"/>'
            para = Paragraph(txt, makeBodyStyle())
            story.append(para)
        

            story.append(index)
    
            doc.build(story, canvasmaker=index.getCanvasMaker())
Example #4
0
def simple_index():
    doc = SimpleDocTemplate("simple_index.pdf", pagesize=letter)
    styles = getSampleStyleSheet()

    flowables = []

    ptext = """I'm a custom <index item="bulletted"/>bulletted paragraph"""
    para = Paragraph(ptext, style=styles["Normal"], bulletText='-')
    flowables.append(para)
    flowables.append(PageBreak())

    ptext = """<index item="Python"/>Python is an indexed word"""
    para = Paragraph(ptext, style=styles["Normal"], bulletText='-')
    flowables.append(para)

    index = SimpleIndex()
    flowables.append(PageBreak())
    flowables.append(index)

    doc.build(flowables, canvasmaker=index.getCanvasMaker())
    def test0(self):
        """
        Test case for Indexes. This will draw an index %sat the end of the
        document with dots seperating the indexing terms from the page numbers.
        Index terms are grouped by their first 2, and first 3 characters.
        The page numbers should be clickable and link to the indexed word.
        """
        # Build story.

        for headers in False, True:
            path = outputfile("test_platypus_index%s.pdf" % (headers and "_headers" or ""))
            doc = MyDocTemplate(path)
            story = []
            styleSheet = getSampleStyleSheet()
            bt = styleSheet["BodyText"]

            description = "<font color=red>%s</font>" % (
                self.test0.__doc__ % (headers and "with alphabetic headers " or "")
            )
            story.append(Paragraph(description, bt))
            index = SimpleIndex(dot=" . ", headers=headers)

            for i in range(20):
                words = randomtext.randomText(randomtext.PYTHON, 5).split(" ")
                txt = " ".join(
                    [
                        (len(w) > 5 and "<index item=%s/>%s" % (quoteattr(commajoin([w[:2], w[:3], w])), w) or w)
                        for w in words
                    ]
                )
                para = Paragraph(txt, makeBodyStyle())
                story.append(para)

            # test ampersand in index term
            txt = '\nMarks &amp; Spencer - purveyors of fine groceries, underwear and ampersands - should have their initials displayed however they were input.\n<index item="M&amp;S,groceries"/><index item="M&amp;S,underwear"/><index item="M&amp;S,ampersands"/>'
            para = Paragraph(txt, makeBodyStyle())
            story.append(para)

            story.append(index)

            doc.build(story, canvasmaker=index.getCanvasMaker())
Example #6
0
    def test0(self):
        '''
        Test case for Indexes. This will draw an index %sat the end of the
        document with dots seperating the indexing terms from the page numbers.
        Index terms are grouped by their first 2, and first 3 characters.
        The page numbers should be clickable and link to the indexed word.
        '''
        # Build story.

        for headers in False, True:
            path = outputfile('test_platypus_index%s.pdf' %
                              (headers and '_headers' or ''))
            doc = MyDocTemplate(path)
            story = []
            styleSheet = getSampleStyleSheet()
            bt = styleSheet['BodyText']

            description = '<font color=red>%s</font>' % (
                self.test0.__doc__ %
                (headers and 'with alphabetic headers ' or ''))
            story.append(Paragraph(description, bt))
            index = SimpleIndex(dot=' . ', headers=headers)

            for i in range(20):
                words = randomtext.randomText(randomtext.PYTHON, 5).split(' ')
                txt = ' '.join([(len(w) > 5 and '<index item=%s/>%s' %
                                 (quoteattr(commajoin([w[:2], w[:3], w])), w)
                                 or w) for w in words])
                para = Paragraph(txt, makeBodyStyle())
                story.append(para)

            #test ampersand in index term
            txt = '\nMarks &amp; Spencer - purveyors of fine groceries, underwear and ampersands - should have their initials displayed however they were input.\n<index item="M&S,groceries"/><index item="M&S,underwear"/><index item="M&amp;S,ampersands"/>'
            para = Paragraph(txt, makeBodyStyle())
            story.append(para)

            story.append(index)

            doc.build(story, canvasmaker=index.getCanvasMaker())
Example #7
0
def _test0(self):
    "This makes one long multi-page paragraph."
    from reportlab.platypus.flowables import DocAssign, DocExec, DocPara, DocIf, DocWhile

    # Build story.
    story = []

    styleSheet = getSampleStyleSheet()
    h1 = styleSheet['Heading1']
    h1.pageBreakBefore = 1
    h1.keepWithNext = 1
    h1.outlineLevel = 0

    h2 = styleSheet['Heading2']
    h2.backColor = colors.cyan
    h2.keepWithNext = 1
    h2.outlineLevel = 1

    bt = styleSheet['BodyText']

    story.append(Paragraph("""Cross-Referencing Test""", styleSheet["Title"]))
    story.append(
        Paragraph(
            """
        Subsequent pages test cross-references: indexes, tables and individual
        cross references.  The number in brackets at the end of each paragraph
        is its position in the story. (%d)""" % len(story), bt))

    story.append(Paragraph("""Table of Contents:""", styleSheet["Title"]))
    toc = TableOfContents()
    story.append(toc)

    chapterNum = 1
    for i in range(10):
        story.append(
            Paragraph(
                'Chapter %d: Chapters always starts a new page' % chapterNum,
                h1))
        chapterNum = chapterNum + 1
        for j in range(3):
            story.append(
                Paragraph(
                    'Heading1 paragraphs should always'
                    'have a page break before.  Heading 2 on the other hand'
                    'should always have a FRAME break before (%d)' %
                    len(story), bt))
            story.append(
                Paragraph(
                    'Heading 2 should always be kept with the next thing (%d)'
                    % len(story), h2))
            for j in range(3):
                story.append(
                    Paragraph(
                        randomText(theme=PYTHON, sentences=2) +
                        ' (%d)' % len(story), bt))
                story.append(
                    Paragraph(
                        'I should never be at the bottom of a frame (%d)' %
                        len(story), h2))
                story.append(
                    Paragraph(
                        randomText(theme=PYTHON, sentences=1) +
                        ' (%d)' % len(story), bt))
    story.append(Paragraph('The Index which goes at the back', h1))
    story.append(SimpleIndex())

    doc = MyDocTemplate(outputfile('test_platypus_xref.pdf'))
    doc.multiBuild(story)
Example #8
0
    def test2(self):
        "This makes one long multi-page paragraph in multi-pass for testing docWhile etc etc"
        from reportlab.platypus.flowables import DocAssign, DocExec, DocPara, DocIf, DocWhile
        from test_platypus_xref import MyDocTemplate
        from reportlab.platypus.tableofcontents import TableOfContents, SimpleIndex
        from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
        from reportlab.platypus import Paragraph
        from reportlab.lib import colors
        from reportlab.lib.randomtext import randomText, PYTHON

        # Build story.
        story = []

        styleSheet = getSampleStyleSheet()
        h1 = styleSheet['Heading1']
        h1.pageBreakBefore = 1
        h1.keepWithNext = 1
        h1.outlineLevel = 0

        h2 = styleSheet['Heading2']
        h2.backColor = colors.cyan
        h2.keepWithNext = 1
        h2.outlineLevel = 1

        bt = styleSheet['BodyText']

        story.append(
            Paragraph("""Cross-Referencing Test""", styleSheet["Title"]))
        story.append(
            Paragraph(
                """
            Subsequent pages test cross-references: indexes, tables and individual
            cross references.  The number in brackets at the end of each paragraph
            is its position in the story. (%d)""" % len(story), bt))

        story.append(Paragraph("""Table of Contents:""", styleSheet["Title"]))
        toc = TableOfContents()
        story.append(toc)

        chapterNum = 1
        for i in range(10):
            story.append(
                Paragraph(
                    'Chapter %d: Chapters always starts a new page' %
                    chapterNum, h1))
            chapterNum += chapterNum
            story.append(DocAssign('chapterNum', chapterNum))
            for j in range(3):
                story.append(
                    Paragraph(
                        'Heading1 paragraphs should always'
                        'have a page break before.  Heading 2 on the other hand'
                        'should always have a FRAME break before (%d)' %
                        len(story), bt))
                story.append(
                    Paragraph(
                        'Heading 2 should always be kept with the next thing (%d)'
                        % len(story), h2))
                for j in range(3):
                    story.append(
                        Paragraph(
                            randomText(theme=PYTHON, sentences=2) +
                            ' (%d)' % len(story), bt))
                    story.append(
                        Paragraph(
                            'I should never be at the bottom of a frame (%d)' %
                            len(story), h2))
                    story.append(
                        Paragraph(
                            randomText(theme=PYTHON, sentences=1) +
                            ' (%d)' % len(story), bt))

            story.extend([
                DocAssign('currentFrame', 'doc.frame.id'),
                DocAssign('currentPageTemplate', 'doc.pageTemplate.id'),
                DocAssign('aW', 'availableWidth'),
                DocAssign('aH', 'availableHeight'),
                DocAssign('aWH', 'availableWidth,availableHeight'),
                DocAssign('i', 3, life='forever'),
                DocIf('i>3', Paragraph('The value of i is larger than 3', bt),
                      Paragraph('The value of i is not larger than 3', bt)),
                DocIf('i==3', Paragraph('The value of i is equal to 3', bt),
                      Paragraph('The value of i is not equal to 3', bt)),
                DocIf('i<3', Paragraph('The value of i is less than 3', bt),
                      Paragraph('The value of i is not less than 3', bt)),
                DocWhile('i', [
                    DocPara('i',
                            format='The value of i is %(__expr__)d',
                            style=bt),
                    DocExec('i-=1')
                ]),
                DocPara('repr(doc._nameSpace)', escape=True),
            ])
        story.append(Paragraph('The Index which goes at the back', h1))
        story.append(SimpleIndex())

        doc = MyDocTemplate(
            outputfile('test_platypus_programming_multipass.pdf'))
        doc.multiBuild(story)
        story.append(Paragraph('Heading 2 should always be kept with the next thing ({})'.format(len(story)), h2))
        # 二级标题
        for p in range(3):
            story.append(Paragraph(randomText(theme=PYTHON, sentences=2) + ' ({})'.format(len(story)), bt))
            # 两句,内容随机产生,普通文本
            story.append(Paragraph('I should never be at the bottom of a frame ({})'.format(len(story)), h2))
            # 二级标题
            story.append(Paragraph(randomText(theme=PYTHON, sentences=1) + ' ({})'.format(len(story)), bt))
            # 一句,内容随机产生,普通文本
    story.extend([
        DocAssign('currentFrame', 'doc.frame.id'),
        DocAssign('currentPageTemplate', 'doc.pageTemplate.id'),
        DocAssign('aW', 'availableWidth'),
        DocAssign('aH', 'availableHeight'),
        DocAssign('aWH', 'availableWidth,availableHeight'),
        DocAssign('i', 3),
        DocIf('i>3', Paragraph('The value of i is larger than 3', bt),
              Paragraph('The value of i is not larger than 3', bt)),
        DocIf('i==3', Paragraph('The value of i is equal to 3', bt),
              Paragraph('The value of i is not equal to 3', bt)),
        DocIf('i<3', Paragraph('The value of i is less than 3', bt),
              Paragraph('The value of i is not less than 3', bt)),
        DocWhile('i', [DocPara('i', format='The value of i is %(__expr__)d', style=bt), DocExec('i-=1')]),
        DocPara('repr(doc._nameSpace)'),
    ])

story.append(Paragraph('The Index which goes at the back', h1))  # 索引的标题
story.append(SimpleIndex())  # 添加索引
doc = MyDocTemplate('multipass.pdf')
doc.multiBuild(story)
Example #10
0
text = """
this is sample <index item="index"/> without index
"""
p_text = Paragraph(text, style=styles["Normal"])

text1 = """
this is sample <index item="reportlab"/> with reportlab index
"""
p_text1 = Paragraph(text1, style=styles["Normal"])

text2 = """

this is sample <index item="flowable"/> flowable index
"""
p_text2 = Paragraph(text2, style=styles["Normal"])

index = SimpleIndex()
for i in range(1, 4):
    flow_obj.append(p_text)
    flow_obj.append(PageBreak())

for i in range(1, 4):
    flow_obj.append(p_text1)
    flow_obj.append(PageBreak())

for i in range(1, 4):
    flow_obj.append(p_text2)
    flow_obj.append(PageBreak())

flow_obj.append(index)
pdf.build(flow_obj, canvasmaker=index.getCanvasMaker())