use wkhtmltopdf instead of chromium

This commit is contained in:
Daniel Langbein 2021-09-15 17:14:44 +02:00
parent cd5fb9ebdb
commit 1e965f99a5
2 changed files with 46 additions and 2 deletions

View File

@ -1,5 +1,21 @@
#!/usr/bin/env bash #!/usr/bin/env bash
function convert0(){
file="${1}"
html="${file}".html;
pdf="${file}".pdf;
# depends=(chromium cmark-gfm) # 'chromium' is e.g. provided by the package 'ungoogled-chromium'
# Convert
cmark-gfm "${file}" --to html --unsafe > "${html}"
chromium --headless --disable-gpu --print-to-pdf="${pdf}" file://"${html}"
# Cleanup
rm "${html}"
}
function convert1(){ function convert1(){
md="${1}" md="${1}"
out="${md}".pdf out="${md}".pdf

View File

@ -1,8 +1,8 @@
#!/usr/bin/env bash #!/usr/bin/env bash
SCRIPT_TITLE=md-to-pdf SCRIPT_TITLE=md-to-pdf
depends=(chromium cmark-gfm) # 'chromium' is e.g. provided by the package 'ungoogled-chromium' depends=(wkhtmltopdf cmark-gfm) # 'wkhtmltopdf' with qt-error is e.g. provided by the package 'wkhtmltopdf-static'
optdepends=(notify-send) optdepends=(notify-send de-p1st-image-width-limit)
# Uncomment for debugging: # Uncomment for debugging:
exec 1> "${HOME}"/"${SCRIPT_TITLE}".log 2>&1 exec 1> "${HOME}"/"${SCRIPT_TITLE}".log 2>&1
@ -44,6 +44,19 @@ function check_depends() {
done 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 check_depends
# read newline-delimited absolute paths of selected files (on local filesystem) # read newline-delimited absolute paths of selected files (on local filesystem)
@ -56,11 +69,26 @@ echo "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | while read file; do
html="${file}".html; html="${file}".html;
pdf="${file}".pdf; pdf="${file}".pdf;
parent_dir="$(dirname "${file}")"
# Convert # Convert
cmark-gfm "${file}" --to html --unsafe > "${html}" || notify "${SCRIPT_TITLE}" "Conversion of ${file} to html failed!" --icon=dialog-information; 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; 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 # Cleanup
rm "${html}" || notify "${SCRIPT_TITLE}" "Could not delete intermediate file ${html}!" --icon=dialog-information; rm "${html}" || notify "${SCRIPT_TITLE}" "Could not delete intermediate file ${html}!" --icon=dialog-information;
done done