Removing personal data from SVG file
Solution 1:
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.
Solution 2:
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:
- removes all
inkscape
andsodipodi
attributes - removes all
inkscape
andsodipodi
elements - removes
metadata
element with all ancestors - removes
style
attributes (we use CSS) - only when the variablepreserve-style
is set tofalse
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')