#!/usr/bin/python # -*- coding: utf-8 -*- """Hello-world using ReportLab textobject for greedy word wrapping. I guess this is a little more than a hello-world; see reportlabhello.py. """ import random from reportlab.pdfgen.canvas import Canvas from reportlab.lib.pagesizes import A4 words = ['hello, ', 'world. ', 'hi, ', 'hi there, ', 'furled. '] def start_page(c): c.setFont('Times-Roman', 12) # PostScript points return c.beginText(36, A4[1]-36) # PostScript points def main(): choose = random.Random(9).choice c = Canvas('hellotext.pdf', invariant=True) t = start_page(c) right_margin = A4[0]-36 # PostScript points capitalizing = True for i in range(1000): word = choose(words) if capitalizing: word = word.capitalize() width = c.stringWidth(word) if t.getX() + width > right_margin: t.textLine() if t.getY() < 36: c.drawText(t) c.showPage() t = start_page(c) t.textOut(word) capitalizing = word.endswith('. ') c.drawText(t) c.save() if __name__ == '__main__': main()