I can't seem to find this particular need online, but I thought for sure that someone has been able to accomplish this.
I have a ReportLab document (BaseDocTemplate) with one page template on the first page, and a different page template on the second page. The first page template has 6 frames while the second just has one (see the image below).
What I am trying to accomplish is this: when direction_frame
has filled with text (which is of unknown size), the overflow text then goes into direction_frame2
on the second page.
Of course, what's happening now is that overflow text from direction_frame
is then flowing into title_frame
on the second page, because the document is duplicating the first page's PageTemplate after it overflows, not having inserted NextPageTemplate()
and PageBreak()
into the story at any point.
It seems, then, what I need is either:
To find some way to discern the size of each paragraph as it is added to the story so that I can know when it's about to overflow, calling NextPageTemplate()
and PageBreak()
at that point, or
To find some way to tell ReportLab to flow into direction_frame2
on second_page_template
once direction_frame
is full.
Here are some snippets of code to see how I'm setting this up:
# create the page templates
first_page_template = PageTemplate(
id='first_page_template',
frames=[title_frame, expected_frame, top_frame_left, top_frame_right, cook_frame, direction_frame])
second_page_template = PageTemplate(
id='second_page_template',
frames=[direction_frame2])
# create the document
doc = BaseDocTemplate(
'print.pdf',
pagesize=letter,
pageTemplates=(first_page_template, second_page_template)
)
# add the various elements to the story
story = []
... add text to the first 5 frames ...
# add the direction parts as paragraphs
story.append(FrameBreak())
for i in range(len(directions)):
story.append(Paragraph(directions[i], style=normal_paragraph))
I had attempted to move to the next page template after adding x-number of paragraphs, but this is highly inaccurate as the length of each paragraph can vary widely.
I figured it out! I found the answer purely by accident. As I was trying different things in my IDE, the argument I needed came up. The solution was to add autoNextPageTemplate=1
in the arguments for first_page_template
. This apparently tells the document to automatically switch to the next page template after all the frames on that template have been filled. So, my code wound up being:
# create the page templates
first_page_template = PageTemplate(
id='first_page_template',
frames=[title_frame, expected_frame, top_frame_left, top_frame_right, cook_frame, direction_frame],
autoNextPageTemplate=1) # <== This accomplished what I was looking to do ***
second_page_template = PageTemplate(
id='second_page_template',
frames=[direction_frame2])
# create the document
doc = BaseDocTemplate(
'print.pdf',
pagesize=letter,
pageTemplates=(first_page_template, second_page_template)
)
# add the various elements to the story
story = []
... add text to the first 5 frames ...
# add the direction parts as paragraphs
story.append(FrameBreak())
for i in range(len(directions)):
story.append(Paragraph(directions[i], style=normal_paragraph))
So, I'm leaving this here in case it helps someone else looking to do the same thing.
Edit: It appears that this isn't to be found in ReportLab's document pages on their website, but it does appear in the release notes for version 2.6: