Skip to content

Instantly share code, notes, and snippets.

@rbf
Last active May 30, 2019 10:36
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 rbf/a442d108cc5ed1fb5163 to your computer and use it in GitHub Desktop.
Save rbf/a442d108cc5ed1fb5163 to your computer and use it in GitHub Desktop.
Script to export an SVG file to PDF, EPS and PNG (multiple sizes and background colors) using Inkscape from the command line (tested on Mac OS 10.8+).
#!/bin/bash
# The MIT License (MIT)
#
# Copyright (c) 2013-2014 https://gist.github.com/rbf
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Source:
# https://gist.github.com/a442d108cc5ed1fb5163
#
# Install:
# curl -sSL https://gist.githubusercontent.com/rbf/a442d108cc5ed1fb5163/raw/inkscape_export --create-dirs -o /usr/local/bin/inkscape_export && chmod a+x /usr/local/bin/inkscape_export
#
# Inspired from:
# http://tavmjong.free.fr/INKSCAPE/MANUAL/html/CommandLine-General.html
# http://tavmjong.free.fr/INKSCAPE/MANUAL/html/CommandLine-Export.html
# http://how-to.wikia.com/wiki/How_to_use_Inkscape_in_commandline_mode
log_err(){
echo "$@" 1>&2
}
FORCE_GENERATE_ALL="false"
if [ "${1}" == "--all" ] || [ "${1}" == "-a" ]
then
FORCE_GENERATE_ALL="true"
shift
fi
GENERATE_ONLY_TRANSPARENT_PNG="false"
if [ "${1}" == "--png" ]
then
GENERATE_ONLY_TRANSPARENT_PNG="true"
shift
fi
EXPORT_AREA="drawing"
if [ "${1}" == "--page" ] || [ "${1}" == "-p" ]
then
EXPORT_AREA="page"
shift
fi
if [ "${1%%=*}" == "--size" ]
then
if [ "${1}" == "--size" ]
then
log_err "--size needs an integer argument between 1 and 9999."
exit 4
fi
EXPORT_PNG_SIZE="${1##--size=}"
shift
fi
if [ "${1%%=*}" == "--sizes" ]
then
if [ "${1}" == "--sizes" ]
then
log_err "--sizes needs an argument following the pattern '--sizes=[first,[incr,]]last' similar to the command 'seq'."
exit 4
fi
EXPORT_PNG_SIZES_RANGE="${1##--sizes=}"
shift
fi
SOURCE_FILE="${1}"
if [ -z "${SOURCE_FILE}" ]
then
log_err "No source file specified."
log_err "Nothing to do."
exit 1
fi
if ! [ -f "${SOURCE_FILE}" ]
then
log_err "File '${SOURCE_FILE}' not found!"
log_err "Nothing to do."
exit 2
fi
if [ "${SOURCE_FILE//*.}" != "svg" ]
then
log_err "File must be of type 'svg'."
log_err "I don't know what to do with file '${SOURCE_FILE}'."
log_err "Nothing to do."
exit 3
fi
SOURCE_FILE_BASENAME="$(basename ${SOURCE_FILE} .svg)"
SOURCE_DIR_RELATIVE="$(dirname ${SOURCE_FILE})"
SOURCE_DIR="$(cd ${SOURCE_DIR_RELATIVE}; pwd)"
TARGET_DIR="${SOURCE_DIR}"
TARGET_PNG_SUBDIR="png"
TARGET_DIR_PNG="${TARGET_DIR}/${TARGET_PNG_SUBDIR}"
echo "Processing ${SOURCE_FILE}"
echo "File basename: ${SOURCE_FILE_BASENAME}"
echo "Source dir: ${SOURCE_DIR}"
INKSCAPE_CMD="/Applications/Inkscape.app/Contents/Resources/script"
EXPORT_VECTOR_FORMATS="pdf eps"
EXPORT_PNG_SIZES=(2500 1000 512 256 128 64 48 32 24 16)
if [ -n "${EXPORT_PNG_SIZE}" ]
then
if ! [[ "${EXPORT_PNG_SIZE}" =~ ^[1-9][0-9]{0,3}$ ]]
then
log_err "Invalid size: ${EXPORT_PNG_SIZE}"
log_err "Size must be an integer value between 1 and 9999"
exit 4
fi
EXPORT_PNG_SIZES=("${EXPORT_PNG_SIZE}")
elif [ -n "${EXPORT_PNG_SIZES_RANGE}" ]
then
if ! seq ${EXPORT_PNG_SIZES_RANGE//,/ } 1>/dev/null 2>&1
then
log_err "Wrong range: ${EXPORT_PNG_SIZES_RANGE}."
log_err "Size range must follow the pattern '[first,[incr,]]last' similar to the command 'seq'."
exit 4
fi
EXPORT_PNG_SIZES=($(seq ${EXPORT_PNG_SIZES_RANGE//,/ } 2>/dev/null | sort -urn))
if [ "${#EXPORT_PNG_SIZES[@]}" -gt 100 ]
then
log_err "Better not to generate more than 100 PNGs."
log_err "Try with a range smaller than ${EXPORT_PNG_SIZES_RANGE}."
exit 4
fi
for size in ${EXPORT_PNG_SIZES[@]}
do
if [ "${size}" -le 0 ]
then
log_err "Sizes must be greater than zero."
log_err "Try with a range different from '${EXPORT_PNG_SIZES_RANGE}'"
exit 4
fi
done
if [ "${#EXPORT_PNG_SIZES[@]}" -gt 10 ]
then
echo "Would generate ${#EXPORT_PNG_SIZES[@]} PNG files in following sizes: ${EXPORT_PNG_SIZES[@]}"
read -p "Are you sure you want to generate ${#EXPORT_PNG_SIZES[@]} PNG files? [y/N] " answer
[ "${answer::1}" != "y" ] && exit 0
fi
fi
EXPORT_SMALLEST_PNG_SIZE="${EXPORT_PNG_SIZES[${#EXPORT_PNG_SIZES[@]} - 1]}"
EXPORT_PNG_BACKGROUNDS="transparent white sqred grey black"
for color in ${EXPORT_PNG_BACKGROUNDS}
do
$FORCE_GENERATE_ALL && break;
$GENERATE_ONLY_TRANSPARENT_PNG && EXPORT_PNG_BACKGROUNDS="transparent" && break
[ "${color}" == "transparent" ] && continue
[[ "${SOURCE_FILE_BASENAME}" == *"${color}"* ]] || continue
EXPORT_PNG_BACKGROUNDS="${EXPORT_PNG_BACKGROUNDS//${color}}"
done
transparent="rgb(255,255,255)"
white="rgb(255,255,255)"
sqred="rgb(170,0,0)"
grey="rgb(127,127,127)"
black="rgb(0,0,0)"
transparent_opacity="0"
white_opacity="255"
sqred_opacity="255"
grey_opacity="255"
black_opacity="255"
mkdir -p "${TARGET_DIR}"
mkdir -p "${TARGET_DIR_PNG}"
for color in ${EXPORT_PNG_BACKGROUNDS}
do
mkdir -p "${TARGET_DIR_PNG}/${color}"
done
for format in ${EXPORT_VECTOR_FORMATS}
do
$GENERATE_ONLY_TRANSPARENT_PNG && break
echo -n "Generating ${SOURCE_FILE_BASENAME}.${format}... "
inkscape_output="$(\
$INKSCAPE_CMD \
--file "${SOURCE_DIR}/${SOURCE_FILE_BASENAME}.svg" \
--without-gui \
--export-text-to-path \
--export-area-${EXPORT_AREA} \
--export-${format} "${TARGET_DIR}/${SOURCE_FILE_BASENAME}.${format}" 2>&1)"
if [ -e "$(find "${TARGET_DIR}" -type f -iname "${SOURCE_FILE_BASENAME}.${format}" -mtime -2s )" ]
then
echo "OK"
else
log_err "FAILED!"
log_err "${inkscape_output}"
log_err
fi
done
for width in ${EXPORT_PNG_SIZES[@]}
do
! $FORCE_GENERATE_ALL && [ -n "${size_ratio}" ] && [ "$(( ${width} / ${size_ratio} ))" -lt "${EXPORT_SMALLEST_PNG_SIZE}" ] && continue
for color in ${EXPORT_PNG_BACKGROUNDS}
do
target_filename="${SOURCE_FILE_BASENAME//transparent/${color}}_${width}px.png"
color_opacity="${color}_opacity"
echo -n "Generating ${TARGET_PNG_SUBDIR}/${color}/${target_filename}... "
inkscape_output="$(\
$INKSCAPE_CMD \
--file "${SOURCE_DIR}/${SOURCE_FILE_BASENAME}.svg" \
--without-gui \
--export-background "${!color}"\
--export-background-opacity "${!color_opacity}"\
--export-area-${EXPORT_AREA} \
--export-width "${width}" \
--export-png "${TARGET_DIR_PNG}/${color}/${target_filename}" 2>&1)"
if [ -e "$(find "${TARGET_DIR_PNG}/${color}" -type f -iname ${target_filename} -mtime -2s )" ]
then
echo "OK"
if [ -z "${size_ratio}" ]
then
size_ratio="$(file "${TARGET_DIR_PNG}/${color}/${target_filename}")"
size_ratio="${size_ratio##*PNG image data, }"
size_ratio="${size_ratio%%,*}"
size_ratio="${size_ratio//x//}"
size_ratio="$(( ${size_ratio} - 1 ))"
size_ratio="$(( ${size_ratio} > 0 ? ${size_ratio} : 1 ))"
fi
else
log_err "FAILED!"
log_err "${inkscape_output}"
log_err
fi
done
done
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment