How Can I Convert Github-Flavored Markdown To A PDF

159

65

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.

dillmo

Posted 2013-12-16T11:52:11.977

Reputation: 2 201

@dillmo, Convert to HTML first, then use Chrome to print-to-pdf.

– Pacerier – 2016-02-18T20:30:15.450

The syntax coloring on GitHub is not part of GitHub Flavored Markdown. At least as far as I know. – Der Hochstapler – 2013-12-16T12:51:48.513

@OliverSalzburg Github uses Linguist to provide syntax highlighting.

– DanteTheEgregore – 2013-12-16T13:37:52.253

Answers

161

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).

Josh Werts

Posted 2013-12-16T11:52:11.977

Reputation: 1 726

2this is easy enough. – s2t2 – 2015-09-05T00:21:41.823

19grip 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 – 2015-09-12T01:27:00.147

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 – 2015-09-29T13:36:29.643

1Unfortunately grip needs the access to GitHub. It does not work offline – nowox – 2015-10-30T14:06:48.683

7

Alternatively you can download (free!) Atom (https://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 – 2016-01-04T19:54:31.973

6This does come with the Github-like frame around it. Is there a way to print to PDF, removing the borders / title bar (i.e. printing everything inside the frame)? – Joost – 2016-03-07T14:05:13.000

@Joost This is how I removed the borders. It did the job, but is ugly and might not work in all cases.

– None – 2017-02-02T22:40:23.700

3

You can use Pandoc with the light-weight wkhtmltopdf instead of latex like this:

pandoc README-Template.md --from=gfm --pdf-engine=wkhtmltopdf --output README.pdf

– Asme Just – 2018-04-13T19:15:53.177

@m0nhawk, Is this what GitHub uses to render their README.md files? – joe_04_04 – 2019-09-29T18:35:47.537

56

You can also use Node.js based markdown-pdf

npm install -g markdown-pdf
markdown-pdf /path/to/markdown

Quanlong

Posted 2013-12-16T11:52:11.977

Reputation: 792

2This 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 – 2015-11-08T03:14:12.610

Would like to add that this has out-of-the-box Unicode support, which is very nice. – Niek – 2017-03-13T19:00:05.370

You can also install github-markdown-css to if you want to use GitHub's CSS file.

– jpmc26 – 2017-06-09T23:07:37.113

@jpmc26 how can you use it, I am no knowledge of javascript or npm? – Abhishek Bhatia – 2017-08-16T18:16:37.397

1

@AbhishekBhatia This is the first result when I Googled "npm introduction": http://smalljs.org/package-managers/npm/. It's just a file buried in node_modules once you install it.

– jpmc26 – 2017-08-16T18:51:29.713

markdown-pdf --css-path github-markdwon.css <path/to/markdown.md> (download github-markdown.css from https://github.com/sindresorhus/github-markdown-css0

– driedler – 2019-09-04T22:29:45.683

anchors don't work with this method. Only thing that works consistently is pandoc via this method: pandoc -f markdown -t html5 input.md -o output.pdf – James Campbell – 2019-10-30T03:09:28.927

43

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.

Magnus

Posted 2013-12-16T11:52:11.977

Reputation: 2 376

2I 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 – 2015-08-13T01:13:32.350

For those who use non-ASCII letters and get them missing: add font options to pandoc, like these: --variable mainfont="Liberation Serif" --variable sansfont="Liberation Sans" – gluk47 – 2016-09-13T20:18:44.957

5If someone is looking for just the commands: sudo apt install pandoc texlive-latex-recommended texlive-xetex texlive-luatex pandoc-citeproc etoolbox wkhtmltopdf (on Ubuntu, probably not all necessary), then pandoc --variable urlcolor=cyan myfile.md -o myfile.pdf – Tor Klingberg – 2017-06-05T15:31:27.400

@TorKlingberg Thanks, I think that is the best answer! – thc – 2017-09-02T19:22:43.423

This is the only thing that worked fully with anchors intact and other extra features: pandoc -f markdown -t html5 on-premise-admin-guide.md -o test.pdf worked after installing via homebrew and installing wkhtml2pdf. – James Campbell – 2019-10-30T03:10:18.287

4Thanks. Running pandoc -h did return support for GitHub Flavored Markdown, so I'm marking this question as resolved. – dillmo – 2014-02-11T23:49:39.783

12

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.

rpattabi

Posted 2013-12-16T11:52:11.977

Reputation: 593

5It does not work with images either. :( – Adam Arold – 2014-09-02T13:12:04.847

...and is poorly formated :( – Moebius – 2016-04-22T19:20:38.557

Interesting option. Pros: external links are maintained and accessible from the output. Cons: output is black and white, anchor links are removed, external links are not visually indicated because they are b&w. – rodey – 2018-01-10T14:49:37.950

8

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.

kevgathuku

Posted 2013-12-16T11:52:11.977

Reputation: 81

5Doesn't handle images in separate files. – Larry K – 2017-10-16T19:11:44.137

works beautifully – shadi – 2017-10-20T15:22:24.330

6

For those with Linux, use pandoc.

Install:

sudo apt install pandoc texlive-latex-extra

Yes, you need -extra package because of fonts.

Convert:

pandoc --from markdown -o output.pdf my-file.md

Andrejs Cainikovs

Posted 2013-12-16T11:52:11.977

Reputation: 2 611

Didn't include any images, links and ignored things like <font color="red"> though – derHugo – 2019-01-11T13:08:53.823

6

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.

DanteTheEgregore

Posted 2013-12-16T11:52:11.977

Reputation: 2 417

3

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

Saurabh Garg

Posted 2013-12-16T11:52:11.977

Reputation: 31

doesn't work with zip file : couldn't find the markdown file in the archive while there is one. – Moebius – 2016-04-22T19:26:17.570

got "error in conversion to pdf". Using http://markdowntopdf.com instead from kevgathuku

– shadi – 2017-10-20T15:22:09.783

1

Mine solution: convert markdown with pandoc to html (don't forget to use css for pandoc to show table borders), then open it with libreoffice, choose an option export as pdf.

NB: neither of mentioned here and on the Internet solutions worked for me: 1) browser-based solutions (i.e. grip) are adding excess info, like page numbers, which I didn't manage to remove, 2) pandoc convertage to pdf is broken, for me it generates an empty table (perhaps because of unicode, and yes, I configured it to use xetex), 3) site based solutions (i.e. gitprint.com) are also adding redundant things, like github-like margins, whereas I need just a simple table I generated with awk!

Hi-Angel

Posted 2013-12-16T11:52:11.977

Reputation: 384

1pandoc should work for you with options like --variable mainfont="Liberation Serif" --variable sansfont="Liberation Sans" – gluk47 – 2016-09-13T20:17:54.920

@gluk47 cool, that works! – Hi-Angel – 2016-09-19T14:10:08.277

I can recommend this page for converting markdown to PDF (or for printing) http://www.markdownprint.com.

– Johan O – 2018-04-21T13:56:11.003

@JohanO markdownprint.com seems to be down

– derHugo – 2019-01-11T13:10:08.833

1

I have tried several chrome plugins and online converters.
MDtr2PDF is the best one. It supports Github-flavoured-markdown and unicode.

doclin

Posted 2013-12-16T11:52:11.977

Reputation: 21

1Does it support images stored in other files? – Larry K – 2017-10-16T19:12:48.163

No longer seems to be working, and is not TLS-secured. – David Oliver – 2019-05-16T14:47:23.400

This link is dead – Adam Lassek – 2020-01-30T17:24:35.420

1

I refined this snippet for my personal needs:

# sudo apt install grip wkhtmltopdf

MD=${1:-README.md}
PDF=${2:-"$MD".pdf}
PORT=8971
DELAY=10

printf "Converting $MD to $PDF on port $PORT\n"
printf "Waiting $DELAY seconds for server to start...\n"

grip "$MD" localhost:$PORT &
sleep $DELAY
wkhtmltopdf http://localhost:$PORT "$PDF"
kill $(ps -eo pid,command | grep grip | grep -v grep | awk '{print $1}')

Save as /usr/local/bin/md2pdf and sudo chmod +x /usr/local/bin/md2pdf afterwards.

Usage:

  • md2pdf converts README.md to README.md.pdf

  • md2pdf foo.md converts foo.md to foo.md.pdf

  • md2pdf foo.md bar.pdf converts foo.md to bar.pdf

fredoverflow

Posted 2013-12-16T11:52:11.977

Reputation: 193

1

I had the most luck with VSCode and the Markdown PDF extension. The online converters screwed up the encoding for my files or inserted watermarks in the header.

Usage:

  1. Install the extension
  2. Open your MD file with VSCode, make sure Markdown highlighting is enabled
  3. Open the command palette (F1), type export and select markdown-pdf: Export (pdf)

NB: The extension installs and uses a local version of Chromium in the background.

There are several options to control formatting, they are all explained on the website linked above

Lennart

Posted 2013-12-16T11:52:11.977

Reputation: 322