Markpdf: a best in class Markdown -> PDF tool

Markpdf is currently part of GitHub - ralsina/markterm · GitHub (along with markterm and markmark)

It’s a markdown->PDF converter based on litehtml and libharu

It parses markdown using markd then takes the HTML and uses libhtml to do layout and finally libharu to create a PDF.

Some of the highlights:

  • Can create a 600 A4 page PDF of Brothers Karamazov in under 5 seconds.
  • Tables
  • Footnotes
  • Annotations
  • Fenced code blocks with syntax highlighting
  • Inline and display math (cheating via libtexprintf
  • Fully justified with hyphenation (includes implementation of the TeX hyphenation algorithm)
  • Outline based on headings
  • Multiple image types, including svg (requires external tool)
  • Inline HTML rendered
  • HTML blocks rendered
  • emoji, accents, CJK
  • Page breaking with widow heading prevention
  • Pageless mode: all your PDF is a single page. Yes, you can do a page with all of Brothers Karamazov in it.
  • CSS stylesheets (a few included, user can add customized)
  • Color schemes

There isn’t AFAIK, a tool with this much feature coverage that doesn’t involve installing chrome or TeX or Typst. Markpdf is a 9MB static binary.

Here is an example of how the output looks:

As someone who uses Pandoc with Docker, I’m somewhat interested in this tool, but I’m not sure if it’s right for me. A website where you can view sample PDFs created with markpdf might be helpful for people who are on the fence about trying it out.

There will be, at some point.

There is now. Demo with full server-side rendering, can tweak the contents, look, templates, whatever.

Great hat-tip to @sdogruyol and the kemal project, which makes this sort of thing easy and enjoyable to create

I made a similar tool many years ago: GitHub - straight-shoota/md2pdf: Convert markdown files to pdf with flexible template and stylesheet · GitHub
It’s built on Crystal with markd as well, so I suppose it ticks most of the above features.
But I used wkhtmltopdf for rendering. So not the entire Chrome browser, but the webkit engine. I don’t think that exists anymore. It was perhaps a bit easier to just delegate layout and rendering to a single library.

On top, it also integrates Crinja for templating. Maybe that could be an inspiration for markpdf? :wink:

Emojis are broken :(

Probably lacking a font in the docker image :-)

I’ll look into it

I am half decided to support short codes (which are a way to embed crinja templates) but honestly it was so much work getting it to this stage I am hesitant.

Fixed!

Just noticed, this is where I got the idea! Thanks @hugopl !!!

Thank you for implementing something I wanted but would never have time to do so… at least until my kids grow.

Mine’s 19, so keep hope! :slight_smile:

The footnote is not on the foot :blush: but maybe is the issue with a text that span just a tiny bit over a page?

They are technically endnotes :-)

This command-line tool is handy, and I like the concept behind it. However, I’ve run into a few minor issues when trying to use it on macOS or when working with Japanese text, so I might submit a few pull requests in the future.

There is no text shaping so support for some scripts is … iffy.

Now with decent math:

And without TeX!

Ok, it links in MicroTeX, which is a C++ library and does a bridge to libharu so the output looks nice in the PDF

@ralsina is it possible to use it with a set of formatting rules, in my case for scientific / academic papers, when generating the PDF from a markdown?

It supports CSS (within limits)