svglue is a small library that takes a template in form of a specially preparedSVG document and fills in text and images to create an output SVG file. Styleinformation like opacity, size, and ordering is kept.
Certainly, Adobe Illustrator is there with its great set features but, Inkscape is nowhere lesser. It is a very flexible vector graphics editor that offers you almost all the functionality you expect from a more pricier version only. Inkscape VS Illustrator Proffesional Extensibility. Inkscape and Illustrator both are ‘feature packed’.
It's mainly intended to be used to set up a nice workflow creating templatesfor PDF generation:
- Create your template in Inkscape, a placeholder text-element where youwant to fill in text later, or a rectangle for filling in images.
- Add a custom attribute
template-id
to every<tspan>
or<rect>
element that you want to replace. Eachtemplate-id
must be a uniqueidentifier. - Using
svglue
, you can programmatically replace every text or rectusing itstemplate-id
with either a raster image, another SVG graphicor replacement text. - Finally, use something like rsvg,CairoSVG or another SVG-renderer to create a PDFdocument.
Example code:
Step 3:
It's important to note that versions <= 0.5 of
cairosvg
have a bug thatrenders the tiger scaled incorrectly. For now, you can use a different rendererfor better results until that bug is fixed.Installation
svglue
is available via PyPI, soa simple pip install svglue
should suffice to install.Please note that
svglue
is only testet with Python 3.3 upwards.API reference
Note that the main target for this library is Python3. The laterversions of cairosvg do not compile on Python2 anymore for me.
If you need Python2 support, restrict the version of svglue to <=0.2.1.
svglue.load(src=None, file=None)
Loads a template, returning a
Template
-object. The src
/file
loadpattern is used through the library - src
is a string containing thesource of the SVG file, or file
can either be a file-like object (with aread()
method) or a filename for the source file. Only one ofsrc
/file
may be specified.Template.set_text(tid, text)
Replaces the text inside the element
<tspan>
(whose id is theactual tid
) with the specified text
.Template.set_image(tid, src=None, file=None, mimetype=None)
Replaces a rectangle whose id is
tid
(<rect>
) with an<image>
tag to link/embed the specified image. If mimetype
is None
,the image is linked (so file
should be the path/URI of thte image).If
mimetype
is given (should be either image/png
or image/jpeg
),the supplied image is stored inline in the resulting SVG document.Template.set_svg(tid, src=None, file=None)
Conceptually similiar to
Template.set_image()
, this replaces a rectanglewith an SVG-image. However, no linking is supported, the SVG is copied into theresulting SVG-documents <defs>
-section and a <use>
-Element replacesthe rectangle.Since
Template.__str__()
(see below) is used to render templates, thisallows nesting templates by simply passing them as the second argument toset_svg()
.Template.__str__()
Renders the template. Returns the template with all supplied info filled in.