繁体   English   中英

Python reportlab无法正确生成pdf

[英]Python reportlab not generating pdf properly

我正在尝试生成一个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()

缩进错误,第二行与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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM