[reportlab-users] number of pages
Andy Robinson
andy at reportlab.com
Tue Jun 28 09:09:30 EDT 2005
Bogdan Maryniuk wrote:
> However, at least one (ugly) workaround still exists:
>
> 1. Leave empty place, like: "Pages 1 of ".
> 2. Process document and count the pages.
> 3. Process document AGAIN and write these numbers on that place in the forms.
>
> Caveat: if you want right-justified or centered text, you may fail aligning it
> properly, since you don't know exact width of the currently placed text. One
> more workaround for this is to process the document three times: first, you
> know how much pages, second, you know the width of the text for each page
> and finally you paint this stuff properly. :-)
>
> Although I am really intersted what Robin would reply on this...
>
This is close, but we have had the answer for many years in RML
without a big effort in (3)... it needs a little adjustment for
Platypus. You use a "Form XObject", which is a kind of
external graphic subroutine. These must exist at the time the
document is saved, but can be defined 'late' when you know the
number of pages.
1 "Pages 1 of " in page template
2. execute
canvas.doForm("pageCount")
in the page template, where the pageCount form has not been
defined yet.
3. at the very end of your script but before saving, execute
something like this...
canvas.beginForm("pageCount")
canvas.drawString(x, y, canvas.getPageNumber())
canvas.endForm()
This will be easy if using pdfgen but a bit harder in Platypus.
I just scanned our test suite and realized we did not leave
any examples of this. Also, there is no obvious "end of story
and about to save" hook in Platypus; right now you would need
to put code like (3) in an overrided _endBuild method of
BaseDocTemplate. I should add a handle_documentEnd method
and an example to the test suite.
A similar technique lets you draw a table of contents on page 1
if you can accept a maximum-fixed-size rectangle for your document.
This means everything can be done in one pass very quickly. More
accurate typography for more variable situations needs multi
pass rendering.
Hope this helps, I will try to do a usable example today.
- Andy Robinson
More information about the reportlab-users
mailing list