以下是使用reportlab生成pdf的代码。我几乎完成了格式,但第一页的页面模板也从第二页复制。从第二页开始,我只需要一个两列模板,我不想从第二页开始使用TopCenter,frame1和frame2格式。
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter,A4
from reportlab.platypus import Image
from reportlab.lib.units import inch, cm
import json
from reportlab.platypus import
BaseDocTemplate,Frame,Paragraph,PageBreak,
PageTemplate,Spacer,FrameBreak,NextPageTemplate
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.enums import TA_JUSTIFY, TA_CENTER
from reportlab.platypus import BaseDocTemplate, Frame, Paragraph,
PageBreak, PageTemplate
from reportlab.lib.styles import getSampleStyleSheet
def foot1(canvas,doc):
width,height = A4
canvas.saveState()
canvas.setFont('Times-Roman',9)
canvas.drawString(width-0.1*inch, 0.1 * inch, "%d" % doc.page)
canvas.restoreState()
def foot2(canvas,doc):
width,height = A4
canvas.saveState()
canvas.setFont('Times-Roman',9)
canvas.drawString(width-0.1*inch, 0.1 * inch,"%d" % doc.page)
def demo1(canvas):
width,height = A4
doc = BaseDocTemplate('test.pdf',showBoundary=1)
contents =[]
styleSheet = getSampleStyleSheet()
TopCenter = Frame(1.2*inch,height-1.2*inch,width-2.4*inch,1*inch,showBoundary = 1,id='normal')
frame1 = Frame(0.2*inch,0.2*inch,(width-0.6*inch)/2, height-1.6*inch,showBoundary = 1,id='col1')
frame2 = Frame(0.4*inch+(width-0.6*inch)/2,0.2*inch, (width-0.6*inch)/2, height-1.6*inch,showBoundary = 1,id='col2' )
leftlogoframe = Frame(0.2*inch,height-1.2*inch,1*inch,1*inch,showBoundary = 1)
rightlogoframe = Frame((width-1.2*inch),height-1.2*inch,1*inch,1*inch,showBoundary = 1)
frame1later = Frame(0.2*inch,0.2*inch,(width-0.6*inch)/2, height-0.4*inch,showBoundary = 1,id='col1later')
frame2later = Frame(0.4*inch+(width-0.6*inch)/2,0.2*inch, (width-0.6*inch)/2, height-0.4*inch,showBoundary = 1,id='col2later' )
firstpage = PageTemplate(id='firstpage',frames=[leftlogoframe,TopCenter,rightlogoframe,frame1,frame2],onPage=foot1)
laterpages = PageTemplate(id='laterpages',frames=[frame1later,frame2later],onPage=foot2)
bodyStyle = ParagraphStyle('Body',fontSize=11)
para1 = Paragraph('Spam spam spam spam. ' * 300, bodyStyle)
contents.append(NextPageTemplate('firstpage'))
logoleft = Image('isclogo.png')
logoleft._restrictSize(0.7*inch, 0.7*inch)
logoleft.hAlign = 'LEFT'
logoleft.vAlign = 'CENTER'
logoright = Image('isclogo.png')
logoright._restrictSize(0.7*inch, 0.7*inch)
logoright.hAlign = 'RIGHT'
logoright.vAlign = 'CENTER'
contents.append(logoleft)
contents.append(FrameBreak())
json_file = open("details.txt","r",encoding='utf-8')
details = json.load(json_file)
isctitle = styleSheet['Title']
isctitle.fontSize=12
isctitle.alignment=TA_CENTER
isctitle.leading=10
contents.append(Paragraph("INTERNATIONAL STUDENT UNION. Raamstraat 78, Delft",isctitle))
theme = styleSheet['Normal']
theme.fontSize=10
theme.alignment=TA_CENTER
theme.leading = 14
contents.append(Paragraph(str(details["theme"]),theme))
celebrant=styleSheet['Normal']
celebrant.fontSize=10
celebrant.alignment=TA_CENTER
celebrant.leading = 14
contents.append(Paragraph(str("President: "+details["president"]),celebrant))
date = styleSheet['Normal']
date.fontSize=10
date.alignment=TA_CENTER
date.leading = 14
contents.append(Paragraph(str(details["date"]),date))
contents.append(FrameBreak())
contents.append(logoright)
contents.append(FrameBreak())
themeimage=Image('isclogo.png')
contents.append(themeimage)
contents.append(para1)
#contents.append(PageBreak())
contents.append(NextPageTemplate('laterpages'))
contents.append(PageBreak())
doc.addPageTemplates([firstpage,laterpages])
#doc.addPageTemplates([PageTemplate(frames=[leftlogoframe,TopCenter,rightlogoframe,frame1,frame2]), ])
#doc.addPageTemplates([PageTemplate(id='OneCol',frames=Top,onPage=foot1),PageTemplate(id='TwoCol',frames=[frame1,frame2],onPage=foot2)])
doc.build(contents)
c = canvas.Canvas("test.pdf",pagesize=A4,bottomup=1)
demo1(c)
生成的PDF在https://www.dropbox.com/s/16hogbi6oofqg31/test.pdf?dl=0
由于Para1(一个任意字典)中有大量数据,第1页的模板被复制到第2页。我想要的是即使有大量数据,第2页也应该有另一个模板从第3页可以看出
有人可以帮助我实现这个目标吗?