Removing personal data from SVG file

The easiest way to do this is to save the file as an Optimized SVG file. This option is available in the Save As... dialog dropdown. When you click save a dialog will popup asking you what to remove. Make sure the box Keep Editor Data is left unchecked.


I have a lot of SVGs from different sources, so I was looking for an automated solution.

1. SVGO (NodeJS, javascript)

There is a very good NPM package: SVGO

Since I use gulp as a task-runner, so the gulp-svgmin (or gulp-svgo) is very helpful

2. XSLT (transform SVG)

The other automated approach — use XSLT. Here is an XSL transformation I have used to cleanup SVG.

This XSLT does the following:

  1. removes all inkscape and sodipodi attributes
  2. removes all inkscape and sodipodi elements
  3. removes metadata element with all ancestors
  4. removes style attributes (we use CSS) - only when the variable preserve-style is set to false

As output you will get the clean SVG file.

How to run transformation

Javascript (gulp build)

If you use gulp as build system, you could use this plugins:

  • gulp-xslt
  • or gulp-saxon requires the presence of the saxon.jar in the system

Python and lxml.etree

Use this short python script:

#!/usr/bin/python

import lxml.etree as ET

def clean_file(path, pathto):
    svg1 = ET.parse(path)

    xslt = ET.parse('cleanup-svg.xsl')
    transform = ET.XSLT(xslt)
    svg2 = transform(svg1)
    with open(pathto, 'w') as f:
        f.write(ET.tostring(svg2, pretty_print=True))

clean_file('a.svg', 'b.svg')

Tags:

Svg

Inkscape