Generate PDF document from SVG template in Django with Inkscape

In the last blog post, I show how to convert a svg vector image to a pdf file using Inkscape command line tool. We will go one step further, generate pdf document with provided data and SVG template in Django.

For example, we have a svg template which contains some tokens such as "{{username}}", "{{blah}}":

1/ open the svg template, get its content (it's xml), and close it.

svg_file = open("/path/to/data.svg", 'r')
svg_content =

2/ Process content of the svg: replace pre-defined token (e.g. '{{username}}' ) with your data (e.g superman)

new_content = svg_content.replace("{{username}}", "superman")

If you pass variable to replace, make sure it is a string, not an Unicode object:

new_content = svg_content.replace("{{username}}", str(myusername))

3/ Create a new file with '.svg' extension (e.g data.svg), then write the processed data to that file

4/ Using the Inkscape command line tool to convert that svg file to pdf as in my previous blog post (

5/ In Django view, read the pdf file and then render it in the template with embedded tag:

<embed src="{{ STATIC_URL }}account_info/new_created/data.pdf" width="100%" height="1150px" type="application/pdf">