Skip to main content

Ralsina.Me — Roberto Alsina's website

Using vector images in reportlab

One of the big lim­i­ta­tions of re­port­lab is that it has no sup­port for vec­tor-based im­ages. You can't in­sert SVG, EPS or any oth­er vec­tor-based for­mat in your doc­u­ments.

Un­til now.

By hi­jack­ing an­oth­er app called uni­con­ver­tor, I have man­aged to in­sert as vec­tors SVG im­ages in a re­port­lab doc­u­men­t.

Here's the hack­ish code:

import sys,os
from app.io import load
from app.plugins import plugins
import app
from reportlab.platypus import *
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch

app.init_lib()
plugins.load_plugin_configuration()


class SVGImage(Flowable):
  def __init__(self,imgname):
    self.doc = load.load_drawing(imgname)
    for k in dir(self.doc):
      print k
    self.saver = plugins.find_export_plugin(plugins.guess_export_plugin(".pdf"))
    Flowable.__init__(self)

  def wrap(self,aW,aH):
    br=self.doc.BoundingRect()
    return br[2],br[3]

  def drawOn(self,canv,x,y,_sW=0):
    canv.translate(x,y)
    self.saver (self.doc,".ignoreme.pdf",options={'pdfgen_canvas':canv})
    os.unlink(".ignoreme.pdf")

styles = getSampleStyleSheet()
def go():
    doc = SimpleDocTemplate("phello.pdf")
    Story = [Spacer(1,2*inch)]
    style = styles["Normal"]
    p = SVGImage('warning.svg')
    Story.append(p)
    doc.build(Story)

go()

It has sev­er­al prob­lems (see where the sec­ond para­graph end­ed), but it does work.

To run it, you need a warn­ing.svg file (ed­it as need­ed) and you run it this way (re­plac­ing the path as prop­er for your se­tup):

PYTHONPATH=/usr/lib/python2.5/site-packages/uniconvertor/ python use_uniconv.py

In fac­t, this is not lim­it­ed to SVG files. You should be able to use the fol­low­ing for­mat­s:

  • Corel­­DRAW ver.7-X3,X4 (C­­DR/CDT/C­CX/C­­DRX/CMX)

  • Adobe Il­lus­­tra­­tor up to 9 ver. (AI post­script based)

  • Post­script (P­S)

  • En­­cap­­su­lat­ed Post­script (EP­S)

  • Com­put­er Graph­ics Metafile (CG­M)

  • Win­­dows Metafile (WM­F)

  • XFIG

  • Scal­able Vec­­tor Graph­ics (SVG)

  • Sken­­cil/S­ketch/sK1 (SK and SK1)

  • Acorn Draw (AF­F)

Old fashioned mail: Cone

I had one too many prob­lems with kmail from KDE4 in my eee with Kubun­tu, and sylpheed-­claws is just un­us­able in a small screen (the huge wid­get­s! the non-hid­able things all over the in­ter­face!) I de­cid­ed to get old fash­ioned and try a con­sole mail read­er.

I was a pine us­er for many years, and a mutt us­er for a while, and I was deeply dis­ap­point­ed that the last three years have been bad for these pro­gram­s.

Just be­cause you run in a ter­mi­nal, there's no rea­son to be hard to con­fig­ure! Af­ter spend­ing 20 min­utes try­ing to get a de­cent IMAP ac­count set­up (just two IMAP ac­counts) in alpine and an­oth­er 10 won­der­ing if mutt re­al­ly had no place in the UI for ac­count con­fig­u­ra­tion (and whether the De­bian/KUbun­tu de­fault "com­ment­ed" con­fig file is the prod­uct of hard drugs), I tried Cone.

It was blis­s. It was all I re­mem­bered from pine years ago (on lo­cal ac­counts) on­ly over IMAP.

Easy to con­fig­ure, easy to use, quick, ca­pa­ble. I was in love all over again.

While I will keep us­ing KMail on my main note­book un­der Arch Lin­ux where I had no re­li­a­bil­i­ty prob­lems what­so­ev­er, I have Cone con­fig­ured in my server's shell ac­count and in the eee.

This week's release plan: Uqbar

On my on­go­ing some­thing-re­leased-ev­ery-fri­day ram­page, this fri­day I will re­lease a work­ing ver­sion of Uqbar, a Guten­berg project e-­texts in­ter­face.

It may use use pan­doc and rst2pdf and makeztxt and oth­er things to make e-­books that are pret­ty to read, and work on my Sony Clie. We'll see.

This project has been "sleep­ing" since 2005, I think I can now, with my new fo­cus in lim­it­ed, quick de­vel­op­men­t, get a rea­son­able ver­sion ready in two week­s.

I may slip a lit­tle rst2pdf up­date, too.

Af­ter that, back to Urssus.

rst2pdf: What will be new tomorrow

Keep­ing with my new time-based re­lease sched­ule, to­mor­row is again rst2pdf re­lease day! What will be new in 0.7? Sev­er­al things!

  • Fixed sev­er­al se­ri­ous bugs, spe­­cial­­ly with the han­dling of lit­er­al blocks (pre­­for­­mat­t­eds and code block­­s).

  • Im­­ple­­men­t­ed a rather neat side­bar/float­ing block mech­a­nis­m. It on­­ly lets you go float-left or float-right but it looks bet­ter than what you get in HT­M­L, at least:

sidebar-beige

You can even use it to float al­most ar­bi­trary ob­ject­s, so you can have float­ing im­ages or float­ing fig­ures.

  • Fixed the look of hy­per­links. Re­­port­lab had a bug about mak­ing some hy­per­links un­der­­lined, and those with a black thick un­der­­line. Not when you use rst2pdf! Mon­key-­­patched the heck out of that.

  • Ta­ble styling. Let me show you:

rst2pdf-tablestyles

The first row is a head­er row. It au­to­mat­i­cal­ly takes the table-­head­ing style.

The fol­low­ing rows are reg­u­lar, and they take the ta­ble style, which has sup­port for ze­bra ta­bles with al­ter­nat­ing col­ors (white and gray here).

The lone red cell at the right is spe­cial. Its con­tent is this:

.. class:: red

red

If you don't know ReST, that means "red" is a para­graph with class red, so it will be styled what­ev­er way that means (here: red back­ground).

Usu­al­ly that would mean you have a white (or gray) cell with a red para­graph in it. That looks in­cred­i­bly ug­ly.

So, rst2pdf tries to be clev­er: If there is a sin­gle el­e­ment in a cel­l, it will try to guess the cell back­ground from it.

And as you saw above, it works :-)


Contents © 2000-2020 Roberto Alsina