Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have recently started learning Markdown for use with documentation, and need to print out a few of my Markdown pages. I would like to use a command-line, Terminal, etc. utility that allows me to convert Github-flavored Markdown to PDF. It needs to have proper syntax highlighting and should not look horrible. Thanks for any help.

share|improve this question
    
The syntax coloring on GitHub is not part of GitHub Flavored Markdown. At least as far as I know. – Oliver Salzburg Dec 16 '13 at 12:51
    
@OliverSalzburg Github uses Linguist to provide syntax highlighting. – DanteTheEgregore Dec 16 '13 at 13:37
    
@dillmo, Convert to HTML first, then use Chrome to print-to-pdf. – Pacerier Feb 18 at 20:30
up vote 37 down vote accepted

I've had success using grip to display markdown in Chrome and then use Chrome's "Save as PDF" option in the Print dialog.

pip install grip  
grip your_markdown.md

grip will render the markdown on localhost:5000 - just edit away and refresh the browser. Print when ready.

This gave a more reliable representation than pandoc and was lighter weight than installing latex (required by pandoc for pdf generation).

The print is not command line in this answer, but still found this easier/more reliable (looked 100% like Github for a long document including relatively linked images and code highlighting).

share|improve this answer
1  
this is easy enough. – s2t2 Sep 5 '15 at 0:21
4  
grip your_markdown.md --export your_markdown.html is useful option here. Exports it to the html file, which can then be printed from the command line using something like wkhtmltopdf. – Luke Exton Sep 12 '15 at 1:27
    
In Ubuntu, I wasn't able to have cross-linking work in the saved PDF when printing to PDF from either Firefox or Chromium and I had to download the pre-compiled wkhtmltopdf rather than use the version included in the Ubuntu repos. – raphael Sep 29 '15 at 13:36
    
Unfortunately grip needs the access to GitHub. It does not work offline – nowox Oct 30 '15 at 14:06
    
Alternatively you can download (free!) Atom (atom.io), open your file in Atom, use control + shift+ M to view it in preview, save as html, then open the html in your Chrome browser and save as pdf. – Andrew Carter Jan 4 at 19:54

Take a look at pandoc. It does have syntax highlighting. It might require you making (minor) changes to your document since it has its own flavour of markdown and I don't know how closely it matches the GitHub flavour.

share|improve this answer
1  
Thanks. Running pandoc -h did return support for GitHub Flavored Markdown, so I'm marking this question as resolved. – dillmo Feb 11 '14 at 23:49
    
I attempted to install pandoc on Fedora Linux and ran into a dependency nightmare - mainly LaTex related. My advice would be to skip pandoc and try other options first – IanB Aug 13 '15 at 1:13

You can also use Node.js based markdown-pdf

npm install -g markdown-pdf
markdown-pdf /path/to/markdown
share|improve this answer
    
This is pretty awesome and really easy. The link issue comes from the html5 boilerplate's print css which you can just comment out or override in your own stylesheet. – Robert Went Nov 8 '15 at 3:14

If the markdown file was hosted on github repository, gitprint is an interesting option to create pdf / print.

All you need to do is to replace github.com by gitprint.com in the URL. Here is an example from gitprint's homepage.

Unfortunately, it does not work on markdown gists, and works only with markdown files at the repository.

share|improve this answer
2  
It does not work with images either. :( – Adam Arold Sep 2 '14 at 13:12
    
...and is poorly formated :( – Moebius Apr 22 at 19:20

As I stated in my comment, Github uses Linguist to provide syntax highlighting. On Github, you can use this to specify syntax highlighting like so:

```ruby
require 'redcarpet'
markdown = Redcarpet.new("Hello World!")
puts markdown.to_html
```

Unfortunately, there's no good way to convert Markdown directly to a PDF file with syntax highlighting.

Alternatives:

Vim:

If you have vim, you can easily achieve syntax highlighting by running the following from a terminal:

vim -c hardcopy -c quit /path/to/file.ps

Or inside of vim:

:hardcopy >/path/to/file.ps

This will produce a PostScript file that can be converted to pdf using, for example, ps2pdf:

ps2pdf /path/to/file.ps

Source-highlight:

If you'd like instead to go the route of HTML or LaTeX, you could try Source-highlight instead. A list of all languages supported by Source-highlight can be found here.

A few example Source-highlight commands include:

source-highlight -s java -f html -i Hello.java -o Hello1.html
source-highlight -s java -f html --input Hello.java --output Hello2.html --doc
source-highlight -s java -f html -i Hello.java -o Hello3.html --title "Happy Java with java2html :-)" --tab 3

Using this input file

And each outputting their own respective HTML file:

Hello1.html
Hello2.html
Hello3.html

Further examples of Source-highlight usage can be found here

Windows:

Vim, ps2pdf (provided by Ghostscript) and Source-highlight are all available via Cygwin.

share|improve this answer

There's an online converter available at http://www.markdowntopdf.com
This provides syntax highlighting out of the box and is the simplest solution I've seen so far. It also correctly handles other features specific to GFM e.g. tables.

share|improve this answer

I have recently created a service to convert markdown documents to PDF. It supports Github flavoured markdown as well as syntax highlighting. The service is located at: http://markdown2pdf.com

share|improve this answer
    
doesn't work with zip file : couldn't find the markdown file in the archive while there is one. – Moebius Apr 22 at 19:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.