开发者

ReportLab Image is drawn" on the canvas.Canvas

ReportLab's Image is coming out mirrored on the PDF Canvas with the following code snippet:

from reportlab.pdfgen import canvas
from reportlab.platypus import Image

pdf = canvas.Canvas(filename, bottomup=0)

logo_image = Image(
    "%s/images/wsp_logo.jpg" % settings.STATIC_ROOT,
    width=200,
    height=200) 
logo_image.drawOn开发者_如何学C(pdf, 100, 100)

How to have it drawn 'normally', as one would expect to see it?


Use canvas.scale function to flip the image.

canvas.saveState()
canvas.translate(x, y)
canvas.scale(1,-1)
canvas.drawImage(img_path, 0, 0, width=-width, height=-height, mask='auto')
canvas.restoreState()


I can't test at the moment, but it's possibly because of bottomup = 0 in your creation of the Canvas object. The default is 1. From the documentation:

The bottomup argument switches coordinate systems. Some graphics systems (like PDF and PostScript) place (0,0) at the bottom left of the page others (like many graphical user interfaces [GUI's]) place the origen at the top left. The bottomup argument is deprecated and may be dropped in future

Need to see if it really works for all tasks, and if not then get rid of it

Given the warnings in that quote, my guess is that setting it to 0 is the source of the problem.


To draw picture with the origin point(0,0) at bottom-left corner.

canvas.saveState()
canvas.transform(1, 0, 0, -1, 0, H)
draw_your_picture_code()
canvas.restoreState()

H is the height of page.


So I ran into the same issue, and I found barbaric to have to change canvas properties just to change the image. In my case I was importing the image from an url:

img = ImageReader(url)

ImageReader class has an attribute _image which is a PIL Class (ex: <PIL.PngImagePlugin.PngImageFile>)

and I could flip it this way:

    import PIL
    from reportlab.lib.utils import ImageReader

    img = ImageReader(url)
    img._image = img._image.transpose(PIL.Image.FLIP_TOP_BOTTOM)


I think it would be more appropriate if you changed the title of the question to be congruent with the actual question.

I had this same problem recently where a logo came up mirrored for no reason, while another image file did not show up mirrored with the exact same code. I couldn't explain the problem, but I don't know how reportlab is using the PIL library or what algorithms are at play in the background. I had a logo with very strange angles, so it probably tried to mirror it while it did debug in the background to assume that the original image was mirrored.

Without actually mirroring the image file myself (to allow reportlab to re-mirror it), I created this hack with the canvas (and using drawImage) to make the logo show up as normal:

Where 'c' is my canvas variable:

        c.saveState()
        c.scale(1,-1)
  
        c.drawImage(logo1, width=100, x=100, y=-100, preserveAspectRatio=True, mask='auto')

        c.restoreState()

I see that someone has also provided a similar answer. But when you scale the canvas to (1,-1) - my answer can add to the idea that you should use a y value of about -100 on the image so that you will get to see the image on the canvas if using the A4 or letter page size.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜