mirror of
https://codeberg.org/privacy1st/md-to-pdf.git
synced 2024-11-25 23:05:01 +01:00
97 lines
2.9 KiB
Bash
Executable File
97 lines
2.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
SCRIPT_TITLE=md-to-pdf
|
|
depends=(wkhtmltopdf cmark-gfm) # 'wkhtmltopdf' with qt-error is e.g. provided by the package 'wkhtmltopdf-static'
|
|
optdepends=(notify-send de-p1st-image-width-limit)
|
|
|
|
# Uncomment for debugging:
|
|
exec 1> "${HOME}"/"${SCRIPT_TITLE}".log 2>&1
|
|
set -x
|
|
|
|
#######
|
|
|
|
|
|
function command_exists(){
|
|
command -v "$1" > /dev/null
|
|
}
|
|
|
|
function notify() {
|
|
# $1: Message summary
|
|
# $2: Message body
|
|
# $3-n: notify-send options
|
|
|
|
if command_exists notify-send; then
|
|
notify-send "$@";
|
|
else
|
|
# Write message to stderr
|
|
printf '%s: %s\n' "$1" "$2" 1>&2;
|
|
fi
|
|
}
|
|
|
|
function check_depends() {
|
|
for dep in "${depends[@]}"; do
|
|
if ! command_exists "${dep}"; then
|
|
notify "${SCRIPT_TITLE}" "The dependency ${dep} is missing!" --icon=dialog-information;
|
|
exit 1;
|
|
fi
|
|
done
|
|
|
|
for dep in "${optdepends[@]}"; do
|
|
if ! command_exists "${dep}"; then
|
|
# Inform about missing opt. dependency on /dev/stderr
|
|
printf 'Info: The optional dependency %s is missing.' "${dep}" 1>&2;
|
|
fi
|
|
done
|
|
}
|
|
|
|
function modify_html() {
|
|
# Images should be no wider than 700px, otherwise they may be only partially visible.
|
|
# This method adds a style attribute to HTML image tags to limit their size.
|
|
#
|
|
# in: path to HTML file
|
|
# out: None. The given HTML file will be modified.
|
|
file="${1}"
|
|
|
|
if command_exists 'de-p1st-image-width-limit'; then
|
|
de-p1st-image-width-limit "${file}" || notify "${SCRIPT_TITLE}" "Could not modify ${html}!" --icon=dialog-information;
|
|
fi
|
|
}
|
|
|
|
check_depends
|
|
|
|
# read newline-delimited absolute paths of selected files (on local filesystem)
|
|
echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | while read file; do
|
|
# If $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS contains a tailing newline,
|
|
# $file may be the empty string
|
|
if [ "${file}" = '' ]; then
|
|
continue;
|
|
fi
|
|
|
|
html="${file}".html;
|
|
pdf="${file}".pdf;
|
|
parent_dir="$(dirname "${file}")"
|
|
|
|
# Convert
|
|
cmark-gfm "${file}" --to html --unsafe > "${html}" || notify "${SCRIPT_TITLE}" "Conversion of ${file} to html failed!" --icon=dialog-information;
|
|
chromium --headless --disable-gpu --print-to-pdf="${pdf}" file://"${html}" || notify "${SCRIPT_TITLE}" "Conversion of ${html} to pdf failed!" --icon=dialog-information;
|
|
|
|
modify_html "${html}"
|
|
|
|
# https://wkhtmltopdf.org/usage/wkhtmltopdf.txt
|
|
wkhtmltopdf \
|
|
--page-size A4 \
|
|
--allow "${parent_dir}" `# We assume that all referenced images are located in the same directory` \
|
|
`# or in a subdirectory of the html file` \
|
|
--encoding UTF-8 \
|
|
--outline \
|
|
--orientation Portrait \
|
|
--footer-center '[Page]' `# Adds page numbers` \
|
|
'--disable-smart-shrinking' \
|
|
"${html}" "${pdf}" || notify "${SCRIPT_TITLE}" "Conversion of ${html} to pdf failed!" --icon=dialog-information;
|
|
|
|
# Cleanup
|
|
rm "${html}" || notify "${SCRIPT_TITLE}" "Could not delete intermediate file ${html}!" --icon=dialog-information;
|
|
done
|
|
|
|
notify "${SCRIPT_TITLE}" 'Done!' --icon=dialog-information;
|