简体   繁体   English

Python Reportlab分页符

[英]Python Reportlab Page Break

I am trying to generate a pdf report with reportlab in python. 我正在尝试使用python中的reportlab生成pdf报告。

My aim is to have the first page of my pdf file with just a simple title and a table without the actual contents. 我的目标是让我的pdf文件的第一页只有一个简单的标题和一个没有实际内容的表。 The actual contents will starts from second page. 实际内容将从第二页开始。

After looking through some SO Posts, I found out that afterPage() command can be used to break pages. 在查看了一些SO帖子之后,我发现可以使用afterPage()命令来破解页面。 So, I came up with something like this: 所以,我提出了这样的事情:

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer,KeepTogether,tables
from reportlab.lib import colors
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.lib.pagesizes import A4,landscape
from reportlab.lib.units import inch,cm,mm
from reportlab.pdfgen import canvas

PAGE_HEIGHT = defaultPageSize[1]
PAGE_WIDTH = defaultPageSize[0]
styles = getSampleStyleSheet()
style = styles["Normal"]

def myFirstPage(canvas, doc):
    canvas.saveState()
    canvas.setFont('Times-Bold',15)
    canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-38, 'Title 1')
    canvas.drawCentredString(PAGE_WIDTH/2.0, PAGE_HEIGHT-58, 'Title 2')

    NormalStyle = tables.TableStyle([
        ('BOX',(0,0),(-1,-1),0.45,colors.blue),
        ('ALIGN',(0,0),(-1,-1),'LEFT'),
        ('BACKGROUND',(0,0),(-1,-1),colors.lightblue)
        ])

    mytable = tables.Table([('test','test'),('test2','test2'),('test3','test3')],
    colWidths = 1* inch,rowHeights= 0.25 *inch,style = NormalStyle)

    mytable.wrapOn(canvas,PAGE_WIDTH ,PAGE_HEIGHT)
    mytable.drawOn(canvas,(doc.width/2)-20,700)

    doc.afterPage()
    canvas.restoreState()

def myLaterPages(canvas, doc):
    canvas.saveState()
    canvas.setFont('Times-Roman', 9)
    canvas.restoreState()

doc = SimpleDocTemplate("myreport.pdf",
                        leftMargin = 0.75*inch,
                        rightMargin = 0.75*inch,
                        topMargin = 1*inch,
                        bottomMargin = 1*inch)

data = "".join(['this is a test' for i in range(200)])
mydata = Paragraph(data,style)
Story = [Spacer(2.5,0.75*inch)]
Story.append(mydata)

doc.build(Story, onFirstPage=myFirstPage, onLaterPages=myLaterPages)

But instead all my title,table and contents are drawn in the first page and the afterPage() function does not seems to have any real effects on my contents of the document. 但相反,我的所有标题,表格和内容都是在第一页中绘制的,而afterPage()函数似乎对我的文档内容没有任何实际影响。

How can I change my code so that the contents ( data in my code) starts from second page? 如何更改我的代码以便内容(我的代码中的data )从第二页开始?

You can use PageBreak() for this. 您可以使用PageBreak() Just insert Story.append(PageBreak()) and import it from reportlab.platypus . 只需插入Story.append(PageBreak())并从reportlab.platypus导入它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM