Wednesday, May 27, 2009

Render tables with pangocairo like reportlab

Hello,
lately I was wondering if there was any alternative to the well known reportlab python software for creating PDF reports. I immediately thought about Cairo. The only two problems are:
  • Cairo doesn't create multiple pages
  • No support for creating tables containing text, necessary for table-based reports
I still can't realize how to achieve the first feature, but the second one could be solved using Pango layouts.
The idea is to create the cells of the table using such layouts, so that the text get wrapped etc.

Here's the Pango tables code snippet containing the necessary classes for achieving the job. Notice that the methods in the snippet often make use of Pango units instead of pixels.
Now let's use the Table class as follows: create a table with two rows and two columns, then show it twice with different background colors.

surface = cairo.PDFSurface ("test.pdf", 300, 400)
cr = cairo.Context (surface)
cr = pangocairo.CairoContext (cairo.Context (surface))

sizes = [pango.SCALE*12*10, pango.SCALE*12*10]
data = [["first test with pango tables", "seems to work correctly"],
["though it needs", "support for borders and spans"]]

table = Table (cr, sizes, data, pango.FontDescription ("Sans 12"))
cr.rectangle (0, 0, pango.PIXELS(table.get_width ()), pango.PIXELS(table.get_height()))
cr.set_source_rgb (0.8, 0.8, 0.8)
cr.fill ()
cr.set_source_rgb (0, 0, 0)
table.show_table (cr)

cr.translate (0, 200)
cr.rectangle (0, 0, pango.PIXELS(table.get_width ()), pango.PIXELS(table.get_height()))
cr.set_source_rgb (0.4, 0.5, 0.7)
cr.fill ()
cr.set_source_rgb (0, 0, 0)
table.show_table (cr)

Here's the result test.pdf:


3 comments:

Selvam said...

Thanks for clean details,

How could i add border each column and rows ?

Selvam said...

well,i have added border support for table.

If anyone needs,i can pass it.

pandith13 said...

Good post thanks for share information.
SRI ANNAPOORNESHAWARI ASTROLOGY CENTER.Best Astrologer In Indiana