Pandoc安装实现Markdown转PDF (CentOS6)

时间:2022-04-27
本文章向大家介绍Pandoc安装实现Markdown转PDF (CentOS6),主要内容包括Pandoc简介、Pandoc安装、cabal安装、pandoc安装、texlive安装、Pandoc使用、总结、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

Pandoc简介

pandoc 是一种文档标记语言转换工具,可实现不同文档标记语言间的格式转换,由Haskell语言编写,以命令行的形式实现与用户的交互,可支持多种平台,windowslinuxmac等。据说在出版行业也在使用。

官方网址:https://pandoc.org/

Pandoc安装

由于Centos6的yum源中haskell相关的版本都比较老了,所有须要自行源码安装,须要安装的内容有:ghc, cabal, pandoc, texlive。

ghc是haskell的编译器,cabal是一种包管理器,可以很方便的自动安装各种包和依赖,pandoc就是使用cabal来安装的,TeX是一种文档排版系统,texlive是like unix下的一种TeX实现,对TeX的理解不深,所以先这么简单理解了,PDF的生成须要依赖它。

ghc安装

pandoc官方文档提到:

Note that pandoc requires GHC >= 7.8.

所以我下载安装7.8.2, 下载安装源码包

$ wget http://www.haskell.org/ghc/dist/7.8.2/ghc-7.8.2-x86_64-unknown-linux-centos65.tar.bz2
$ tar xf ghc-7.8.2-x86_64-unknown-linux-centos65.tar.bz2
$ cd ghc-7.8.2
$ ./configure
$ make install

cabal安装

$ wget http://www.haskell.org/cabal/release/cabal-install-1.20.0.3/cabal-install-1.20.0.3.tar.gz
$ tar xf cabal-install-1.20.0.3.tar.gz
$ cd cabal-install-1.20.0.3

由于国内环境访问haskell官方源网络比较慢,所以下面修改源的地址为南京大学的镜像站。

修改bootstrap.sh文件中的

HACKAGE_URL="https://hackage.haskell.org/package" 

HACKAGE_URL="http://mirrors.nju.edu.cn/hackage/package"

修改稿

URL=${HACKAGE_URL}/${PKG}-${VER}/${PKG}-${VER}.tar.gz

URL=${HACKAGE_URL}/${PKG}-${VER}.tar.gz

$ ./bootstrap.sh

待其安装成功,将cabal命令链接到/usr/bin下,这样PATH中就可以找到cabal

ln -s /root/.cabal/bin/cabal /usr/bin/cabal

执行cabal update,待出现如下提示后,执行Ctrl+C中断命令,还是由于haskell官方源网络比较慢,修改生成的配置文件中源的地址为南京大学的镜像站

$ cabal update
Config file path source is default config file.
Config file /root/.cabal/config not found.
Writing default configuration to /root/.cabal/config
Downloading the latest package list from hackage.haskell.org

修改~/.cabal/config remote-repo: hackage.haskell.org:http://hackage.haskell.org/packages/archive

remote-repo: mirrors.nju.edu.cn:http://mirrors.nju.edu.cn/hackage

再次执行cable update, 会提示执行cabal install cabal-install

$ cable update
Downloading the latest package list from mirrors.nju.edu.cn
Skipping download: Local and remote files match.
Note: there is a new version of cabal-install available.
To upgrade, run: cabal install cabal-install
$ cabal install cabal-install

待安装完成

pandoc安装

很简单,执行

$ cabal install pandoc --enable-tests

时间可能稍微有点久,耐心等待。

安装完成后,将/root/.cabal/bin加入到PATH环境变量中。这样pandoc命令就可以找到。

texlive安装

下载源码包,执行安装脚本

$ wget http://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz
$ tar zxvf install-tl-unx.tar.gz
$ cd install-tl-20170930
$ ./install-tl

时间比较久,耐心等待。

安装完成之后,其程序包目录为/usr/local/texlive/2017/bin/x86_64-linux,须要将其加入到PATH变量中, PATH=$PATH:/usr/local/texlive/2017/bin/x86_64-linux

Pandoc使用

安装好后就可以使用pandoc命令来执行转换操作了,使用pandoc将带有中文的markdown转为pdf

$ pandoc -N --toc --columns=10 --latex-engine=xelatex -V CJKmainfont=STSong -V geometry:margin=1in -o test.pdf test.md

参数说明:

-N        为章节进行数字编号

--toc    为文档添加目录

--columns    当不设置时表格的宽度可能有问题,会影响表格的宽度

--latex-engine=xelatex   设置latex引擎,要想正确的输出中文就得指定xelatex作为引擎

-V CJKmainfont=STSong  要想正确的输出中文还得指定一个合适的中文字体,这个字体在Centos上须要先安装好

-V geometry:margin=1in   指定上下左右的空白尺寸

-o 指定输出文件名

总结

pandoc非常的强大,可以对几乎所有的文档格式进行互转,比如markdown,docx,pdf,html,docbook,epub等等。另外pandoc的参数也很多,有待后续继续研究。

另外本文只介绍在CentOS操作系统上比较合适的安装方式,其他的比如Ubuntu上可能有更好的更方便的安装方式,请参考Pandoc官方文档http://pandoc.org/installing.html