[reportlab-users] Variant nextPageTemplate's: "chapter" pages with subsequent pages
Robin Becker
robin at reportlab.com
Thu Jan 24 11:43:17 EST 2013
On 24/01/2013 16:09, wandering free wrote:
> I am in the process of converting the database from pfaf.org (plant for a
> future) into a pdf book. I have hit a bit of a stumbling block regarding
...........
> The code (as you can probably tell) works fine for single page plants. It
> also works (semi) as expected for multi page plants.
>
> However, as you can also probably see, a two (or four,etc) page plant will
> break the template arrangement, because the above code assumes page
> position based upon plant number, rather than page number.
>
> I can't see a solution for this in the location of the code above (i.e
> during the Story.append cycle) - as at that point I can not tell whether
> the plant has used more than one page, and thus what page I am currently
> on, as such.
>
we do have code in the toolkit to allow dynamic behaviours in the story see the
module reportlab/platypus/flowables.py for
DocAssign, DocExec, DocAssert, DocPara, DocIf & DocWhile
these flowables represent really primitive computational constructs which are
evaluated at layout time.
sample from the latest tests folder
> from reportlab.lib.styles import ParagraphStyle
> from reportlab.platypus import SimpleDocTemplate, Paragraph
> from reportlab.platypus.flowables import DocAssign, DocExec, DocPara, DocIf, DocWhile
> normal = ParagraphStyle(name='Normal', fontName='Helvetica', fontSize=8.5, leading=11)
> header = ParagraphStyle(name='Heading1', parent=normal, fontSize=14, leading=19,
> spaceAfter=6, keepWithNext=1)
> story = [
> DocAssign('currentFrame','doc.frame.id'),
> DocAssign('currentPageTemplate','doc.pageTemplate.id'),
> DocAssign('aW','availableWidth'),
> DocAssign('aH','availableHeight'),
> DocAssign('aWH','availableWidth,availableHeight'),
> DocAssign('i',3),
> DocIf('i>3',Paragraph('The value of i is larger than 3',normal),Paragraph('The value of i is not larger than 3',normal)),
> DocIf('i==3',Paragraph('The value of i is equal to 3',normal),Paragraph('The value of i is not equal to 3',normal)),
> DocIf('i<3',Paragraph('The value of i is less than 3',normal),Paragraph('The value of i is not less than 3',normal)),
> DocWhile('i',[DocPara('i',format='The value of i is %(__expr__)d',style=normal),DocExec('i-=1')]),
> DocPara('repr(doc._nameSpace)',escape=True),
> DocPara('doc.canv.getPageNumber()','The current page number is %(__expr__)d',style=normal)
> ]
> doc = SimpleDocTemplate(outputfile('test_doc_programming.pdf'))
> doc.build(story)
you can access the document doc and lots of its properties etc etc. That should
allow you to check the page number and then select a suitable template using
appropriate logic.
> I hoped that I could perhaps tweak the nextPageTemplate structure from my
> custom docTemplate, but I can't figure out if this is possible.
>
> Is it? Or is there another solution? Would really appreciate any help. Have
> been reading all over, but the best examples I can find don't quite cover
> this scenario.
>
> Any questions, please ask. Thank you
--
Robin Becker
More information about the reportlab-users
mailing list