如何在 Reportlab 中构建文本

问题描述 投票:0回答:1
text = 'Hello World'
length = len(text)
can.translate(400, 293)
can.rotate(-1 * 200 / 1.06)
can.rotate(-1 * (200 / length) / 1.06)
for n in text:
    can.rotate(200 / length)
    can.translate(0, -1 * 0.8*inch)        
    can.drawString(0, 0, n)

这是我尝试过的。 文本是拱形的,但字母是旋转的。 正确的做法是什么?

python pdf canvas reportlab
1个回答
0
投票

如果您使用更简单的公式来绘制字母,可能会更容易。由于您同时旋转和平移字母,因此很难将字母垂直旋转回去。

使用余弦和正弦似乎是一个好主意,因为您想在不使用旋转的情况下将字母绘制在圆圈中。

这是一个例子:

from reportlab.pdfgen import canvas
import numpy as np

can = canvas.Canvas("rl-hello_again.pdf", pagesize=(595.27, 841.89))
text = 'Hello World'
length = len(text)
can.translate(400, 293)
degrees_perletter = 180 / length
i = 0
for n in text:
    x = np.sin(np.radians(degrees_perletter * i)) * 20
    y = np.cos(np.radians(degrees_perletter * i)) * 20
    can.translate(x, y)
    can.drawString(0, 0, n)
    i += 1

can.showPage()
can.save()

© www.soinside.com 2019 - 2024. All rights reserved.