[reportlab-users] Scaling and Translation

Ian Sparks Ian.Sparks at etrials.com
Wed Jul 6 08:45:06 EDT 2005


Thanks Ashley and Andy, I was working on this last night and stumbled across your solutions. Your replies validate the approach, specifically :

1. Put the origin at the top left of the page (thanks Andy!)
2. Pass in the scaling factor (thanks Ashley!)

For those who care some sample code follows. It draws a grid on the page (useful for lining things up I find), then draws a number of boxes with different scaling factors applied. The important thing for me was that they their left sides and tops were all aligned.

Thanks!


from reportlab.lib.units import inch
from reportlab.pdfgen import canvas


def drawGrid(canv):
    """Draw grid lines on the canvas"""
    canv.saveState()
    try:
        canv.setStrokeColorRGB(0.2,0,1)
        canv.setLineWidth(1)

        #Draw horizontal
        for i in xrange(1,12):
            canv.line(0,-inch * i,8.5 * inch,-inch * i)

        #Draw vertical
        for i in xrange(1,9):
            canv.line(i * inch,0,i * inch,-11 * inch)
    finally:
        canv.restoreState()

def drawGreenBox(canv):
    """Draw a green box on the canvas"""
    canv.saveState()
    try:
        canv.setStrokeColorRGB(0,1,0.2)
        canv.rect(0,-1 * inch,inch,-inch) #Draw an inch-square box.
    finally:
        canv.restoreState()

def drawScaledBox(canv):
    """Draw a box in the same location as the green box but scaled, do it in red"""
    canv.saveState()
    try:
        canv.scale(0.5,0.5)       #Make all drawing operations half-size
        canv.setStrokeColorRGB(1,0.2,0)
        canv.rect(0,-1 * 2 * inch,inch,-inch) #Draw an inch-square box.
    finally:
        canv.restoreState()


def drawScaledBox2(canv,scaling_factor):
    """Draw a box in the same location as the green box but scaled, do it in subtly changing color based on scaling_factor"""
    canv.saveState()
    try:
        canv.scale(scaling_factor,scaling_factor)       #Scale all drawing operations 
        canv.setStrokeColorRGB(1,0.7,0.6 * scaling_factor)
        canv.rect(0,-1 * (1.0 / scaling_factor) * inch,inch,-inch) #Draw an inch-square box.
    finally:
        canv.restoreState()

       
if __name__ == "__main__":

    canv = canvas.Canvas("test.pdf",pagesize=(8.5 * inch, 11 * inch))

    canv.translate(0,11 * inch) #set the top of the page as the origin
 
    drawGrid(canv)
    drawGreenBox(canv)
    drawScaledBox(canv)
    drawScaledBox2(canv,0.4)
    drawScaledBox2(canv,0.5)
    drawScaledBox2(canv,0.6)
    drawScaledBox2(canv,0.7)
    drawScaledBox2(canv,0.8)
    drawScaledBox2(canv,0.9)
    canv.save()





> -----Original Message-----
> From: Ashley Lloyd [mailto:ashleylloyd at hotmail.com]
> Sent: Wednesday, July 06, 2005 3:47 AM
> To: reportlab-users at reportlab.com
> Subject: RE: [reportlab-users] Scaling and Translation
> 
> 
> I know this is a very simple answer, but surely it would work ...
> 
> Couldn't you just set it up, setting a variable called, say, 
> sizing (or 
> xsizing and ysizing if necessary), so having:
> sizing=0.5
> canv.scale(sizing,sizing)       #Make all drawing operations half-size
> canv.rect(0,9 * inch * 1/sizing,inch,inch) #Draw an 
> inch-square box 9-inches 
> up the page.
> 
> Ok, initially that would mean changing every drawing 
> operation, but when 
> making subsequent adjustments, you'd just have to assign sizing as 
> necessary.
> 
> HTH
> 
> Ashley
> 
> >From: "Ian Sparks" <Ian.Sparks at etrials.com>
> >Reply-To: Support list for users of Reportlab software 
> ><reportlab-users at reportlab.com>
> >To: "Reportlab-Users (E-mail)" <reportlab-users at reportlab.com>
> >Subject: [reportlab-users] Scaling and Translation
> >Date: Tue, 5 Jul 2005 16:26:52 -0400
> >
> >
> >
> >I have a set drawing operations that render a complex shape. 
> Ordinarily the 
> >shape wouldn't fit on the canvas page so I'm going to scale 
> the shape.
> >
> >So :
> >
> >from reportlab.lib.units import inch
> >from reportlab.pdfgen import canvas
> >
> >canv = canvas.Canvas("D:/PDFGen2/t.pdf")
> >canv.scale(0.5,0.5)       #Make all drawing operations half-size
> >canv.rect(0,9 * inch,inch,inch) #Draw an inch-square box 
> 9-inches up the 
> >page.
> >canv.save()
> >
> >BUT! I want to maintain the position of the box at 9 inches 
> from the bottom 
> >of the page even though I want it to draw half-size. The 
> above code will 
> >draw it 4.5 inches from the top of the page.
> >
> >I can adjust to this :
> >
> >canv.rect(0,9 * inch * 2,inch,inch) #Draw an inch-square box 
> (account for 
> >scaling)
> >
> >but that means touching every drawing operation.
> >
> >So My question : Is there a better, more global way to achieve this?
> >
> >
> >
> >_______________________________________________
> >reportlab-users mailing list
> >reportlab-users at reportlab.com
> >http://two.pairlist.net/mailman/listinfo/reportlab-users
> 
> 
> _______________________________________________
> reportlab-users mailing list
> reportlab-users at reportlab.com
> http://two.pairlist.net/mailman/listinfo/reportlab-users
> 


More information about the reportlab-users mailing list