[reportlab-users] Re: Broken PDFs with multiBuild and file objects

Robin Becker reportlab-users@reportlab.com
Mon, 6 Jan 2003 11:15:13 +0000

Andy says

> PDF isn't a sequential format, at least not the simple way we
> do it, so you need the whole object tree in memory. We could
> add some new APIs so that the primary, preferred way is to make
> a PDF in memory, and the existing behaviour builds simply on
> this to save to a filename if given.

I'm not sure I understand the relevance here. The PDF is currently
always assembled in memory (in a list) and we then write that only right
at the end. I believe the correct way to handle this for multi-build is
to separate out the build part from the save part. This is what I
attempted to do for rml2pdf.

That required a specialisation of the BaseDocTemplate class to allow for
a different _endBuild.

eg      def _endBuild(self):
                if self._hanging!=[] and self._hanging[-1] is PageBegin:
                        del self._hanging[-1]

                if getattr(self,'_doSave',1): self.canv.save()
                if self._onPage: self.canv.setPageCallBack(None)

this was incorporated in the standard sometime ago and I should have
used it instead of my temprary hack.

then in the standard thing we always save. The multiBuild methods can
then set self._doSave = 0, and iterate till completion.

The real fix then does

        self._doSave = 0
        while 1:
                happy = self._allSatisfied()

                if happy:
                        self._doSave = 0

Robin Becker