User:Simon/Trim4/Layout using Reportlab: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 60: Line 60:
     canvas.drawString(120,703,"JOHN DOE")
     canvas.drawString(120,703,"JOHN DOE")
      
      
     canvas.save()/nowiki>
     canvas.save()</nowiki>


The main difference here is the canvas.line function - which draws lines on the canvas.
The main difference here is the canvas.line function - which draws lines on the canvas. Passing it two X,Y pairs sets the coordinates for where each line should begin and end.<br>
[[File:reportlab_form.JPG|600px|frameless]]
[[File:reportlab_form.JPG|600px|frameless]]



Latest revision as of 15:10, 27 October 2019

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:

    #import the canvas object from pdfgen
    from reportlab.pdfgen import canvas
    #supply a filename to the canvas object
    c = canvas.Canvas("hello.pdf")
    # drawstring draws text - starts at the bottom left, so this is 100 points (pixels) from the left margin, and 750 from the bottom
    c.drawString(100,750,"Welcome to Reportlab!")
    #save the PDF
    c.save()

Reportlab pdf.JPG

By adding a zero to the bottomup keyword argument you can change where drawstring starts drawing text from - this will probably set it to begin at top left The default canvas is A4, this can be changed to other formats (such as US letter) with the following code:

   
    #import the letter format from the pagesizes library
    from reportlab.lib.pagesizes import letter
    #import the canvas object from pdfgen
    from reportlab.pdfgen import canvas
    #supply a filename to the canvas object, specify the pagesize to be letter
    canvas = canvas.Canvas('myfile.pdf', pagesize=letter)
    #grab the width and height for letter - this can be useful to calculate when to add a page break or to define margins
    width, height = letter

Other options for the canvas object:

   
    def __init__(self,filename,
    pagesize=letter,
    bottomup = 1,
    pageCompression=0,
    encoding=rl_config.defaultEncoding,
    verbosity=0
    encrypt=None):

A form:

   from reportlab.lib.pagesizes import letter
    from reportlab.pdfgen import canvas
    
    canvas = canvas.Canvas("form.pdf", pagesize=letter)
    canvas.setLineWidth(.3)
    canvas.setFont('Helvetica', 12)
    
    canvas.drawString(30,750,'OFFICIAL COMMUNIQUE')
    canvas.drawString(30,735,'OF ACME INDUSTRIES')
    canvas.drawString(500,750,"12/12/2010")
    canvas.line(480,747,580,747)
    
    canvas.drawString(275,725,'AMOUNT OWED:')
    canvas.drawString(500,725,"$1,000.00")
    canvas.line(378,723,580,723)
     
    canvas.drawString(30,703,'RECEIVED BY:')
    canvas.line(120,700,580,700)
    canvas.drawString(120,703,"JOHN DOE")
    
    canvas.save()

The main difference here is the canvas.line function - which draws lines on the canvas. Passing it two X,Y pairs sets the coordinates for where each line should begin and end.
Reportlab form.JPG


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 = '<font size=12>%s</font>' % formatted_time
     
    Story.append(Paragraph(ptext, styles["Normal"]))
    Story.append(Spacer(1, 12))
     
    # Create return address
    ptext = '<font size=12>%s</font>' % full_name
    Story.append(Paragraph(ptext, styles["Normal"]))       
    for part in address_parts:
        ptext = '<font size=12>%s</font>' % part.strip()
        Story.append(Paragraph(ptext, styles["Normal"]))   
 
    Story.append(Spacer(1, 12))
    ptext = '<font size=12>Dear %s:</font>' % full_name.split()[0].strip()
    Story.append(Paragraph(ptext, styles["Normal"]))
    Story.append(Spacer(1, 12))
     
    ptext = '<font size=12>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.</font>' % (magName, 
                                                                                                    issueNum,
                                                                                                    subPrice,
                                                                                                    limitedDate,
                                                                                                    freeGift)
    Story.append(Paragraph(ptext, styles["Justify"]))
    Story.append(Spacer(1, 12))
     
     
    ptext = '<font size=12>Thank you very much and we look forward to serving you.</font>'
    Story.append(Paragraph(ptext, styles["Justify"]))
    Story.append(Spacer(1, 12))
    ptext = '<font size=12>Sincerely,</font>'
    Story.append(Paragraph(ptext, styles["Normal"]))
    Story.append(Spacer(1, 48))
    ptext = '<font size=12>Ima Sucker</font>'
    Story.append(Paragraph(ptext, styles["Normal"]))
    Story.append(Spacer(1, 12))
    doc.build(Story)

Reportlab letter.JPG