Skip to content

Instantly share code, notes, and snippets.

@petrelharp
Created July 9, 2014 10:41
Show Gist options
  • Save petrelharp/bbb0bacf999a412870af to your computer and use it in GitHub Desktop.
Save petrelharp/bbb0bacf999a412870af to your computer and use it in GitHub Desktop.
exports named layers in an inkscape svg file to a pdf (via stdout); for example to make animations for a latex presentation
#!/bin/bash
# $1 is the file to extract layers from
# the rest are names of the layers to export
# modified from https://answers.launchpad.net/inkscape/+question/48164
#
# needs xmlstarlet
#
# Notes:
# - doesn't deal with spaces in layer names
type xmlstarlet >/dev/null 2>&1 || { echo >&2 "can't find xmlstarlet"; exit 1; }
TMPFILE=$(mktemp /tmp/output.XXXXXXXXX).svg
cp $1 $TMPFILE
YESLAYERS=${*:2}
ALLLAYERS=$(xmlstarlet sel -t -m "//*[@inkscape:groupmode=\"layer\"]" -v "concat(@inkscape:label,' ')" $TMPFILE|sort -Vr)
NOLAYERS=$(comm -1 -3 <(echo $YESLAYERS|tr ' ' '\n'|sort) <(echo $ALLLAYERS|tr ' ' '\n'|sort))
for layer in $NOLAYERS
do
echo $layer
id=$(xmlstarlet sel -t -m "//*[@inkscape:label=\"$layer\"]" -v "@id" $TMPFILE)
xmlstarlet ed -S -L -d "//*[@id=\"$id\"]" $TMPFILE
done
inkscape --without-gui --export-pdf=/dev/stdout $TMPFILE
[ -f $TMPFILE ] && rm $TMPFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment