Ashish Kurian
Ashish Kurian

Reputation: 71

Different page template from second page

The following is my code for generation of a pdf using reportlab. I am almost finished with the format but the page template of first page is getting copied from the second page as well. From second page, I just need a two column template and I do not want the TopCenter, frame1 and frame2 format from second page.

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)

The generated PDF is here https://www.dropbox.com/s/16hogbi6oofqg31/test.pdf?dl=0

As there was large data in Para1 (a arbitaraty dictionary), the template of page 1 was copied to page 2. What I want is that even if there is a large amount of data, the page 2 should have the other template as seen from page 3

Can someone help me here in acheiving this?

Upvotes: 1

Views: 3351

Answers (1)

Ashish Kurian
Ashish Kurian

Reputation: 71

I found answer to my question by clearly going through this post which I am sharing.

Setting a separate template from second page

Upvotes: 1

Related Questions