Skip to content

Instantly share code, notes, and snippets.

@aboucaud
Created March 18, 2021 21:37
Show Gist options
  • Select an option

  • Save aboucaud/ff28bed1d061bbe33530998052efb975 to your computer and use it in GitHub Desktop.

Select an option

Save aboucaud/ff28bed1d061bbe33530998052efb975 to your computer and use it in GitHub Desktop.
[Convert HTML slides to PDF with decktape]
#!/usr/bin/env bash
#: Name : slides2pdf
#: Date : 2020-10-05
#: Author : Alexandre Boucaud
#: Version : 1.0.0
#: Description : Convert HTML slides to PDF with decktape
#: Options : See usage() function.
## Initialize defaults
WEBSITE="https://github.com/astefanutti/decktape"
LOCALHOST_NAME="http://host.docker.internal"
CONTAINER="astefanutti/decktape"
DOCKER_OPTIONS="--rm -t"
LOCALHOST_OPTION="--net=host"
MAC_FONTS_MOUNT="-v ${HOME}/Library/Fonts:/home/node/.local/share/fonts"
SLIDES_MOUNT="-v `pwd`:/slides"
DRY_RUN=0
UPDATE=0
LOCALHOST=0
OUTPUTFILE="slides.pdf"
set -e
# set -exo pipefail
function usage() {
echo "usage: ${0} [-h] [-n DRYRUN?] [-l LOCALHOST?] [-u UPDATE?] [-o output_pdf_name] <slides_url>"
}
## For each provided option arg
while getopts ":hnluo:" opt
do
case "$opt" in
h) usage && exit 0 ;;
n) DRY_RUN=1 ;;
l) LOCALHOST=1 ;;
u) UPDATE=1 ;;
o) OUTPUTFILE="$OPTARG" ;;
\?) echo "Invalid option $OPTARG" && exit 1 ;;
esac
done
## Remove options from args
shift "$(( $OPTIND - 1 ))"
## Unless source_file arg(s) found, print usage and exit (0 to avoid breaking pipes)
if [[ ! -n "$1" ]]; then
usage
exit 1
fi
slides_url=$1
if [ $LOCALHOST -eq 1 ]; then
# Replace localhost name with docker pattern
slides_url=${slides_url/"http://localhost"/$LOCALHOST_NAME}
slides_url=${slides_url/"http://127.0.0.1"/$LOCALHOST_NAME}
DOCKER_OPTIONS="${DOCKER_OPTIONS} ${LOCALHOST_OPTION} ${MAC_FONTS_MOUNT}"
fi
DOCKER_OPTIONS="${DOCKER_OPTIONS} ${SLIDES_MOUNT}"
function main() {
if [ $UPDATE -eq 1 ]; then
docker pull ${CONTAINER}
fi
docker run ${DOCKER_OPTIONS} ${CONTAINER} ${slides_url} ${OUTPUTFILE}
}
function dry_run() {
echo "--- Begin dry run mode."
echo ""
echo "This will run the following commands :"
if [ $UPDATE -eq 1 ]; then
echo " docker pull ${CONTAINER}"
fi
echo " docker run ${DOCKER_OPTIONS} ${CONTAINER} ${slides_url} ${OUTPUTFILE}"
echo ""
echo "--- End dry run mode."
}
function test_docker_installed() {
command -v "docker" > /dev/null || (echo "Please install Docker to use slide2pdf => https://www.docker.com/get-started" && exit 1)
}
function test_docker_running() {
$(command -v "docker") ps > /dev/null || (echo "Please start the Docker deamon to use slide2pdf." && exit 1)
}
function run_main() {
if [ $DRY_RUN -eq 1 ]; then
dry_run
else
test_docker_installed
test_docker_running
main
fi
echo ""
echo "Powered by decktape : ${WEBSITE}"
}
run_main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment