How to convert Markdown + CSS -> PDF?

≡放荡痞女 提交于 2019-11-30 06:26:59

Pandoc can convert your Markdown to HTML, but the styling/layout is a different topic. If you want to produce a PDF but use CSS for styling, you need something that can interpret CSS. That is either use a browser and print to PDF, pay for Prince or try wkhtmltopdf. Btw, pandoc can also use wkhtmltopdf now:

pandoc -t html5 --css mystyles.css input.md -o output.pdf

But I suspect if you want a beautifully-typeset PDF for free you'll have to learn LaTeX or ConTeXt which is a modern and more self-contained replacement for LaTeX, both can be used with pandoc. See creating a PDF with pandoc.

You can also give PanWriter a try: a markdown editor I built, where you can inject CSS and export the PDF from the paginated preview.

There is really nice and simple tool for browsing Markdown documents which additionally supports export to PDF features:

GFMS - Github Flavored Markdown Server

It's simple and lightweight (no configuration needed) HTTP server you can start in any directory containing markdown files to browse them.

Features:

  • full GFM Markdown support
  • source code syntax highlighting
  • browsing files and directories
  • nice looking output (and configurable CSS style sheets)
  • export to PDF (best-looking markdown-to-pdf output I've ever seen)

gfms -p 8888

wget "http://localhost:8888/file.md?pdf" -O file.pdf

To a certain extent, I'd suggest just learning the basic latex formatting you need - it removes a layer of interpretation by the renderer.

However, pandoc does support html input, so in theory, you could export markdown->html(with custom css), then call pandoc again to convert to html. I don't know if (or how much) of the formatting would be saved - css can be really complicated to parse.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!