DEV Community

loading...

Various means for Markdown to PDF via Pandoc (not always HTML/CSS friendly)

patarapolw profile image Pacharapol Withayasakpunt ・2 min read

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,

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)
Enter fullscreen mode Exit fullscreen mode

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.

\setuppapersize[A4, portrait]
\setuplayout[
  header=0pt,
  margin=2cm,
  footerdistance=0pt]
Enter fullscreen mode Exit fullscreen mode

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.

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.

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

GitHub logo patarapolw / make-pdf

Beautifully make a pdf from couples of image files

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 or Electron? 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. Not sure how well it compared to all other options. But again, I have to download a binary for this...

Discussion

pic
Editor guide
Collapse
stereoplegic profile image
Mike Bybee

I love pandoc for markdown, HTML, and docx conversion (with a template docx and customized styles, or else it'll be ugly), but there are much easier options for PDF. Puppeteer and React-PDF are infinitely better.

Collapse
patarapolw profile image
Pacharapol Withayasakpunt Author

Actually, there are many tools to create PDF from JavaScript with total customizability; but... not zero setup.

That means I cannot use <h1> directly...

But it seems that I can indeed customize the MDX...

Collapse
patarapolw profile image
Pacharapol Withayasakpunt Author

I feel that React-PDF + web browser / Puppeteer / Electron should have perfect customizability.

Anyone tried this with MDX?

Collapse
zoedreams profile image
☮️✝️☪️🕉☸️✡️☯️

Oracle has the lock down on this. I used this tech for a eDiscover job i had like 15 years gao.