0
votes

I am trying to generate a pdf which will print the font size along with a sentence. But after the pdf is getting generated, it is overlapping.

from reportlab.lib.units import inch
from reportlab.lib.colors import magenta, red
from reportlab.pdfgen import canvas

lyrics = ['This is first line', 'This is second line', 'This is third line', 'This is the fourth line', 'This is the fifth line']

def textsize(canvas):
    canvas.setFont('Times-Roman', 20)
    canvas.setFillColor(red)
    canvas.drawCentredString(2.75*inch, 2.5*inch, "Font Size examples")
    canvas.setFillColor(magenta)

    size = 7
    y = 2.3 * inch
    x = 1.3 * inch
    for line in lyrics:
        canvas.setFont('Helvetica', size)
    canvas.drawString(x, y, '%s points' % size)
    canvas.drawString(x, y, line)
    y = y - (size * 1.2)
    size = size + 1.5

c = canvas.Canvas('font.pdf')
textsize(c)
c.showPage()
c.save()
1

1 Answers

1
votes

You have wrong indentation and the second line with drawString is causing the overlapping.

from reportlab.lib.units import inch
from reportlab.lib.colors import magenta, red
from reportlab.pdfgen import canvas

lyrics = ['This is first line', 'This is second line', 'This is third line', 'This is the fourth line', 'This is the fifth line']

def textsize(canvas):
    canvas.setFont('Times-Roman', 20)
    canvas.setFillColor(red)
    canvas.drawCentredString(2.75*inch, 2.5*inch, "Font Size examples")
    canvas.setFillColor(magenta)

    size = 7
    y = 2.3 * inch
    x = 1.3 * inch
    for line in lyrics:
        canvas.setFont('Helvetica', size)
        canvas.drawString(x, y, '%s points' % size)
        print x, y, line
        canvas.drawString(x + 2*inch, y, line)
        y = y - (size * 1.2)
        size = size + 1.5

c = canvas.Canvas('font.pdf')
textsize( c )
c.showPage()
c.save()