Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Markdown + CSS -> PDF?

Tags:

I'm trying to convert a Markdown file into a PDF. I'm looking for only two things:

  • A way to easily change the style of the pdf (for example with a CSS file)
  • A syntax highlighter for code blocks

What tools can I use for that? I tried Pandoc, but it uses Latex for the formatting which is not easy to use.

like image 837
pimpampoum Avatar asked May 23 '14 09:05

pimpampoum


People also ask

How do I turn a Markdown into a PDF?

Usage. Just focus the window containing your markdown file and use the convert command ( Packages > Markdown PDF > Convert ). The output PDF will be styled similar to the markdown on github.com , as well as any user styles you have added.

Can you style Markdown with CSS?

Markdown does not support CSS styles. Since Markdown converts to HTML, Any valid HTML works. CSS styles are added with inline or style tags in HTML.


2 Answers

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 (see also print-css.rocks). Btw, pandoc can also use wkhtmltopdf now:

pandoc -t html --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.

like image 111
mb21 Avatar answered Oct 06 '22 01:10

mb21


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

like image 33
Pawel Wiejacha Avatar answered Oct 06 '22 00:10

Pawel Wiejacha