Skip to content

Instantly share code, notes, and snippets.

@lucmann
Created December 8, 2021 09:44
Show Gist options
  • Save lucmann/5e592d3f09521b50e1b371a8022b8aaf to your computer and use it in GitHub Desktop.
Save lucmann/5e592d3f09521b50e1b371a8022b8aaf to your computer and use it in GitHub Desktop.
convert markdown to pdf with pandoc
#!/usr/bin/env bash
PREFERRED_FONT="AR PL KaitiM GB"
CJK_MAIN_FONT=
zh_fonts_file=$(mktemp -t fonts.XXX)
# save fonts family into a temp file to avoid the trouble with spaces among font family string
fc-list :lang=zh --format "%{family[0]}\n" > $zh_fonts_file
if [[ ! -s $zh_fonts_file ]]; then
echo "No available zh-cn font found"
exit 1
fi
while read ft
do
CJK_MAIN_FONT="$ft"
if [[ "$ft" = "$PREFERRED_FONT" ]]; then
break
fi
done < $zh_fonts_file
if command -v pandoc > /dev/null 2>&1; then
pandoc --pdf-engine=xelatex \
-V CJKmainfont="$CJK_MAIN_FONT" \
-V colorlinks=true \
-V linkcolor=blue \
--toc \
-N \
$1 \
-o ${1/%md/pdf}
else
echo "pandoc not found"
exit 2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment