User:Simon/Trim4/Layout using Reportlab: Difference between revisions
No edit summary |
No edit summary |
||
Line 2: | Line 2: | ||
From this tutorial, I created a basic page: | From this tutorial, I created a basic page: | ||
<nowiki> #import the canvas object from pdfgen | |||
from reportlab.pdfgen import canvas | |||
#supply a filename to the canvas object | #supply a filename to the canvas object | ||
c = canvas.Canvas("hello.pdf") | c = canvas.Canvas("hello.pdf") | ||
Line 12: | Line 12: | ||
[[File:reportlab_pdf.JPG|600px|frameless]] | [[File:reportlab_pdf.JPG|600px|frameless]] | ||
By adding a zero to the bottomup keyword argument you can change where drawstring starts draing text to - 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: | |||
<nowiki> | |||
#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</nowiki> | |||
---------- | ---------- | ||
A form: | A form: |
Revision as of 13:59, 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()
By adding a zero to the bottomup keyword argument you can change where drawstring starts draing text to - 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
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 = '<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)