使用pandoc将md渲染为pdf

一大段 markdown 与 latex的介绍, 太长了我就不写了

可以去查wikipedia

pandoc

pandoc就能互相转换, 介绍略

安装软件

ubuntu:

1
2
3
sudo apt install texlive-full

sudo apt install pandoc

或者去pandoc github仓库下载最新版

https://github.com/jgm/pandoc/releases/

1
2
xetex -v
pandoc -v

安装字体

我用的是wsl, 把一些windows的字体复制过来, 当然可以用一些开源的中文字体 但是我懒得整了 以后有机会可以看看(

1
2
3
4
5
mkdir -p ~/.local/share/fonts
cd /mnt/c/Windows/Fonts
cp sim* ~/.local/share/fonts
fc-cache -fv
fc-list | grep sim

看到:

1
2
3
4
5
6
/home/qwq/.local/share/fonts/simhei.ttf: SimHei,黑体:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/home/qwq/.local/share/fonts/simsunb.ttf: SimSun\-ExtB:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/home/qwq/.local/share/fonts/simfang.ttf: FangSong,仿宋:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta
/home/qwq/.local/share/fonts/simsun.ttc: SimSun,宋体:style=Regular,常规
/home/qwq/.local/share/fonts/simsun.ttc: NSimSun,新宋体:style=Regular,常规
/home/qwq/.local/share/fonts/simkai.ttf: KaiTi,楷体:style=Regular,Normal,obyčejné,Standard,Κανονικά,Normaali,Normál,Normale,Standaard,Normalny,Обычный,Normálne,Navadno,Arrunta

字体安装完了

转换

1
pandoc report.md --pdf-engine=xelatex -o 1.pdf -V CJKmainfont="SimSun"

可以用一些模板 以后再说()

Pandoc 支持 YAML 格式的 header,通过 header 可以指定文章的标题,作者,更新时间等信息,一个示例 header 如下:

1
2
3
4
5
---
title: "My title"
author: "author"
date: 2021-12-12
---

加入mermaid流程图 支持

1
2
npm install --global mermaid-filter
pandoc xxx.md -o xxx.pdf -F mermaid-filter

shell函数 md2pdf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function md2pdf() {
filename=$(basename "$1" .md)
pdf_file="${filename}.pdf"
if [ -e "$pdf_file" ]; then
printf "Warning: %s already exists. Do you want to overwrite it? (y/n) " "$pdf_file"
read -r reply
case "$reply" in
[Yy]) ;;
*)
echo "Conversion canceled."
return 1
;;
esac
fi
# --standalone -o output.tex
pandoc "$1" -o $pdf_file \
--pdf-engine=xelatex \
-V CJKmainfont="SimSun" \
-V geometry:"top=2cm, bottom=1.5cm, left=2cm, right=2cm" \
-N -V colorlinks \
--highlight-style tango \
-F mermaid-filter

}

更多可以参考:

Pandoc+TeXLive实现Markdown转PDF

另一个在typora里导出latex pdf的方案(css)

https://github.com/ouyen/typora-latex-theme-pku

-------------end-------------