Skip to content

Instantly share code, notes, and snippets.

@dtony
Created September 24, 2024 14:19
Show Gist options
  • Save dtony/c884c5249e4dff40bdc26646304661e9 to your computer and use it in GitHub Desktop.
Save dtony/c884c5249e4dff40bdc26646304661e9 to your computer and use it in GitHub Desktop.
Convert markdown with mermaid and math formulas to pdf
#!/bin/bash
input=$1
set -e
if [ "$#" -ne 1 ]; then
echo "One argument required"
exit 1
fi
if [ ! -f $1 ]; then
echo "$1 not found!"
exit 1
fi
math_md=`basename $1 .md`_math.md
mmd_md=`basename $1 .md`_mmd.md
pdf=`basename $1 .md`.pdf
# render mermaid
npx -p @mermaid-js/mermaid-cli mmdc -p puppeteerConfigFile.json -s 4 -i $1 --outputFormat=png -o $mmd_md
# render math formulas
python -m readme2tex --output $math_md $mmd_md --username tony --project test --nocdn
# replace git cdn urls by local ones
sed -i $math_md -e "s/https:\/\/.*svgs\//svgs\//"
# render pdf with pandoc
pandoc -V margin-top=2cm -V margin-left=2cm -V margin-right=2cm -V margin-bottom=2cm -V papersize=A4 -t html $math_md -o $pdf --pdf-engine-opt=--enable-local-file-access
# cleanup
rm svgs/*.svg
rmdir svgs
rm $math_md
rm $mmd_md
rm `basename $1 .md`_mmd*.png
{
"executablePath": "/snap/bin/chromium"
}
@dtony
Copy link
Author

dtony commented Sep 24, 2024

Requirements:

  • npm (npx command)
  • readme2text (pip install readme2tex)
  • pandoc (apt install pandoc or whatever)

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