[reportlab-users] Reverse order of pages

Andy Robinson andy at reportlab.com
Wed Jun 28 12:29:10 EDT 2006


> Am I right in thinking that somewhere in the bowels of
> reportlab there is some routine that works on a list
> of pages to convert the data to a pdf stream? If there
> is, I could find that list, reverse it, and let
> reportlab do its stuff from that point on.
> 
I was about to explain that it's not that simple, then tried it, and it 
is pretty easy!  Look at the save method of 
reportlab.pdfgen.canvas.Canvas, and add a line as I did below:

     def save(self):
         """Saves and close the PDF document in the file.
            If there is current data a ShowPage is executed automatically.
            After this operation the canvas must not be used further."""
         if len(self._code): self.showPage()
         self._doc.Pages.pages.reverse()    #  REVERSE HERE
         self._doc.SaveToFile(self._filename, self)


This seems to do the right thing.

If you are using the Canvas you can subclass it.  If using Platypus,
it's best again to subclass the Canvas and pass in your new class
as an argument to BaseDocTemplate.build; there is a keyword argument for it.

I am not sure it's worth adding a 'reverse' feature just now, as more 
generally the feature people need when printing is to do arbitrary
permutations (e.g. 1,2,3,4  ->  2,3,1,4) and also arrangements of pages 
onto one larger page.  But hopefully this will get you there.

- Andy







More information about the reportlab-users mailing list