Skip to content

Instantly share code, notes, and snippets.

@dreness
Last active April 1, 2021 02:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreness/1ebb2a9b3175ccdaa1fd109f84e30243 to your computer and use it in GitHub Desktop.
Save dreness/1ebb2a9b3175ccdaa1fd109f84e30243 to your computer and use it in GitHub Desktop.
Batch convert SVGs to PDF using Inkscape
#!/bin/zsh
IMAGE_DIR=$1
INK="/Applications/Inkscape.app/Contents/MacOS/inkscape"
SCRIPT="inkscript"
COUNT=0
# If gdate is available (brew install coreutils), we can show precise timing info
type -a gdate &> /dev/null && HAS_GDATE=1
# Make a script for Inkscape's --shell mode
makeInkScript() {
for file in *.svg ; do
echo << EOF >> ${SCRIPT}
file-open:${file}
export-filename:${file}.pdf
export-type:pdf
export-do
file-close:${file}
EOF
COUNT=$((COUNT += 1))
done
echo "Batching ${COUNT} images..."
}
doAllThatStuff() {
cat ${SCRIPT} | ${INK} --shell &> /dev/null
}
pushd $IMAGE_DIR
[ ! -z $HAS_GDATE ] && INK_START=$(gdate +%s.%N)
makeInkScript
[ ! -z $HAS_GDATE ] && INK_END=$(gdate +%s.%N)
if [ ! -z $HAS_GDATE ] ; then
echo -n "Time required to compose Inkscape script: "
echo $((INK_END - INK_START))
fi
[ ! -z $HAS_GDATE ] && CONVERT_START=$(gdate +%s.%N)
doAllThatStuff > /dev/null
[ ! -z $HAS_GDATE ] && CONVERT_END=$(gdate +%s.%N)
if [ ! -z $HAS_GDATE ] ; then
echo -n "Time required to convert images: "
echo $((CONVERT_END - CONVERT_START))
fi
popd
@beren12
Copy link

beren12 commented Mar 26, 2021

👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment