[reportlab-users] TOC and page x of y canvas

Robin Becker robin at reportlab.com
Wed Nov 11 07:01:49 EST 2015


On 09/11/2015 20:35, brian wrote:
> Does anyone know the answer to this question:
> http://stackoverflow.com/questions/16041397/python-reportlab-clickable-toc-with-x-of-y-page-numbering
>
>
> I'm doing a similar thing and when I create the TOC with a numbered canvas the
> links don't work.  If I remove the numbered canvas, the links work.  I'd like to
> not have to roll my own TOC solution.
>
> Brian

Perhaps you could post some code, and please not like the link above which 
doesn't work without considerable editing and guessing. The attempt is being 
made there to defer page building until the end which is a bit daft.

Do not use the numbered canvas it is wholly problematic/idiotic. It might be 
possible to defer all page writing until the very end, but Canvas and everything 
else wasn't written with that intention so it's almost certainly not a good idea.

Anyhoo if I do make the code work as intended the links are clickable, but I 
still don't like the idea of using the NumberedCanvas.


So far as I can understand it there is/are two possible reasons to use the 
NumberedCanvas. We need to find the total number of pages and possibly we need 
to avoid having multiple passes.

Well if we don't know the number of Toc entries up front we cannot avoid 
multiple passes because of a variable number of TOC pages. If we know the TOC 
will fit on n pages then it is possible to do both TOC & page x of y with one 
pass. So 1) we assume a for "y" the total number of pages and 2) we assume 
form(s) for the whole TOC.

If that solution won't do then you are probably back to doing multi-build anyway 
and that easily allows for variable length TOC and for collection of the number 
of pages at the end of the story. Three passes will suffice.
-- 
Robin Becker


More information about the reportlab-users mailing list