How to convert Markdown into PDF file

The working environment is:

  • OS: Mac OS X EI Capitan
  • Sublime Text 3

Take use of Sublime Text 3 (ST3) Package Unicode Character Highlighter to check whether the file contains unicode characters, such as Hanzi.


Read more about ST3:


If .md doesn’t contains unicode characters

You can directly use ST3 Package Pandoc to convert .md file into .pdf:

  • Command+Shift+P
  • Enter Pandoc
  • Choose PDF

Bingo!

If .md contains unicode characters

If the markdown file contains unicode characters, you can use the command as follow to convert the .md into .pdf1:

1
$pandoc -N -s --smart --latex-engine=xelatex -V CJKmainfont='Hiragino Sans GB' -V mainfont='Monaco' -V geometry:margin=1in XXX.md  -o XXX.pdf

where XXX.md is the name of your markdown file and XXX.pdf is the name of the PDF file.

Follow the Pandoc User’s Guide, you can change the options of pandoc.

pandoc options

  • -N, --number-sections
    • Number section headings in LaTeX, ConTeXt, HTML, or EPUB output. By default, sections are not numbered. Sections with class unnumbered will never be numbered, even if --number-sections is specified.
  • -s, --standalone
    • Produce output with an appropriate header and footer (e.g. a standalone HTML, LaTeX, TEI, or RTF file, not a fragment). This option is set automatically for pdf, epub, epub3, fb2, docx, and odt output.
    • --latex-engine=xelatex means that we use XeTeX as the TeX typesetting engine.
  • -V KEY[=VAL], --variable=KEY[:VAL]
    • Set the template variable KEY to the value VAL when rendering the document in standalone mode. This is generally only useful when the --template option is used to specify a custom template, since pandoc automatically sets the variables used in the default templates. If no VAL is specified, the key will be given the value true.
    • CJKmainfont: Choose a font from List of CJK fonts and make sure the font is available on your OS.
      • PingFang SC & PingFang TC & PingFang HK: 苹方-简 & 蘋方-繁 & 蘋方-港
      • WenQuanYi or Zen Hei: 文泉驿正黑
      • Heiti SC & Heiti TC: 黑体-简 & 黑體-繁
      • Hiragino Sans GB: 冬青黑体简体中文

Variables for LaTeX

  • mainfont, sansfont, monofont, mathfont, CJKmainfont
    • font families for use with xelatex or lualatex: take the name of any system font, using the fontspec package. Note that if CJKmainfont is used, the xecjk package must be available.
      • mainfont: 衬线字体
      • sansfont: 无衬线字体
      • monofont: 等宽字体
  • geometry
    • option for geometry package, e.g. margin=1in; may be repeated for multiple options

Comments

In fact, generally you can take use of the Print function of Google Chrome Explorer to easily fulfill the conversion. For example, my explorer is Google Chrome, Version 63.0.3239.84 (Official Build) (64-bit) and in ST3:

  • Command+Shift+P
  • Enter mp
  • Choose Markdown Preview: Preview in Browser
  • Choose github or markdown
  • In the pop-up Chrome page, you can check the corresponding html file
  • Press Command+P to print the web page

However, this method is not meticulous enough.

1. https://www.zhihu.com/question/20849824
-------------End of postThanks for your time-------------
BaoDuGe_飽蠹閣 wechat
Enjoy it? Subscribe to my blog by scanning my public wechat account