[reportlab-users] Writing a platypus DocTemplate that will fit all flowables on a single page
robin at reportlab.com
Tue Nov 28 08:04:09 EST 2006
Peter Russell wrote:
> I have an application where I need to be sure that user content all fits
> on a single page in two equally sized columns. To do this I may need to
> shrink the font size of flowables, after they have been constructed, and
> passed to the build method of the DocTemplate.
> Is this sort of thing possible, or will I need to use lower level
> constructs to achieve this?
> Also I don't suppose anyone knows of a good algorithm for distributing
> blocks between two columns such that the two columns have equal height?
get all of the relevant flowables in a list. Find their heights using the wrap
method with an infinite height. You'll need to account for the inter flowable
spaces eg space before space after etc. That gives you a good start point.
The _listWrapOn function in flowables.py does this for you. The KeepInframe
flowable (in the same file) does the shrink trick for a single frame. The
complexity there should give you an idea of what you're up against. When shrink
is operative the wrap method is searching for a scaling that allows the given
list of flowables to fit into the remaining space of the current frame. It does
this by solving for a scaling that will make things fit.
Suppose you misuse KeepInFrame by providing it with an empty frame that's twice
as long as your two frames and just as wide. The scaling that allows your
flowables to fit should be near the required scaling for your pair of frames.
The hard part would be instrumenting that algorithm to provide you with the
split point. Do you want to allocate only whole flowables to each frame or can
the split. If you can do the split then a pair of KeepInFrames will now satisfy.
Provided the split is fairly equal the final scalings should be close.
More information about the reportlab-users