Python 带有TTfont的reportlab字符串如何获取它';她身高多少?

Python 带有TTfont的reportlab字符串如何获取它';她身高多少?,python,reportlab,Python,Reportlab,我正在使用reportlab生成pdf文件。我在pdf上绘制字符串时遇到了一些问题。如何使用TTFont获取字符串的高度 守则: # Register fonts. pdfmetrics.registerFont(ttfonts.TTFont('fz1', 'fz1.ttf')) pdfmetrics.registerFont(ttfonts.TTFont('fz3', 'fz3.ttf')) pdfmetrics.registerFont(ttfonts.TTFont('fz4', 'fz4.

我正在使用reportlab生成pdf文件。我在pdf上绘制字符串时遇到了一些问题。如何使用TTFont获取字符串的高度

守则:

# Register fonts.
pdfmetrics.registerFont(ttfonts.TTFont('fz1', 'fz1.ttf'))
pdfmetrics.registerFont(ttfonts.TTFont('fz3', 'fz3.ttf'))
pdfmetrics.registerFont(ttfonts.TTFont('fz4', 'fz4.ttf'))
pdfmetrics.registerFont(ttfonts.TTFont('fz5', 'fz5.ttf'))
pdfmetrics.registerFont(ttfonts.TTFont('w5', 'w5.ttc'))

def draw_text(canvas, fontName, fontSize, x, y, text, cmyk_color=None):
    t = canvas.beginText(x * mm, y * mm)
    t.setFont(fontName, fontSize)

    if cmyk_color is None:
       cmyk_color = (0, 0, 0, COLOR_DIV_RATIO)

    canvas.setFillColorCMYK(cmyk_color[0] / COLOR_DIV_RATIO,
                            cmyk_color[1] / COLOR_DIV_RATIO,
                            cmyk_color[2] / COLOR_DIV_RATIO,
                            cmyk_color[3] / COLOR_DIV_RATIO)
    t.textLine(text)
    canvas.drawText(t)

c.drawImage('f1.jpg', 0, 0, CANVAS_WIDTH * mm, CANVAS_HEIGHT * mm)
draw_text(c, 'fz1', 15, mm2pixel(5), mm2pixel(45), u'This is a string')
我可以通过以下方式获得字符串的宽度:

text_width = stringWidth(text, 'fz1', 15)

但是,如何获得字符串的高度呢?

如果使用段落,然后将其包装在画布上,它将返回给您它将占用的宽度和高度

p = Paragraph(text, style=preferred_style)
width, height = p.wrapOn(self.canvas, aW, aH)
p.drawOn(self.canvas, x_pos, y_pos)
aH=可用高度
aW=可用宽度


样式
指的是段落样式。

我相信这就是字体大小的意思(15可能是15分高),尽管我愿意猜测还有更多,目前还不记得所有细节