Python reportlab没有正确生成pdf

时间:2014-01-30 14:38:18

标签: python reportlab

我正在尝试生成一个pdf,它将打印字体大小和一个句子。但是在生成pdf之后,它就会重叠。

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 个答案:

答案 0 :(得分:0)

你有错误的缩进,而drawString的第二行导致重叠。

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()