Download SVG as JPEG using flask
<p>I'm trying to serve HTML page with SVG attributes on it; so as soon as I click "Create" I want to be able to download that file as .jpg instead of SVG. I looked over at multiple convertors that works with the command line for instance like this.</p>

<pre><code>os.system("rsvg-convert -h 32 save.svg &gt; icon-32.jpg")
</code></pre>

<p>Similiary, I want to be able to click create and download the .jpg version of my file. 
I'm currently trying to do this using Flask.</p>

<pre><code>without_filter = &lt;svg viewBox="0 0 400 150" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="background: #40484b;"&gt;
    &lt;defs&gt;
        &lt;!-- if you use the same shape multiple times, you put the reference here and reference it with &lt;use&gt; --&gt;
        &lt;rect id="path-1" x="25" y="25" width="100" height="100"&gt;&lt;/rect&gt;
        &lt;rect id="path-3" x="150" y="25" width="100" height="100"&gt;&lt;/rect&gt;
        &lt;rect id="path-5" x="275" y="25" width="100" height="100"&gt;&lt;/rect&gt;

        &lt;!-- gradient --&gt;
        &lt;linearGradient x1="50%" y1="0%" x2="50%" y2="100%" id="gradient"&gt;
            &lt;stop stop-color="#FF8D77" offset="0%"&gt;&lt;/stop&gt;
            &lt;stop stop-color="#FFBDB1" offset="50%"&gt;&lt;/stop&gt;
            &lt;stop stop-color="#F4E3F6" offset="100%"&gt;&lt;/stop&gt;
        &lt;/linearGradient&gt;

        &lt;!-- clip-path --&gt;
        &lt;clipPath id="clip"&gt;
            &lt;circle cx="325" cy="75" r="50"&gt;&lt;/circle&gt;
        &lt;/clipPath&gt;

        &lt;!-- filter --&gt;
        &lt;filter x="-50%" y="-50%" width="200%" height="200%" filterUnits="objectBoundingBox" id="glow"&gt;
            &lt;feGaussianBlur stdDeviation="15" in="SourceGraphic"&gt;
                &lt;animate attributeName="stdDeviation" attributeType="XML"
                    begin="0s" dur="0.25s" repeatCount="indefinite"
                    values="15;20;15"/&gt;
            &lt;/feGaussianBlur&gt;
        &lt;/filter&gt;
    &lt;/defs&gt;
    &lt;g fill-rule="evenodd"&gt;
        &lt;use xlink:href="#path-1" filter="url(#glow)" fill="#FF8D77" fill-opacity="0.5"&gt;&lt;/use&gt;
        &lt;use xlink:href="#path-3" fill="url(#gradient)"&gt;&lt;/use&gt;
        &lt;use xlink:href="#path-5" fill="#FF8D77" clip-path="url(#clip)"&gt;&lt;/use&gt;
    &lt;/g&gt;
&lt;/svg&gt;
</code></pre>

<p>So I'm trying to download the (html) as .jpeg as soon as the user clicks "create"
Here's my view function.</p>

<pre><code>@app.route('/filters/&lt;filename&gt;', methods=['GET','POST'])
def AddFiltersTag(filename):
    form = AddFilterTags(request.form)
    if form.validate_on_submit():
        html = re.sub(r'(.*?&lt;defs&gt;)(.*)', r'\g&lt;1&gt;' + add_filter_tag + add_filter + end_tag + '\g&lt;2&gt;', without_filter)
        save_html = open('save.svg','wb')
        save_html.write(html)
        os.system("rsvg-convert -h 32 save.svg &gt; icon-32.jpg")
        return download_html
</code></pre>

<p>So that function AddFilterTag gets trigged when a user clicks "Submit" at my form. but in that case I want to download, the above code simply saves the file as .svg and converts it later on using os.system, But I want to actually download the .jpg version directly. Is there a way to do that?</p>

<p>After saving the file and converting it into an image.</p>

<p>You can directly return the file from the directory, which will give a download popup to the user.</p>

<pre><code>...
os.system("rsvg-convert -h 32 save.svg &gt; icon-32.jpg")
return send_from_directory("/path/to/saved/image", "icon-32.jpg")
</code></pre>

<p>Be sure to import the method as follows: </p>

<pre><code>from flask import send_from_directory
</code></pre>

<p>If you're using this in production, make sure nginx or an actual web server is sending the file. You can read more at <a href="http://flask.pocoo.org/docs/0.11/api/#flask.send_from_directory" rel="nofollow">http://flask.pocoo.org/docs/0.11/api/#flask.send_from_directory</a></p>

python flask operating-system jpeg encode