Created
September 6, 2024 13:15
-
-
Save okurka12/240680fa56769d422dda2b425f95f925 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# originally from ima2-sbirka repo | |
# | |
# brief: | |
# ------ | |
# | |
# A script to convert a single svg file to pdf | |
# Usage: ./svg2pdf SVGFILE | |
# | |
# checks that SVGFILE ends with .svg and that it exists | |
# | |
# if equally named .pdf file already exists, asks before overwriting | |
# | |
# deps: | |
# ----- | |
# | |
# sudo apt install librsvg2-bin | |
# | |
# version: 2.54.7+dfsg-1~deb12u1 (debian bookworm) | |
# known to NOT RASTERIZE | |
# check with opening the PDF in LibreOffice draw | |
# (see the shapes in the navigator panel) | |
# https://superuser.com/questions/381125/how-do-i-convert-an-svg-to-a-pdf-on-linux | |
# | |
USAGE="USAGE: $0 SVGFILE" | |
RSVG=rsvg-convert | |
# ask if something is ok, exit if it's not | |
# if OPTION_Y variable is "-y", do nothing | |
prompt_confirm () { | |
if [ "$OPTION_Y" != "-y" ]; then | |
printf "%s (y/n) " "$*" | |
read -r PROCEED | |
if [ "$PROCEED" != "y" ]; then | |
echo "abort" | |
exit | |
fi | |
fi | |
} | |
# if $1 exists, ask if its ok to delete and then delete | |
safe_delete () { | |
if [ -f "$1" ]; then | |
prompt_confirm "delete $1?" | |
echo "deleting $1" | |
rm "$1" | |
fi | |
} | |
# if $1 exists, ask if its ok to overwrite | |
ask_overwrite () { | |
if [ -f "$1" ]; then | |
prompt_confirm "overwrite $1?" | |
fi | |
} | |
################################################################################ | |
if [ "$1" = "" ]; then | |
echo "$USAGE" | |
exit | |
fi | |
# check if rsvg package is present | |
$RSVG --help > /dev/null 2> /dev/null | |
if [ $? != 0 ]; then | |
echo "err: rsvg package ($RSVG) seems to be missing" >&2 | |
exit 1 | |
fi | |
# check that the filename ends with .svg | |
CHECK_COUNT=$(echo $1 | grep -E -c ".*\.svg$") | |
if [ "$CHECK_COUNT" != "1" ]; then | |
echo "err: '$1' is not a .svg file" | |
exit | |
fi | |
# check that the file exists | |
if [ ! -f "$1" ]; then | |
echo "err: file '$1' does not exist" | |
fi | |
NEW_FILENAME=$(echo "$1" | sed "s|.svg|.pdf|g") | |
echo "converting $1 -> $NEW_FILENAME" | |
ask_overwrite $NEW_FILENAME | |
$RSVG -f pdf -o $NEW_FILENAME $1 | |
# -f pdf - output format | |
# -o filename - output filename |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment