簡體   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