Pandoc使用和配置


Pandoc介紹
將文件從一種標記格式轉換為另一種格式(比如 html 和 markdown等標記語言),它可以將文檔在 Markdown、LaTeX、reStructuredText、HTML、Word docx 等多種標記格式之間相互轉換,並支持輸出 PDF、EPUB、HTML 幻燈片等多種格式。pandoc支持轉換的格式非常多:

安裝

  • Pandoc 可以在Linux上進行安裝:
 scoop install pandoc
 # 安裝成功后 查看幫助(會發現一些有用的選項)
 pandoc -h
  • pandoc 可以從官網下載文件;

    首先從github上獲取最新版本,有5種版本可供選擇,為了不用安裝我下載了其pancoc-2.0.2-windows.zip版本
    解壓之后得到4個文件:.rtf與.txt——版權聲明文件,.html——用戶指南,pandoc.exe——命令行工具。主要使用pandoc.exe來進行文件轉換。

  • Git Windows 64位下載地址:
    百度網盤,密碼:b5t6

安裝后的配置
安裝之后,使用 pandoc --version 命令用於檢查pandoc是否可用,這時候如果直接在任意處打開cmd來執行,windows可能不能識別此命令,需要將pandoc.exe的路徑配置到path環境變量中去
我的路徑:

我的電腦——>鼠標右鍵:屬性——>高級系統設置——>高級:環境變量——>系統變量:path——>.......;D:\Typora—編輯軟件\Pandoc;

注:系統環境變量是相對所有用戶而言,所以配置一個就可以了。

使用 pandoc
安裝好上述兩個軟件,在要轉換文件格式的目錄下,右鍵選擇 git bash,運行下面命令,檢查pandoc軟件是否正常安裝:

 pandoc -h #或者
 pandoc --version

 pandoc.exe 2.10.1
 Compiled with pandoc-types 1.21, texmath 0.12.0.2, skylighting 0.8.5
 Default user data directory: C:\Users\huang\AppData\Roaming\pandoc
 Copyright (C) 2006-2020 John MacFarlane
 Web:  https://pandoc.org
 This is free software; see the source for copying conditions.
 There is no warranty, not even for merchantability or fitness
 for a particular purpose.

使用
可以顯式指定輸入和輸出的格式,使用 -f 輸入格式 -t 輸出格式
如果未明確指定輸入或輸出格式,pandoc將根據文件的擴展名進行猜測

1. md 轉 html
pandoc -s -f gfm -t html5 --css=css/markdownPad-github.css test.md -o test.html   
2.md 轉 docx文件     
pandoc test.md -o test.docx 

-s 表示使用標准模板輸出
-f gfm -t html5 表示用 gfm 引擎來解析,從 Github Flavored MarkDown 到 HTML5。從網上看的別人寫的是-f markdown_github -t html,試着使用這個來編譯會產生Warning,提示找不到markdown_github,用gfm替代,索性就直接在命令里使用gfm
--css=css/markdownPad-github.css 把 markdownPad-github.css 這個css文件加到生成的HTML文件中,是其呈現出不同的樣式
test.md -o test.html 把test.md這個MarkDown文件輸出成同的.html文件,這樣使用有時候會有Warning,應該是需要把.html取一個與.md文件不同的名字
本想使用github-markdown.css來呈現 github 式的 MarkDown 文件,但生成的html文件並沒有.markdown-body等 class 名字,應該是 html 模板的原因,后來改用markdownPad-github.css
3.md 轉 pdf
pandoc --pdf-engine=xelatex --template=D:\tools\Pandoc\pm-template.latex test.md -o test.pdf

生成pdf過程中的問題

  • 執行命令:
    pandoc --latex-engine=xelatex test.md -o test.pdf

  • 編譯出錯:
    latex-engine has been removed. Use --pdf-engine instead.
    替換為:pandoc --pdf-engine=xelatex test.md -o test.pdf

  • 編譯生成的pdf文件只有英文,中文不顯示,原因是沒有指定中文字體,在 cmd 中用 fc-list 查看所有安裝的字體:``
    fc-list :lang=zh 輸出所有中文字體,

  • cmd輸出的中文亂碼,執行chcp 65001打開 active code page 65001 可以看到正常的中文輸出

  • 注:用 -V mainfont="Microsoft YaHei" 指定中文字體,必須是雙引號,否則會報錯
    pandoc --pdf-engine=xelatex -V mainfont="Microsoft YaHei" test.md -o test.pdf

  • 中文正常顯示后,發現中文不換行。原因是 Pandoc 使用的 latex 模板文件需要修改
    看了幾篇文章,他們都改用了Tzeng Yuxio的模板文件,下載該模板,修改命令為:

pandoc --pdf-engine=xelatex --template=pm-template.latex test.md -o test.pdf
或者 指明絕對路徑:
pandoc --pdf-engine=xelatex --template=D:\tools\Pandoc\pm-template.latex test.md -o test.pdf

利用 Typora 進行轉換

比如中Typora中markdown與word的互轉: 在Typora中:Typora 👉 文件 👉 導入/導出 選擇 word格式即可。(markdown中的數學公式也可以轉換到word中。)

引用參考:簡書
引用參考:簡書


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM