[reportlab-users] Strange problem with an inline image in a Paragraph

Lele Gaifax lele at metapensiero.it
Thu Jun 25 11:48:31 EDT 2020


Hi all,

I have a problem with adding an inline image in a Paragraph(), using raw
Frame(). My application builds a PDF document using a sequence of
"rectangles", where each rectangle gets drawn by a particular function.

A few days ago I started seeing something completely unexpected and
inexplicable: I have tree such rectangles in a row, each containing a single
image and some text, and one of them comes out with a double image!

Here is an extremely cut down self-contained example script that exhibits the
problem:

    from reportlab.pdfgen.canvas import Canvas
    from reportlab.platypus import Frame, Paragraph
    from reportlab.lib.styles import getSampleStyleSheet
    from reportlab.rl_config import defaultPageSize
    from reportlab.lib.units import cm

    from PIL import Image, ImageDraw


    def go():
        im = Image.new('L', (50, 50))
        draw = ImageDraw.Draw(im)
        draw.line([(0, 0), (50, 50)], fill=100, width=3)
        draw.line([(0, 50), (50, 0)], fill=200, width=3)
        with open('/tmp/strange.jpeg', 'wb') as f:
            im.save(f, 'JPEG')

        styles = getSampleStyleSheet()
        with open("strange.pdf", "wb") as output:
            c = Canvas(output, pagesize=defaultPageSize)

            # This works: a single image appears
            
            f = Frame(1*cm, 2*cm, 1*cm, 1*cm)
            s = styles["Normal"]
            t = '<img src="/tmp/strange.jpeg" valign="middle"/>'
            p = Paragraph(t, s)
            f.addFromList([p], c)

            # This "fails": a "twin" image appears
            
            f = Frame(1*cm, 4*cm, 1*cm, 1*cm)
            s = styles["Normal"]
            t = '<img src="/tmp/strange.jpeg" valign="middle" height="25" width="25"/>'
            p = Paragraph(t, s)
            f.addFromList([p], c)

            c.showPage()
            c.save()


    if __name__ == "__main__":
        go()

Am I missing something, or doing something wrong?

Thanks in advance for any hint,
ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele at metapensiero.it  |                 -- Fortunato Depero, 1929.



More information about the reportlab-users mailing list