我正在尝试使用将要旋转的图像创建PDF文档。 我可以成功创建PDF文档,添加图像并保存,但是一旦我尝试旋转它,我就会遇到很多问题。
我想要了解的一件事是旋转的斧头在哪里,是0,0(PDF左下角)还是其他地方?
这是我目前正在运行的代码:
output = BytesIO()
# create a new PDF with Reportlab
c = canvas.Canvas(output)
c.saveState()
c.translate(X?, Y?) # TODO find this !
c.rotate(45)
c.drawImage('path/to/image.png', position_left, position_top, width=img_width, height=img_height, mask='auto')
c.restoreState()
c.save()
(由于PDF文档(0,0)点在右下角,我有position_left
和position_top
引用文档的左上角,我想要放置图像)
我的问题在于我不知道在c.translate(X?, Y?)
上放置什么值以使图像在其中心轴上旋转,即保持在文档上的相同位置,但是从其自身旋转中心点。
使用c.translate(X?, Y?)
是否可行或是否需要使用高级机制来“旋转”PDF文档上的图像?如果是这样,你能指出我正确的轨道吗?
感谢您的帮助。
答案 0 :(得分:1)
您可以使用以下SO Thread
中提到的技术A simple method for rotate images in reportlab
from reportlab.platypus.flowables import Image
class RotatedImage(Image):
def wrap(self,availWidth,availHeight):
h, w = Image.wrap(self,availHeight,availWidth)
return w, h
def draw(self):
self.canv.rotate(90)
Image.draw(self)
I = RotatedImage('../images/somelogo.gif')