User:Simon/Trim4/Layout using Reportlab

From XPUB & Lens-Based wiki
< User:Simon
Revision as of 14:25, 25 October 2019 by Simon (talk | contribs) (Created page with "Reportlab is a Python library that allows you to make multi-page documents, working with a canvas. I found a nice introduction and tutorial here: https://www.blog.pythonlibrar...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Reportlab is a Python library that allows you to make multi-page documents, working with a canvas. I found a nice introduction and tutorial here: https://www.blog.pythonlibrary.org/2010/03/08/a-simple-step-by-step-reportlab-tutorial/

From this tutorial, I created a basic page:

   from reportlab.pdfgen import canvas
   
   c = canvas.Canvas("hello.pdf")
   c.drawString(100,750,"Welcome to Reportlab!")
   c.save()


A form:

   from reportlab.lib.pagesizes import letter
   from reportlab.pdfgen import canvas
    
   canvas = canvas.Canvas('myfile.pdf', pagesize=letter)
   width, height = letter

and a letter with flowing text:

   import time
   from reportlab.lib.enums import TA_JUSTIFY
   from reportlab.lib.pagesizes import letter
   from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image
   from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
   from reportlab.lib.units import inch
    
   doc = SimpleDocTemplate("form_letter.pdf",pagesize=letter,
                           rightMargin=72,leftMargin=72,
                           topMargin=72,bottomMargin=18)
   Story=[]
   logo = "python_logo.png"
   magName = "Pythonista"
   issueNum = 12
   subPrice = "99.00"
   limitedDate = "03/05/2010"
   freeGift = "tin foil hat"
    
   formatted_time = time.ctime()
   full_name = "Mike Driscoll"
   address_parts = ["411 State St.", "Marshalltown, IA 50158"]
    
   im = Image(logo, 2*inch, 2*inch)
   Story.append(im)
    
   styles=getSampleStyleSheet()
   styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
   ptext = '%s' % formatted_time
    
   Story.append(Paragraph(ptext, styles["Normal"]))
   Story.append(Spacer(1, 12))
    
   # Create return address
   ptext = '%s' % full_name
   Story.append(Paragraph(ptext, styles["Normal"]))       
   for part in address_parts:
       ptext = '%s' % part.strip()
       Story.append(Paragraph(ptext, styles["Normal"]))   

   Story.append(Spacer(1, 12))
   ptext = 'Dear %s:' % full_name.split()[0].strip()
   Story.append(Paragraph(ptext, styles["Normal"]))
   Story.append(Spacer(1, 12))
    
   ptext = 'We would like to welcome you to our subscriber base for %s Magazine! \
           You will receive %s issues at the excellent introductory price of $%s. Please respond by\
           %s to start receiving your subscription and get the following free gift: %s.' % (magName, 
                                                                                                   issueNum,
                                                                                                   subPrice,
                                                                                                   limitedDate,
                                                                                                   freeGift)
   Story.append(Paragraph(ptext, styles["Justify"]))
   Story.append(Spacer(1, 12))
    
    
   ptext = 'Thank you very much and we look forward to serving you.'
   Story.append(Paragraph(ptext, styles["Justify"]))
   Story.append(Spacer(1, 12))
   ptext = 'Sincerely,'
   Story.append(Paragraph(ptext, styles["Normal"]))
   Story.append(Spacer(1, 48))
   ptext = 'Ima Sucker'
   Story.append(Paragraph(ptext, styles["Normal"]))
   Story.append(Spacer(1, 12))
   doc.build(Story)