[reportlab-users] How to use graphics Drawing objects with pdfgen Canvas

Robin Becker robin at reportlab.com
Mon Nov 28 12:44:20 EST 2005


tim graber wrote:
> In the graphics guide, I found this paragraph on using
> a drawing object directly with Platypus or
> pdfgen/canvas.  I have been trying to figure out how
> to incorporate the graphics Drawing object directly
> onto a pdfgen Canvas object.  Does anyone have any
> experience with this?  Thanks.
> 
> "The PDF renderer has special "privileges" - a Drawing
> object is also a Flowable and, hence, can be placed
> directly in the story of any Platypus document, or
> drawn directly on a Canvas with one line of code. In
> addition, the PDF renderer has a utility function to
> make a one-page PDF document quickly."
> 
> from reportlab.pdfgen import canvas
> from reportlab.graphics.shapes import *
> from reportlab.lib import colors
> from reportlab.lib.units import inch
> 
> c = canvas.Canvas("hello.pdf")
> c.translate(1*inch,1*inch)
> d = Drawing(400, 200)
> #d.add(Rect(50, 50, 300, 100,
> fillColor=colors.yellow)) d.add(String(150,100, 'Hello
> World', fontSize=18)) c.drawImage(d, inch, inch, 130,
> 48)

The above is wrong as Drawings are not images. In practice you need to render a 
drawing to a canvas. So inpractice you need two lines to render a drawing to a 
canvas eg

from reportlab.graphics import renderPDF

renderPDF.draw(drawing, canvas, x, y, [showBoundary=True/False])


set showBoundary=True to get a (red?) box round the drawing.
-- 
Robin Becker


More information about the reportlab-users mailing list