How I create fancy PDF-documents in Markdown
I haven’t had the need to create any ‘fancy’ document in ages, but the other day I had to create a PDF-document with fancy formatting for a work related thing.
After some testing of various solutions and tools, I ended up with a setup using Pandoc, WeasyPrint and Markdown. I consider Markdown to be the best markup language and I use it for everything!
The source for this PDF-document looks like this:
# H1 Header
I really like using Markdown.
1. First item
2. Second item
3. Third item
4. Fourth item
> This is some quoted text.
```sh
#!/bin/sh
echo "This is a code block."
```
And this is the CSS I used:
:root {
--blue: #3465A4;
}
@font-face {
font-family: 'NotoSans';
src: url('NotoSans-Condensed.ttf') format('ttf')
}
html {
font-family: 'NotoSans';
line-height: 1.45em;
}
h1 {
font-size: 26px;
color: var(--blue);
padding-bottom: 0.15em;
border-bottom: 1px solid #CCC;
}
pre {
background-color: rgba(0,0,0,0.05);
border: 1px solid rgba(0,0,0,0.1);
padding: 1em;
font-size: 12px;
}
blockquote {
background-color: rgba(0,0,0,0.05);
border-left: 5px solid rgba(0,0,0,0.1);
padding: 0.1em 0.75em;
}
And this is the command I used to render the document:
$ pandoc --pdf-engine weasyprint -c style.css demo.md -s -o demo.pdf
Installation
To make this work you need to install Pandoc and WeasyPrint. In Gentoo the packages are called app-text/pandoc
and dev-python/weasyprint
.
With that said. I did not install Pandoc from source myself. It simply requires way too many packages for my liking (when compiled from source):
# emerge pandoc
[...]
Total: 133 packages (133 new), Size of downloads: 165,258 KiB
I instead used a binary package that’s available from their GitHub-page. The archive weights in at about 14 MB and unpacked at about 68 MB. I then just put the folder in ~/.local/bin/
and created a symbolic link to the binary file.