Generating PDFs in Python with the ReportLab Toolkit (notes/outline for 2010 Jan 11 presentation to TuPLE : edited post-presentation version)
What is it?
A library for generating PDFs directly from Python code
Formerly called ReportLab PDF Library
- for ReportLab Inc. open source
What are some alternatives?
- ReportLab Toolkit (aka ReportLab PDF Library)
- FLOSS
- cross-platform
- since ca. 2001
- opening existing PDFs to extract stuff requires non-FLOSS add-on.
- I happen to have used a version of this. :-)
- pyPdf (at http://pybrary.net/pyPdf)
- FLOSS
- (‘a modified BSD’)
- pure Python —> should be cross-platform
- newer: 2006-
- additionally,
tries to let you open existing PDFs and read data out of them.- [Joel in audience commented that pyPdf is oriented more around taking
an existing PDF and changing it.]
- [Joel in audience commented that pyPdf is oriented more around taking
- FLOSS
- pisa (aka XHTML2PDF?)
- Generates PDF from HTML and CSS.
- Uses ReportLab Toolkit AND pyPdf.
- GPL
- z3c/pdftemplate ?
- really a repackaging of ReportLab Toolkit?
- PyODConverter
- an interface to OpenOffice.
- LGPL
- at least one Windows-only proprietary $ solution using a COM interface.
- hack up interfaces to other languages for libraries e.g. PHP, Ruby.
Installation
In Feb 2008 I got my version from
http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/README?cvsroot=reportlab
Old-style (as of Version 2.1, late 2007, early 2008):
- simple & easy manual installation
- fully documented
- all changes you perform on file system are fully documented
- IF YOU WILL USE IMAGES, also requires the “Python Imaging Library”.
New-style:
- setup script
- Windows installer for Windows
License
- basically a post-1999 BSD license.
- It’s open source.
- You have to include (a copy of) the copyright notice and license
with any redistribution- even in binary form
- can be in accompanying
manual, documentation, “other materials provided…”
- can be in accompanying
- even in binary form
What’s in this Library?
pdfgen is low-level; Platypus is higher-level.
pdfgen
provides features such as
- canvas (to draw on)
- cursors (to write text)
- text
- can have e.g. fonts, origins, transforms (e.g. rotation, scaling)
- graphical/geometric paths
- e.g. rectangles, ellipses, Bezier curves
- line drawing, fills…
- colors
- images
I ended up using Platypus for my layout.
Platypus
Formatting tools include
- document templates
- page templates
- non-flowing (fixed location on page) parts
- e.g. can have header, footer, page number, watermarks…
- frame into which ‘flowables’ go
- e.g. text, tables, diagrams, hard page breaks…
- non-flowing (fixed location on page) parts
- page templates
- you feed a ‘story’ of ‘flowables’ into document template
to generate the document.
My project used a table-based layout method
- for each page, a table (a flowable)
- fills the page
- holds everything for easy layout
- makes sense for my calendar/datebook with specific layout desired
- Platypus tables can calculate their height and width
- so code can even measure, fiddle things to get desired size/fit.
- I used one ‘fiddle factor’,
but suspect quick & dirty style
of my relevant function was to blame.
- I used one ‘fiddle factor’,