如何使用Reportlab将目录添加到画布?

时间:2019-03-14 10:44:23

标签: python reportlab

我想将目录添加到画布。我有两个文件,一个文件创建目录,另一个文件创建pdf文件。下面是两个示例:

内容脚本表下方:

from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Paragraph, Flowable
from reportlab.platypus import PageBreak
from reportlab.platypus.tableofcontents import TableOfContents

    class DelayedRef(Flowable):
            _ZEROSIZE = True
            def __init__(self, toc, *args):
                    self.args = args
                    self.toc = toc

            def wrap(self,w,h):
                    return 0,0

            def draw(self,*args,**kwd):
                    self.toc.addEntry(*self.args)

    def simple_toc():
            doc = SimpleDocTemplate("simple_toc.pdf")
            story = []
            styles = getSampleStyleSheet()

            toc = TableOfContents()
            toc.levelStyles = [
                    ParagraphStyle(fontName='Helvetica', fontSize=14, name='Heading1',
                                               leftIndent=20, firstLineIndent=-20, spaceBefore=5,
                                               leading=16),
                    ParagraphStyle(fontName='Times-Roman', fontSize=14, name='Heading2',
                                                       leftIndent=20, firstLineIndent=-20, spaceBefore=5,
                                                       leading=16),
            ]
            story.append(toc)

            ipsum = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit,
            sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
            Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
            nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
            reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
            pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
            culpa qui officia deserunt mollit anim id est laborum.'''

            para = Paragraph("The Beginning", style=styles['Heading1'])
            story.append(DelayedRef(toc, 0, 'The Beginning', 1))
            story.append(para)
            para = Paragraph(ipsum, style=styles['Normal'])
            story.append(para)
            story.append(PageBreak())

            para = Paragraph("The Middle", style=styles['Heading1'])
            story.append(DelayedRef(toc, 0, 'The Middle', 2))
            story.append(para)
            para = Paragraph("The Middle Sub-Header", style=styles['Heading2'])
            story.append(DelayedRef(toc, 1, 'The Middle Sub-Header', 2))
            story.append(para)
            para = Paragraph(ipsum, style=styles['Normal'])
            story.append(para)
            story.append(PageBreak())

            para = Paragraph("The End", style=styles['Heading1'])
            story.append(DelayedRef(toc, 0, 'The End', 3))
            story.append(para)

            doc.multiBuild(story)

在创建pdf文件的文件(create_pdf.py)下方。

def create_pdf():
    import_font() #Dit is een functie die fonts importeert
    c = canvas.Canvas("test.pdf", pagesize=(A4))

    #titel pagina toevoegen
    generate_title_page(c)

    #Generate index page
    c.showPage()
    simple_toc()

    #add chapter pagina toevoegen
    c.showPage()
    generate_chapter(c)

    c.save()

我想将目录添加到画布。此时,它会创建一个名为“ simple_toc.pdf”的新pdf文件。

下面是激活create_pdf.py的run.py文件

from reportbuilder.pdf_builder.create_pdf import create_pdf

    #Genereert PDF file
    if __name__ == "__main__":
        #pdf_title_page.create_pdf()
        #pdf_content_page.create_addChapter()
        create_pdf()

希望有人可以帮助我解决这个问题,他们已经花了几个小时寻找解决方案。

0 个答案:

没有答案