Reputation: 39859
I'm trying to create a PDF document with an image that will be rotated. I can successfully create the PDF document, add the image and save it, but as soon as I try rotating it, I'm having many issues.
One thing I'm trying to understand is where is the axe for the rotation, is it at 0,0 (bottom left for PDF) or somewhere else?
Here's my current running code:
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()
(Since PDF documents (0,0) point is at bottom right, I have position_left
and position_top
that refers to the top left point of the document, where I want to place the image).
My issue here is that I don't know how what values to put on c.translate(X?, Y?)
to make the image rotate on its center axis, i.e. stays at the same position on the document, but rotate on itself from its center point.
Is using c.translate(X?, Y?)
would work or do I need to use advanced mechanisms to rotate "just" the image on the PDF document?
Upvotes: 0
Views: 1267
Reputation: 146510
You can use the technique mentioned in below 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')
Upvotes: 1