Various means for Markdown to PDF via Pandoc (not always HTML/CSS friendly)
Tags: pandoc, markdown, pdf, latex
Published: 2020-10-06 09:48:21

This is also the case with RMarkdown.

I am doing this on Ubuntu Linux, and it might work easily for macOS, but not sure about Windows.

## Installing pandoc is only a starting point. Dependencies are required.

To get started,

```sh
sudo apt install pandoc
sudo apt install texlive-full # If you are planning on converting via LaTeX or ConTeXt
sudo apt install wkhtmltox # If you are planning on converting via HTML engine
sudo apt install inkscape libsvg-bin # If you are planning on using SVG (at least in TeX)
```

## Margins might not be what you want, and YAML frontmatter is a way, not always Markdown content

I haven't find a way to do this in all LaTeX, ConTeXt, and HTML mode.

Also, for LaTeX and ConTeXt, you can put the TeX code directly in Markdown.

In ConTeXt, where I see as most flexible, you can change paper sizes and margins easily as well.

- https://wiki.contextgarden.net/Layout
- https://tex.stackexchange.com/questions/71048/how-to-set-the-page-layout-dimensions-in-context

```tex
\setuppapersize[A4, portrait]
\setuplayout[
  header=0pt,
  margin=2cm,
  footerdistance=0pt]
```

IIRC, in LaTeX, the only way is to put `geometry: a4,margin=2cm` inside YAML frontmatter.

## In TeX mode, CSS styling and extended Markdown are not always working (as far as I can find)

I need this for image resizing. I don't know which Markdown parser supports everything I want, unless I extend the engine myself (as I did in Showdown.js and Markdown-it).

This post, where I find later, might help to answer.

{% post https://dev.to/learnbyexample/customizing-pandoc-to-generate-beautiful-pdfs-from-markdown-3lbj %}

## Syntax highlighting is not supported in ConTeXt, only in LaTeX and HTML5 (with wkhtmltopdf)

I suffered from this greatly, albeit I can fix with some kind of Markdown to PNG.

{% post https://dev.to/patarapolw/pandoc-file-md-t-context-doesn-t-have-syntax-highlighting-50l3 %}

## Markdown is not a templating engine, and cannot include other files

Where TeX can, but `\input{}` cannot include Markdown files, only TeX.

So, I fixed it with another templating engine (Handlebars). (See [Pandoc branch](https://github.com/patarapolw/make-pdf/tree/pandoc).)

{% github patarapolw/make-pdf no-readme %}

## Pandoc requires you to be the correct Current Working Directory

To locate local images, or you don't want to put images in your PDF?

## Lastly, Pandoc + texlive-full are a hell of installation 

Combining together, both are large (5GB), and slow-to-compile (especially ConTeXt MarkIV).

It might not be that large, if I use merely wkhtmltox (or topdf).

But, can I trust it better than [Puppeteer](https://www.npmjs.com/package/percollate) or [Electron](https://www.npmjs.com/package/electron-pdf)? Not only smaller, but I also know what I am expecting.

However, Electron is much less customizable than Puppeteer.

https://www.npmjs.com/search?q=html%20pdf

There is also [a way via PhamtomJS](https://www.npmjs.com/package/html-pdf-lts). Not sure how well it compared to all other options. But again, I have to download a binary for this...