[reportlab-users] Dynamic page sizes

Glenn Linderman v+python at g.nevcal.com
Tue Oct 18 12:57:32 EDT 2011

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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20111018/08c29d2a/attachment.html>

More information about the reportlab-users mailing list