[reportlab-users] Dual pass numbered canvas causes images to disappear

Tim Roberts timr at probo.com
Wed Nov 12 17:09:31 EST 2008

Chris Kratz wrote:


> When generating a pdf, if I use the normal canvas, my image shows up

> just fine in the generated pdf.


> doc.build(story, onFirstPage=self.firstPage, onLaterPages=self.laterPages)


> But, I need the ability to print page x of y in the footers. So, I

> used a slightly modified version of the NumberedCanvas class found

> here: http://code.activestate.com/recipes/546511/


> So, when I call doc.build with the NumberedCanvas, I get the Page x of

> y in the footer, but images just seem to not show. No errors or any

> other indications of problems, just no images. The document size

> seems to indicate the image is included in the pdf, even if it isn't

> displaying.

> ...

> Anyone have any ideas why NumberedCanvas might stop images from showing?

Yes. It's because that recipe relies on the undocumented internal
structure of ReportLab, and the structure has changed since it was
written. The standard Canvas showPage procedure sets a flag in the
queued up page that says whether the page contains images or not. When
the page does not contain images, pdfbase\pdfdoc.py skips the
image-handling Postscript code, which is somewhat lengthy. In your
code, if the LAST page does not contain images, then NONE of the pages
will get the image-handling Postscript.

For now, I believe you can short-circuit this by adding
self._currentPageHasImages = 1
before calling "canvas.Canvas.showPage(self)".

> Is there anything I can do to debug this further or a better way to do

> "Page x of y"?

The recommended way is to use doc.multibuild to do a two-pass build: one
to compute the total number of pages, then another to insert that in the
footer. There's also a way to insert a "placeholder" that can be filled
in later at the end of the build, but I don't remember what that's called.

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

More information about the reportlab-users mailing list