27

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.

2

6 Answers 6

27

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.

4
  • I found a good github.css style sheet and used it in my answer here: stackoverflow.com/a/64257218/4561887. Oct 8, 2020 at 7:19
  • I know this is an old thread, but I'm using an R markdown (with the bookdown package) and I can't render this file to a PDF version because of my style.css. Where should I insert this command "pandoc -t " ? . I appreciate any help on this. Thanks @mb21
    – Luis
    Feb 21, 2021 at 16:34
  • "I suspect if you want a beautifully-typeset PDF for free you'll have to learn LaTeX" — problem is, writing/reading in Markdown is much less painful than in LaTeX. So you would want to write in markdown and interject it with some LaTeX-tags I guess, similar to html-tags? Which I don't think is possible.
    – Hi-Angel
    Apr 4, 2021 at 15:32
  • 1
    @Hi-Angel yes you can include raw TeX in pandoc's markdown
    – mb21
    Apr 5, 2021 at 8:25
5

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

5

How to convert a markdown .md document to a PDF at the command-line by using pandoc and CSS style settings

With the right settings, pandoc does a pretty good job, but is still missing the grey background underneath the code blocks which I'd really like it to have :(. Following the lead of @mb21's answer, here's what I came up with for a pretty decent pandoc command for GitHub Flavored Markdown (gfm).

Tested on Ubuntu 20.04:

# Install pandoc and dependencies
sudo apt update
sudo apt install pandoc
sudo apt install wkhtmltopdf  # a dependency to convert HTML To pdf

# Download the github.css CSS style theme
wget https://raw.githubusercontent.com/simov/markdown-viewer/master/themes/github.css

# Convert test.md to test.pdf using the github.css CSS style theme
pandoc -f gfm -t html5 --metadata pagetitle="test.md" --css github.css \
test.md -o test.pdf

The wget command is to download the github.css GitHub CSS formatting theme file from here: https://github.com/simov/markdown-viewer/tree/master/themes. It is part of the Markdown Viewer Chrome plugin here, which I wrote about in my other answer here.

Breakdown of the pandoc command from above:

-f gfm    # from format = Github Flavored Markdown
-t html5  # to format = html5
--metadata pagetitle="test.md"  # html output format (-t html) requires a 
    # mandatory html title, so just set it to the input file name:
    # "test.md"
--css github.css  # use the github.css file as the CSS styling file for
                  # the html output
test.md      # this is the INPUT markdown (Github Flavored Markdown) file
-o test.pdf  # save the OUTPUT PDF as test.pdf 

Sample markdown file, test.md:

Snippet from my project here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/markdown/github_readme_center_and_align_images.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>
```

Pandoc conversion command from above:

pandoc -f gfm -t html5 --metadata pagetitle="test.md" --css github.css \
test.md -o test.pdf

Output PDF screenshot:

Not quite as good as Markdown Viewer, as its still missing the grey background under the code blocks (see what that looks like in my other answer here), but it looks pretty good!

enter image description here

See also:

  1. [my answer] SuperUser: How Can I Convert Github-Flavored Markdown To A PDF
1
  • 1
    btw. the next release of pandoc will also contain this default CSS...
    – mb21
    Oct 8, 2020 at 10:21
2

You can use gh-md-to-html for this, which is a command line tool that does exactly what you want (full disclosure: I'm the author).

You can install it by installing wkhtmltopdf and by then using

pip3 install gh-md-to-html[pdf_export]

And then use

gh-md-to-html path_to_your_file.md -p <name>.pdf -c path_to_your_css.html

Let's dissect what the individual parts of this command do:

  • The -p option declares under which file name to save the resulting pdf file; the "<name>" is automatically replaced with the name of your input file.
  • The -c option is a path to a html-file which contains css within <style>-tags, which will be embedded into the resulting html file before said file is converted to pdf.

Under to hood, gh-md-to-html converts the file to html and then to pdf using wkhtmltopdf, as the name suggests.

The resulting pdf file is, in any case, styled similar to how GitHub styles their README files; if you want to disable that so you can dictate the styling as a whole using you custom css, you can supply the option -s false to the command, which disables the default styling. Code blocks are properly syntax highlighted in both cases, though.

The conversion process is done partially online (using GitHub's markdown REST API); in case you don't want that, you can use pip3 install gh-md-to-html[offline_conversion] and then run gh-md-to-html with the -o OFFLINE option.

0

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.

0

Recently I had the same requirement. While pandoc works well, I don't really like the styling and it is very complex to download a CSS and make it work with that. Maybe I am just too lazy!

Instead, here is a quick hack. You can create your markdown on the jupyter notebook and just download that as PDF. Yes, it requires you to download a few libraries but I love the font and styling.

Another simpler way would be to click on File -> Print Preview from your jupyter notebook and print that as a PDF.

1
  • You can also explore the Download as menu from your jupyter notebook for further customizations. For eg: download as HTML and make modifications to it.
    – AverageGod
    Aug 20 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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