[reportlab-users] carriage return

Tim Roberts timr at probo.com
Mon Jun 6 13:15:15 EDT 2011

Kosei Chiwaki wrote:

> May I know how do I put carriage return into Hello World ?


> from reportlab.pdfgen import canvas

> from reportlab.lib.units import cm

> c = canvas.Canvas("d.pdf")

> c.drawString(9*cm, 22*cm, "Hello\nWorld!")

> c.showPage()

> c.save()

You are expecting ReportLab to assume too much. Where should the new
line begin? How far down should it skip? The Platypus library in
ReportLab has the ability to do things like this, but when you're
writing the raw strings using the low-level canvas interface, you are
expected to manage lines and spacing on your own.

c = canvas.Canvas("d.pdf")
y = 22*cm
c.drawString( 9*cm, y, "Hello" )
y -= c._leading
c.drawString( 9*cm, y, "World!" )
y -= c._leading

"Leading" is the publishing term for the distance between one line's
baseline and the next line's baseline. By default, it gets set to 120%
of the font's point size.

It isn't hard to write a function to draw a multi-line string if you want:

def drawMultiString( canvas, x, y, s ):
for ln in s.split('\n'):
c.drawString( x, y, ln )
y -= c._leading
return y

Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.

More information about the reportlab-users mailing list