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.
-
1The 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](https://github.com/github/linguist) to provide syntax highlighting. – DanteTheEgregore Dec 16 '13 at 13:37
-
1@dillmo, Convert [to HTML first](http://stackoverflow.com/q/7694887/632951), then use Chrome to print-to-pdf. – Pacerier Feb 18 '16 at 20:30
-
Update 2022:If anyone stumbles upon this and you don't want to install any libraries and just want to download a hassle-free PDF or HTML, just go to [dillinger.io](https://dillinger.io/) and paste the markdown content and then from the top menu, you can simply export it as a PDF or HTML. It preserves the formatting, is free, and no need to install any library or tool. – Xonshiz Jun 21 '22 at 16:00
-
Copy the _rendered_ content from a markdown viewer (in my case it was Simplenote) and paste it to Google Docs. Then choose File -> Download -> PDF Document – Anton Belonovich Jul 19 '22 at 11:14
15 Answers
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 or similar (ex: go to http://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).
- 1,818
- 2
- 22
- 34
- 2,326
- 1
- 13
- 5
-
3
-
34`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 01: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
-
1
-
10Alternatively 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 Jan 04 '16 at 19:54
-
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 Mar 07 '16 at 14:05
-
@Joost [This](http://pastebin.com/V4ccMVpK) is how I removed the borders. It did the job, but is ugly and might not work in all cases. – Feb 02 '17 at 22:40
-
7You can use Pandoc with the light-weight [wkhtmltopdf](https://wkhtmltopdf.org/downloads.html) instead of latex like this: `pandoc README-Template.md --from=gfm --pdf-engine=wkhtmltopdf --output README.pdf` – Asme Just Apr 13 '18 at 19:15
-
@m0nhawk, Is this what GitHub uses to render their README.md files? – JosephTLyons Sep 29 '19 at 18:35
-
Compare `grip`'s output to [Markdown Viewer](https://chrome.google.com/webstore/detail/markdown-viewer/ckkdlimhmcjmikdlpkmbgfkaikojcbjk). I think Markdown Viewer looks much better. See my answer I just added here: https://superuser.com/a/1591372/425838. – Gabriel Staples Oct 08 '20 at 05:14
-
1Results with `grip` look better than `markdown-pdf`, but the problem is that the MarkDown code blocks which are too long are shown in PDF with a scrollbar, obviously not interactive, therefore, part of their info is hidden. – forvas Oct 15 '21 at 11:17
-
You can also use Node.js based markdown-pdf
npm install -g markdown-pdf
markdown-pdf /path/to/markdown
- 1,250
- 11
- 14
-
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 Nov 08 '15 at 03:14
-
1Would like to add that this has out-of-the-box Unicode support, which is very nice. – Niek Mar 13 '17 at 19:00
-
You can also install [github-markdown-css](https://github.com/sindresorhus/github-markdown-css) to if you want to use GitHub's CSS file. – jpmc26 Jun 09 '17 at 23:07
-
@jpmc26 how can you use it, I am no knowledge of javascript or npm? – Abhishek Bhatia Aug 16 '17 at 18:16
-
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 Aug 16 '17 at 18:51
-
markdown-pdf --css-path github-markdwon.css
(download github-markdown.css from https://github.com/sindresorhus/github-markdown-css0 – driedler Sep 04 '19 at 22:29 -
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-see Oct 30 '19 at 03:09
-
Doesn't work for me: `internal/validators.js:125` `TypeError [ERR_INVALID_ARG_TYPE]`. I opened an issue here: https://github.com/alanshaw/markdown-pdf/issues/194. – Gabriel Staples Oct 08 '20 at 06:21
-
4This can also be used with `npx` in place of `npm` to avoid having to install globally for one-off usage: `npx markdown-pdf /path/to/markdown` – vulpxn Dec 28 '20 at 03:05
-
After installation got error `Version 9 of Highlight.js has reached EOL and is no longer supported.` – jcubic Sep 19 '21 at 19:08
-
This tool takes HTML code literally, so if you have any HTML in your markdown, this sadly won't work. – baltermia Sep 28 '21 at 10:19
-
If you have relative path images in your markdown, make sure to run the tool from the same directory as the .md file so that the relative paths resolve correctly. – Patrick Aug 08 '22 at 21:39
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.
- 133
- 1
- 7
- 4,146
- 3
- 22
- 28
-
4Thanks. 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
-
4I 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 01:13
-
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 Sep 13 '16 at 20:18
-
8If 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 Jun 05 '17 at 15:31
-
-
1This 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-see Oct 30 '19 at 03:10
-
`pandoc` totally rocks. I used it for all documents in grad school and continue to use it at work. – WestCoastProjects Mar 23 '20 at 04:48
-
I just figured out how to use `pandoc` to match the GitHub style markdown output pretty well: https://stackoverflow.com/a/64257218/4561887. I'm still trying to figure out how to get the grey background behind the code blocks, however. If anyone figures that out. Please let me know! – Gabriel Staples Oct 08 '20 at 07:15
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.
- 241
- 2
- 4
Update 2021-11: The project was abandoned, the domain is now hosting ads or malware.
If the markdown file was hosted on github repository, gitprint was an interesting option to create pdf / print.
All you needed to do is to replace github.com by gitprint.com in the URL.
Unfortunately, it does not work on markdown gists, and works only with markdown files at the repository.
- 204
- 1
- 2
- 9
- 681
- 6
- 8
-
6
-
1
-
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 Jan 10 '18 at 14:49
-
2
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
- 2,820
- 22
- 22
-
4
-
Today, I see pandoc DOES include images that are inserted the markdown way. However, it ignores html markup, such as `
– pauljohn32 May 30 '23 at 20:38
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.
- 2,508
- 2
- 21
- 36
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:
- Install the extension
- Open your MD file with VSCode, make sure Markdown highlighting is enabled
- Open the command palette (F1), type
exportand selectmarkdown-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
- 363
- 5
- 18
-
This worked for me on Windows, but I had to add `"markdown-pdf.breaks": true` to the settings JSON file to see new lines in the PDF. Also `"markdown-pdf.displayHeaderFooter": true` to hide some information which was added to the PDF header and footer. And `"markdown-pdf.styles": [ "C:\\Users\\
\\Documents\\markdown-pdf.css" ]` to change the CSS based on https://github.com/yzane/vscode-markdown-pdf/blob/master/styles/markdown-pdf.css because I did not like the default color for inline code. – hb20007 Aug 06 '21 at 15:51
I'm still looking for a command-line solution which produces results this high-quality [Update: I came up with a pandoc command which uses CSS for formatting, and looks pretty good: pandoc -f gfm -t html5 --metadata pagetitle="test.md" --css github.css test.md -o test.pdf--see my other answer here now!], but:
My preferred technique, because it looks so good, is to use the Markdown Viewer plugin in Chrome. It works really well, and looks surprisingly similar to GitHub markdown! Just open your markdown file in Chrome with this plugin installed and activated, then use the menus to print and save as a PDF right from Chrome.
Markdown Viewer boasts the following features (emphasis added):
✔ Renders local and remote URLs
✔ Granular access to remote origins
✔ Multiple markdown parsers
✔ Full control over the compiler options
✔ Themes (including GitHub theme)
✔ GitHub Flavored Markdown (GFM)
✔ Auto reload on file change
✔ Syntax highlighted code blocks
✔ Table of Contents (TOC)
✔ MathJax and Emoji support
✔ Remembers scroll position
✔ Markdown Content-Type detection
✔ URL detection using RegExp
✔ Toggle Content Security Policy (CSP)
✔ Override page encoding
✔ Settings synchronization
✔ Raw and rendered markdown views
✔ Free and Open Source
Example Markdown file
Here is a sample markdown file, which is a snippet from my example markdown demo file in my project here:
test.md:
## 1.1. Align images left, right, or centered, with NO WORD WRAP:
This:
```html
**Align left:**
<p align="left" width="100%">
<img width="33%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
**Align center:**
<p align="center" width="100%">
<img width="33%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
**Align right:**
<p align="right" width="100%">
<img width="33%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
```
Produces this:
**Align left:**
<p align="left" width="100%">
<img width="33%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
**Align center:**
<p align="center" width="100%">
<img width="33%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
**Align right:**
<p align="right" width="100%">
<img width="33%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
If you'd like to set the text itself to left, center, or right, you can include the text inside the `<p>` element as well, as regular HTML, like this:
```html
<p align="right" width="100%">
This text is also aligned to the right.<br>
<img width="33%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
```
Markdown Viewer output:
The above markdown file produces this output directly in my Chrome browser when viewed with the Markdown Viewer plugin installed and on. Notice it has language-aware syntax highlighting for code blocks, nice-looking, greyed code backgrounds, and reasonable column widths for output content.
Its output looks really nice, and is almost identical to GitHub's markdown output!
It supports themes too. See the "GitHub" theme is selected here, for instance. Just click the little m plugin icon in the top right of your Chrome browser to choose Markdown Viewer settings you want:
grip output:
Contrast this to the output from grip, which I think doesn't look nearly as good. grip doesn't have syntax highlighting for code blocks, greyed background for code, nor reasonable column widths for the output content. On a wide monitor, it takes up the whole width.
On Linux Ubuntu:
sudo apt update
sudo apt install grip
grip test.md
Then Ctrl + click on the url it gives you, to open the output in your browser. Ex: at http://localhost:6419/. Here's the grip-rendered output I see in Chrome:
See also:
- [my answer] How to convert Markdown + CSS -> PDF?
- 1,818
- 2
- 22
- 34
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
- 27,064
- 61
- 75
- 116
- 31
- 1
-
doesn't work with zip file : couldn't find the markdown file in the archive while there is one. – Moebius Apr 22 '16 at 19:26
-
got "error in conversion to pdf". Using http://markdowntopdf.com instead from kevgathuku – Shadi Oct 20 '17 at 15:22
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:
md2pdfconverts README.md to README.md.pdfmd2pdf foo.mdconverts foo.md to foo.md.pdfmd2pdf foo.md bar.pdfconverts foo.md to bar.pdf
- 203
- 2
- 7
-
Add `--export` to grip call so it exports to a HTML file instead of running a webserver and you might skip sleeping and replace kill with just removal of the temporary HTML file. – Najki Jun 15 '21 at 12:16
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!
-
1`pandoc` should work for you with options like `--variable mainfont="Liberation Serif" --variable sansfont="Liberation Sans"` – gluk47 Sep 13 '16 at 20:17
-
-
I can recommend this page for converting markdown to PDF (or for printing) http://www.markdownprint.com. – Johan O Apr 21 '18 at 13:56
-
@JohanO [markdownprint.com](http://www.markdownprint.com/) seems to be down – derHugo Jan 11 '19 at 13:10
I just convert from HTML instead. This works for my needs:
https://github.com/dompdf/dompdf
I found that in general Markdown is not a good format to convert to PDF, as it doesnt have native CSS support. Here is the script I use:
<?php
require 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->getOptions()->setIsFontSubsettingEnabled(true);
$s_in = file_get_contents('index.html');
$dompdf->loadHtml($s_in);
$dompdf->render();
$s_out = $dompdf->output();
file_put_contents('index.pdf', $s_out);
This solution just needs PHP (25 MB) and DomPdf (4 MB), so quite lightweight compared to other options.
- 1
- 24
- 120
- 163
MDPDF - Markdown to PDF converter.
A command line markdown to pdf converter with support for page headers, footers, and custom stylesheets. Mdpdf is incredibly configurable and has a JavaScript API for more extravogant usage.
Installation
Install globally to use from the command line:
npm install mdpdf -g
Usage
$ mdpdf --help
Usage:
$ mdpdf <source> [<destination>] [options]
<source> must be a markdown file, with the extension '.md'.
Examples:
$ mdpdf README.md
$ mdpdf README.md --style=styles.css --header=header.hbs --h-height=22mm
$ mdpdf README.md --footer=footer.hbs --f-height=22mm --debug
$ mdpdf README.md --border-left=30mm
Options:
--style=<filename> A single css stylesheet you wish to apply to the PDF
--header=<filename> A HTML (.html) file to inject into the header of the PDF
--h-height=<height> The height of the header section
--footer=<filename> A HTML (.html) file to inject into the footer of the PDF
--f-height=<height> The height of the footer section
--border=<size> Border (top, left, bottom, right; default: 20mm)
--border-top=<size> Top border (default: 20mm)
--border-left=<size> Left border (default: 20mm)
--border-bottom=<size> Bottom border (default: 20mm)
--border-right=<size> Right border (default: 20mm)
--gh-style Enable default gh-styles, when --style is used
--no-emoji Disables emoji conversions
--no-highlight Disables syntax highlighting
--debug Save the generated html for debugging
--help Display this menu
--version Display the application version
--format=<format> PDF size format: A3, A4, A5, Legal, Letter, Tabloid (Default: A4)
--orientation=<orientation> PDF orientation: portrait or landscape (Default: portrait)
Length parameters (<height> and <size>) require a unit. Valid units are mm, cm, in and px.
Global Settings:
You can also set a global default stylesheet by setting the MDPDF_STYLES environment variable as the path to your single css stylesheet. The --style flag will override this.
- 329
- 2
- 11


