[reportlab-users] Corrupt PDF when including image

Tim Roberts timr at probo.com
Thu Feb 9 15:43:52 EST 2012

Michael Johnson wrote:

> I just started using reportlab (2.5) for a project I'm working on, and the PDFs that I'm generating can't be opened by either Ghostscript 9.04 or Acrobat 9. However, they open fine in Okular and in Gmail's PDF preview. I did some searching and found that for PDFs that include images, sometimes a particular image will cause problems. However, I tried switching to a completely different image and had no better luck. Here's a minimum working Python example:


> #!/usr/bin/env python


> from reportlab.lib.units import inch

> from reportlab.lib.utils import ImageReader

> from reportlab.pdfgen import canvas


> bg = "test_image.png"

> c = canvas.Canvas('test.pdf', 'letter')

The Canvas object does not accept a string as a pagesize. What I see
when I open the PDF is that the page size is whacked out. If you
replace this with the proper method:
from reportlab.lib.pagesizes import letter
c = canvas.Canvas('test.pdf', letter )
then it works fine.

One could argue that it is a bug for Canvas not to check the type. It
expects pagesize to be a 2-tuple. What happens here is that it uses
pagesize[0] as the width and pagesize[1] as the height. In this case,
that's ord('l') and ord('e'), which result in a very small page.

Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.

More information about the reportlab-users mailing list