[reportlab-users] Dynamic page sizes

Robin Becker robin at reportlab.com
Wed Oct 19 04:45:31 EDT 2011


The document object has methods to interrogate page number etc etc if that's
what you want, but usually the way forward is to arrange for a number of
suitable page templates to exist with the right page sizes, frames and
decorative onpage etc etc attributes.

Then in the story you need to start with the right template before the first
page is started, and when you come to the end of an item you prepare for the
next page with a sequence like

story.append(......myflowables for a big poem)

Usually the start page template is a cover page or some such so make that the
first template in the list you set up in the document. With this kind of
approach you shouldn't need to interrogate the current document state too much.

Of course it requires you to know in advance what page size/frame size you
require. If that is purely content driven then you'll need to use the flowables'
wrap method to determine what each flowable requires etc etc.
Robin Becker
On 18/10/2011 17:57, Glenn Linderman wrote:

> On 10/18/2011 1:44 AM, Robin Becker wrote:

>> On 17/10/2011 18:30, Glenn Linderman wrote:

>>> I publish poetry in PDF format, because friends with iOS devices and iBooks

>>> software have informed me that the PDF format is great for use with iBooks...

>>> each page can contain one poem, and double-tapping the screen causes iBooks to

>>> "zoom to width" of the text, presenting a nice, screen-filling,

>>> vertically-scrolling-if-needed page containing the poem at the largest font size

>>> that avoids line breaks.


>>> I generate the PDF files using 8.5x11 paper size, and 10 point font.


>>> The question: How could I generate the PDF file with variant page sizes (widths

>>> primarily, but possibly height as well), such that each poem would have a page

>>> size that just fits the poem? I speculate this would avoid the need for the

>>> double-tap when viewing on iOS devices.


>>> Glenn

>> ........

>> Glenn,


>> what method do you use to create the standard letter size documents? ReportLab

>> has both page oriented and document oriented mechanisms built in.


>> If you use a Canvas ie page oriented then it's possible to change the page

>> size that the canvas will use.


>> #######

>> from reportlab.pdfbase.ttfonts import TTFont

>> from reportlab.pdfbase.pdfmetrics import registerFont

>> from reportlab.pdfgen.canvas import Canvas

>> from reportlab.lib.colors import green

>> registerFont(TTFont("Vera", "Vera.ttf"))

>> registerFont(TTFont("VeraBI", "VeraBI.ttf"))

>> c = Canvas('helloworld.pdf', pagesize=(300,500))

>> c.setFont('Vera', 10)

>> c.drawString(100, 200, u'Hello World')

>> c.showPage()

>> c.setPageSize((400,600))

>> c.setFont('Vera', 20)

>> c.setFillColor(green)

>> c.drawString(100, 300, u'Hello World')

>> c.save()

>> #######


>> if you use the document oriented layout then page sizes are normally

>> determined by a page template so you have to switch the template before the

>> next page throw etc etc


> I'm using platypus right now, although I've seen sample code that does

> drawstring stuff... much more cumbersome. But I don't know how much info I can

> get back out of platypus after the fact. One idea I had was to use a 2nd,

> temporary document to create each poetry page, and then "obtain metrics" (how

> much space was used, and did it actually fit on one page), but I have no clue

> (and found no APIs) how to "obtain metrics".


> I think I'm missing informational APIs: how does one tell what page is being

> generated, or which frame is being filled? and bounding rectangles for the

> generated text in a frame? Am I overlooking something, or is this information

> not available from platypus?


More information about the reportlab-users mailing list