-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathexample.py
27 lines (20 loc) · 898 Bytes
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import svglue
# load the template from a file
tpl = svglue.load(file='sample-tpl.svg')
# replace some text
tpl.set_text('sample-text', u'This was replaced.')
# replace the pink box with 'hello.png'. if you do not specify the mimetype,
# the image will get linked instead of embedded
tpl.set_image('pink-box', file='hello.png', mimetype='image/png')
# svgs are merged into the svg document (i.e. always embedded)
tpl.set_svg('yellow-box', file='Ghostscript_Tiger.svg')
# to render the template, cast it to a string. this also allows passing it
# as a parameter to set_svg() of another template
src = str(tpl)
# write out the result as an SVG image and render it to pdf using cairosvg
import cairosvg
with open('output.pdf', 'wb') as out, open('output.svg', 'w') as svgout:
svgout.write(src)
cairosvg.svg2pdf(bytestring=src, write_to=out)