[reportlab-users] Frames, Logo images, and text
Chuck
watsocd at gmail.com
Tue Jan 16 21:31:21 EST 2007
Sorry for being unclear. I did finally get it working. I am sure I will be back
to ask more questions.
I needed to create a PDF that looks like standard letter head with an some text
(address info) to the left and an image (logo) to the right. The remainder of
the document is simple text, rectangles, and lines.
The examples I have seen for ReportLab are very complex examples with little
documentation. Below is my final version of code for my proof of concept.
Chuck
******************* CUT *********************
from reportlab.pdfgen import *
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import *
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import letter
from pgdb import * #PostgreSQL database access
#precalculate some basics
top_margin = letter[1] - inch
bottom_margin = inch
left_margin = inch
right_margin = letter[0] - inch
frame_width = right_margin - left_margin
def run():
from reportlab.lib.styles import ParagraphStyle
from reportlab.platypus import Image
#create the document
c = Canvas('bill3.pdf', pagesize=letter)
#need a style
normal = ParagraphStyle('normal', fontSize=12)
#This section does the logo and the main address on the bill
Addr = []
Logo = []
Logo.append(Image("pic.jpg", 100, 71)) #Logo
para = Paragraph("Some text1", normal)
Addr.append(para)
para = Paragraph("Some text2", normal)
Addr.append(para)
para = Paragraph("Some text3", normal)
Addr.append(para)
para = Paragraph("Some text4", normal)
Addr.append(para)
para = Paragraph(" ", normal)
Addr.append(para)
f1 = Frame(left_margin, top_margin ,200,100, showBoundary=0) #Text
frame
f2 = Frame(right_margin-300,top_margin ,200,100, showBoundary=0) #Logo
frame
f1.addFromList(Addr,c) #Add the text to the page
f2.addFromList(Logo,c) #Add the Logo to thepage
#This section is the main body of the bill
tx = c.beginText(left_margin, top_margin)
#Draw a line to end the section
(x,y) = tx.getCursor()
c.line(x,y+15,right_margin,y+15) #positive moves the line up
tx = c.beginText(left_margin, top_margin)
tx.setFont("Times-Roman", 12)
tx.textLine("Section 1")
tx.textLine("Next line")
tx.textLine("Next line")
tx.textLine("Next line")
#Draw a line to end the section
(x,y) = tx.getCursor()
c.line(x,y+15,right_margin,y+15) #positive moves the line up
tx.textLine("Section 2")
tx.textLine("Next line")
tx.textLine("Next line")
c.drawText(tx) #Draw all the text on the canvas
c.save() #save the document/write to disk
run()
More information about the reportlab-users
mailing list