[reportlab-users] Fwd: text of paragraph sits at bottom of area.
Robin Becker
robin at reportlab.com
Tue Apr 7 05:52:21 EDT 2009
Kerri Reno wrote:
.........
>
> I'm creating a pay stub with reportlab, that has to fit on one page. I
> started with Jeff Bauer's hcfa1500.py from the reportlab web site - THANKS
> JEFF! I was able to modify it to fit my needs, even including a couple of
> paragraphs. My problem is that the paragraphs print in the bottom of the
> allowed area.
>
> I'm using
>
> stylesheet = getSampleStyleSheet()
> style = stylesheet['Normal']
> style.fontName=self.text_font
> style.fontSize = self.text_size
> style.leading = int(self.text_size * 1.2)
> c = self.canvas
>
> # x, y = bottom left corner of area
> # width and height are the maximum area I want the
> # paragraph to use up
> p = Paragraph(v,style)
> f = KeepInFrame(width,height,[p],mode='shrink')
> f.wrapOn(c,width,height)
> f.drawOn(c,x,y)
>
> This works great, except if the text of the paragraph doesn't take up all
> the space, then it prints in the bottom portion of the area.
>
> I've searched the documentation and the web for an answer, and I just can't
> find it. I'd appreciate any help you can give me.
>
> Thanks in Advance!
> Kerri
>
........
first off the normal way of drawing paragraphs is with the platypus doctemplate
frame work. But if you wish to do this directly into a canvas then it's wise to
know the api that's used.
First off wrap/wrapOn return the width and height that is used by the flowable;
eg
# NB w, h don't have to be equal to width and height
w,h = f.wrapOn(c,width,height)
then the x and y for the drawOn correspond to the bottom left of the flowable.
Assuming you wish to draw from the top down you need to subtract the height from
the top left y.
try this script to get some idea of where things are
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus.paragraph import Paragraph
from reportlab.platypus.flowables import KeepInFrame
stylesheet = getSampleStyleSheet()
style = stylesheet['Normal']
style.fontName = 'Helvetica'
style.fontSize = 10
style.leading = int(style.fontSize * 1.2)
c = Canvas('simple.pdf')
width=72 #1in x 1in width for our para
height=72
#define top left
tlx = 72 #one inch in from left
tly = c._pagesize[1] - 72 #one inch down from top
#show our area
c.setLineWidth = 1
c.setStrokeColor((1,0,0)) #red
c.rect(tlx,tly,width,-height,stroke=1,fill=0)
p = Paragraph('This is a silly paragraph that\'s used to make up the numbers and
show the world where things go.',style)
f = KeepInFrame(width,height,[p],mode='shrink')
w,h=f.wrapOn(c,width,height)
f.drawOn(c,tlx,tly-height)
c.showPage()
c.save()
--
Robin Becker
More information about the reportlab-users
mailing list