Skip to content

Instantly share code, notes, and snippets.

@kmpm
Last active March 23, 2023 00:21
Show Gist options
  • Save kmpm/c254558fcb0608346f49946a53cd8c09 to your computer and use it in GitHub Desktop.
Save kmpm/c254558fcb0608346f49946a53cd8c09 to your computer and use it in GitHub Desktop.
Small file that installes ad uses weasyprint instead of wkhtmltopdf on linuxserver bookstack containers..
#!/bin/bash
# SPDX-FileCopyrightText: 2022 Peter Magnusson <me@kmpm.se>
#
# SPDX-License-Identifier: CC0-1.0
#
# This file installs weasyprint and creates a script that
# tries to proxy calls from bookstack to what it believe is
# wkhtmltopdf.
#
# put this file in /custom-cont-init.d and restart the container
#
# add the following to /config/www/.env
# WKHTMLTOPDF=/usr/local/bin/weasyproxy
# ALLOW_UNTRUSTED_SERVER_FETCHING=true
#
# install weasyprint
apk add --update --no-cache weasyprint
# create the weasyproxy script
cat << EOF > /usr/local/bin/weasyproxy
#!/bin/bash
# this tries to work as a proxy between wkhtmltopdf and weasyprint
# for bookstack to use on for example the linuxserver alpine image
set -o errexit -o pipefail -o noclobber -o nounset
LOGFILE=/config/log/weasyproxy.log
echo "--- weasyproxy \$(date) ---" | tee -a \$LOGFILE
echo "inbound arguments: \$@" | tee -a \$LOGFILE
# -allow a command to fail with !’s side effect on errexit
# -use return value from \${PIPESTATUS[0]}, because ! hosed \$?
! getopt --test > /dev/null
if [[ \${PIPESTATUS[0]} -ne 4 ]]; then
echo 'I’m sorry, `getopt --test` failed in this environment.'
exit 1
fi
LONGOPTS=lowquality,page-size:,outline,print-media-type
OPTIONS=ls:
! PARSED=\$(getopt --options=\$OPTIONS --longoptions=\$LONGOPTS --name "\$0" -- "\$@")
if [[ \${PIPESTATUS[0]} -ne 0 ]]; then
# e.g. return value is 1
# then getopt has complained about wrong arguments to stdout
exit 2
fi
eval set -- "\$PARSED"
PAGE_SIZE=Letter
OUTLINE=n
MEDIA=screen
ARGS="-p" # add -v for verbose and -d for debug output
while true; do
case "\$1" in
-l|--lowquality)
ARGS="\$ARGS --optimize-images"
shift
;;
-p|--page-size)
PAGE_SIZE=\$2
shift 2
;;
--outline)
OUTLINE=y
shift
;;
--print-media-type)
MEDIA=print
shift
;;
--)
shift
break
;;
*)
echo "unsupported argument '\$1'" | tee -a \$LOGFILE
exit 3
;;
esac
done
ARGS="\$ARGS -s /config/weasyproxy/\${PAGE_SIZE}.css --media-type \${MEDIA}"
echo "PAGE_SIZE = \${PAGE_SIZE}" | tee -a \$LOGFILE
echo "OUTLINE = \${OUTLINE}" | tee -a \$LOGFILE
echo "MEDIA = \${MEDIA}" | tee -a \$LOGFILE
echo "ARGS = \${ARGS}" | tee -a \$LOGFILE
if [[ \$# -ne 2 ]]; then
echo "\$0: input and output arguments is required" | tee -a \$LOGFILE
exit 4
fi
weasyprint \${ARGS} \$1 \$2 >>\$LOGFILE 2>&1
EOF
# create css files for page sizes, if missing
if [ ! -d /config/weasyproxy ]; then
mkdir /config/weasyproxy
fi
if [ ! -f /config/weasyproxy/A4.css ]; then
cat << EOF > /config/weasyproxy/A4.css
@page {
size: A4;
}
EOF
fi
if [ ! -f /config/weasyproxy/Letter.css ]; then
cat << EOF > /config/weasyproxy/Letter.css
@page {
size: Letter;
}
EOF
fi
chmod +x /usr/local/bin/weasyproxy
@bonny1992
Copy link

Hey @kmpm thank you for this!
I found you on Linuxserver's issues (linuxserver/docker-bookstack#134) and it works wonderfully.
Only one note tho, I had to change
ARGS="\$ARGS --optimize-images"
from line 59 to
ARGS="\$ARGS --optimize-size images"

I suppose this changed overtime.
Many thanks of course, again!

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