[reportlab-users] Combine DocTemplates into one PDF

Remy C. Cool reportlab-users@reportlab.com
Sun, 14 Mar 2004 16:43:26 +0100


On Saturday 13 March 2004 16:48, Remy C. Cool wrote:

> What I want to do is to create a mailing containing one or more
> pages per client and store these into one single PDF file.
>
> So I will need a sort of MailingTemplate class or function which
> holds the DocTemplate and generates the PDF using this DocTemplate
> per client.
>
> def MailingTemplate(clientList, myDocTemplate):
>
> 	for client in clientList:
> 		# retrieve client data from database
> 		...
> 		# create flowables
> 		...
> 		# use my DocTemplate to generate PDF data
> 		...
> 	# close PDF
> 	...

Found the/one simple solution:

styles = getSampleStyleSheet() 
styleN = styles['Normal'] 
styleH = styles['Heading1'] 

frame_1 = Frame(inch, inch, 6*inch, 7*inch, showBoundary=1)
frame_2 = Frame(inch, inch, 6*inch, 9*inch, showBoundary=1)

page_1 = PageTemplate(id='page_1',frames=[frame_1])
page_2 = PageTemplate(id='page_2',frames=[frame_2])

story = []
clients = range(5)
partext = 'testing ' * 21

for client in clients:
  story.append(NextPageTemplate('page_2'))
  # append addres info here
  story.append(Paragraph("Client %d address" % client, styleH))
  # append content      
  for i in range(50): 
     story.append(Paragraph(partext, styleN))
     if client != clients[-1]:
        story.append(NextPageTemplate('page_1'))
        story.append(PageBreak())
        
doc = BaseDocTemplate('mydoc.pdf',
                      pagesize=letter,
                      pageTemplates=[page_1, page_2]) 
doc.build(story)


Remy