HACKER Q&A
📣 SamCoding

Where are the good Markdown to PDF tools (that meet these requirements)?


I'm trying to convert a very large Markdown file (a couple hundred pages) to PDF.

It contains lots of code in code blocks and has a table of contents at the start with internal links to later pages.

I've tried lots of different Markdown-PDF converters like md2pdf and Pandoc, even trying converting it through LaTeX first, however none of them produce working internal PDF links, have effective syntax highlighting for HTML, CSS, JavaScript and Python, and wrap code to fit it on the page.

I have a very long regular expression (email validation of course) that doesn't fit on one line but no solutions I have found properly break the lines on page overflow.

What tools does everyone recommend?


  👤 tikhonj Accepted Answer ✓
I worked on a 500+ page book[1] in Pandoc that included a bunch of code samples, math, a table of contents with working links and an index. (In hindsight, I wish we had thought about the index from the beginning rather than adding it after the fact.)

What worked well for me: Pandoc with a custom LaTeX template, and a decent amount of inline LaTeX to handle edge cases. We had a LaTeX theme to use from our publisher, but we also needed our own totally separate theme for the free version version of the book.

For one-off things like really long code lines, I found it best to manually figure out how to handle them. Sometimes there was a bit of TeX magic but, more often, I just rewrote or reorganized the code. I see the presentation and structure of code and math snippets as an integral part of how I'm communicating the underlying ideas, so manually changing things around to read better was fundamentally no different from going back and editing prose.

Unfortunately, this also means that the process was relatively hands-on. If you need something ≈completed automated, I expect Pandoc → LaTeX is going to fall a bit short. Edge cases need manual intervention, and it's easy for formatting errors to sneak in—the free version of our book has some formatting mistakes like code bleeding into the margin because I ran out of energy to fix all of them!

[1]: https://github.com/TikhonJelvis/RL-book


👤 solardev
Have you explored the AST (abstract syntax tree) tools yet, like Mdast and the related remark and micromark?

https://github.com/syntax-tree/mdast-util-from-markdown

It might work better if you parse it into an intermediary Mdast format first, do whatever processing you need to implement "pages" (not a part of any Markdown dialect I'm familiar with?" but it shouldn't be hard to write a custom parser for that in Mdast), output that to HTML (via https://github.com/syntax-tree/mdast-util-to-hast) and THEN convert the HTML to PDF.

The AST tools basically give you structured JSON that's much easier to work with programmatically than raw Markdown. Then you can render that semantic JSON into HTML or other outputs.


👤 adolph
It may be worthwhile to take a deeper look at Pandoc if other replies don’t respond with something easier.

In a recent Talk Python to Me podcast [0], the Quarto [1] developers talked about how they are using Pandoc’s Lua interpreter [2] to perform transformations that aren’t part of vanilla pandoc in.md -o out.pdf.

0. https://talkpython.fm/episodes/show/493/quarto-open-source-t...

1. https://quarto.org/

2. https://pandoc.org/custom-writers.html


👤 pronoiac
I think Pandoc and Calibre could work for you.

I've worked on PAIP, Paradigms of Artificial Intelligence Programming, and I might be able to help you a bit. It's around 1k pages long. I used Pandoc to generate an epub file, and then Calibre to turn that into a PDF file. I just tried using Pandoc to generate the PDF file directly, and it/LaTeX choked on some Unicode characters.

For internal ebook links, there's a Lua script. You'll have to keep anchors unique across the book for this:

* good: "chapter1#section1_1" and "chapter2#section2_1"

* bad: a "chapter1#section1" and a "chapter2#section1"

WIP: https://github.com/norvig/paip-lisp/pull/195

For line wrapping of code, there's CSS. I first used it over on "Writing an Operating System in 1,000 Lines"; here's the PR: https://github.com/nuta/operating-system-in-1000-lines/pull/...


👤 geor9e
I think you're overcomplicating it. I assume you created this markdown file and I assume you have a preview render that shows it the way you like it to be shown. So just hit the print button, and in the print dialog select save as PDF.

👤 jedberg
> I have a very long regular expression (email validation of course)

On a tangentially related note, I guarantee you that your regex is wrong. There is only one way to validate an email address:

Send an email to it and have them respond. Otherwise you will block some valid users.

Now of course you can make a regex that gets most email addresses, and if you're ok with that, then that's fine. But if you don't want to accidentally exclude someone, then sending email is the only way to validate it.


👤 martylamb
It's not marketed as a markdown-to-pdf tool, but I've found that Obsidian (https://obsidian.md) does an excellent job. Just create a new "vault", paste your markdown into a new note, and export to PDF.

👤 netbioserror
Typora is the best I've used. It's a GUI, but it's pretty fantastic for a GUI Markdown editor (especially an Electron one), and its PDF export is consistent and customizable with styles. Includes a few good ones out of the box. Plus an automated TOC.

👤 SamCoding
Hi, thanks for all the suggestions, Typst ultimately worked best, as I was generating my Markdown file with a script I could modify it to generate a Typst file and all of the links and highlighting worked beautifully.

👤 contingencies
IMHO electron based markdown editors are generally slow, bloated, short-lived, and often platform-limited.

Use this and add sed lines for any required non-breakyness per normal CSS, rules can be specific to @media print as required.

  $ cat ~/bin/mdview 
  #!/bin/bash
  # markdown viewer
  tmpfile=.mdview.tmp-`uuidgen`.html
  # start html
  echo "" >${tmpfile}
  # duplicate markdown for modification
  cp ${1} ${1}.mdtmp
  # add extra newline after trailing :
  sed -i -e 's/: \*$/:\r\r\n\n/' ${1}.mdtmp
  # generate HTML from markdown
  #  note the --html-no-skiphtml --html-no-escapehtml allows the preservation
  #  of  anchors within text to allow [link][#anchorname]
  lowdown --html-no-skiphtml --html-no-escapehtml -thtml ${1}.mdtmp >>${tmpfile}
  # remove the temporary markdown file
  rm ${1}.mdtmp
  # add newline before images
  sed -i -e 's/

👤 w4rh4wk5
It has been a while, but back them i cobbled together a pipeline using Pandoc [1]. Back then, I wrote my master thesis with this [2]. While the primary output is HTML, PDF is supported as well.

[1]: https://github.com/w4rh4wk/dogx

[2]: https://github.com/W4RH4WK/M.Sc.-Thesis/blob/master/output/t...


👤 fforflo
Does converting to HTML first and then to PDF help?

👤 Syzygies
I've been saving Markdown transcripts of my more involved AI chats, and I was unhappy with how any tool rendered to PDF. In either Cursor or Windsurf, I had Claude 3.5 Sonnet code a Ruby script for me that converts Markdown to Typst, a LaTeX alternative that looks a lot like Markdown. Typst offers beautiful formatting control for the output PDFs.

👤 countrymile
Quarto is worth looking at. Might not be able to solve you regex issue though.

👤 ludsan
I'm surprised Pandoc didn't fit the bill. It's quite configurable with fenced attributes.

I switched from using MD-->(Pandoc-->(latex))--> PDF to using MD-->(Pandoc-->(typst))--> PDF.


👤 agateau
> I have a very long regular expression (email validation of course) that doesn't fit on one line but no solutions I have found properly break the lines on page overflow.

Have you considered manually splitting the regular expression into multiple lines in the source document, using something like the `VERBOSE` mode from Python re module [1]?

[1]: https://docs.python.org/3/howto/regex.html#using-re-verbose


👤 WolfOliver
I would love to read your feedback how it works with MonsterWriter.

1. Download the app [01] 2. Create a new empty document 3. Insert a markdown section type 4. past your markdown code into the markdown section 5. click on "Preview & Export" 6. Configure your PDF

I'm the creator of MonsterWriter. For complex markdown it probably has some shortcomings but I would love to hear what is missing for your use case.

[01] https://www.monsterwriter.com/


👤 IshKebab
I wouldn't use Markdown if you want all those features. Use Pandoc to convert your Markdown to Asciidoc, and then use asciidoctor-pdf.

Unfortunately Asciidoctor is written in Ruby which makes it an arse to work with if you need to write any plugins. And the HTML output uses Google Fonts by default, so I don't think much of the authors. But it's probably the best authoring system I've found for programming style content. For scientific content I would use LyX or maybe Typst.


👤 marcrosoft
Render to html and then use webkit2pdf which will give you a pdf that looks exactly like the html shown in chrome. This is a million times easier than working with PDF libraries

👤 Ancapistani
I’ve not used it for very large documents, but I’ve been very happy with the fidelity of conversion using Marked 2 (https://marked2app.com)

I believe it’s Mac only. I use it sometimes when I’m creating PDFs from my personal documentation to share more publicly, which I keep in Markdown and deploy on Gitlab Pages as a static site.


👤 oulipo
Have you tried https://typst.app/?

👤 tmaly
Have you ever considered rendering the markdown to html then using headless Chrome to render to pdf?

👤 westurner
MyST-MD transforms to LaTeX or HTML, which are transformable to (PostScript and then) PDF. With LaTeX it's possible to exactly typeset.

Sphinx and jupyter-book support MyST Markdown.

PDF Tables of Contents with links to headings or page numbers are possible with MyST and RestructuredText.


👤 amgreg
If you’re on a Mac or iOS you could try creating a Shortcut where you input Markdown, convert to rich text, then output as a PDF. I use Shortcuts regularly. It’s pretty easy to set up. I haven’t tried it on something as larger as 500 pages, though. YMMV

👤 bobek
I had a decent success with pandoc and typst - https://www.bobek.cz/til/pandoc-markdown-typst/

👤 Yoric
Random idea: how hard would it be to convert your markdown to typst?

👤 Oras
weasyprint worked well for me. I'm using it in a service to export resumes.

Keep in mind that you'll need to install custom fonts if you're using languages other than English.



👤 misterspaceman
Have you already tried converting it in Google Docs?

👤 froh
re: regex can you choose a syntax that allows for manual line breaks and manual formatting or even comments? like re.X in python?

https://docs.python.org/3/library/re.html#re.X


👤 i_am_proteus
Quarto should "just work" for this. There's an option to wrap code blocks.

👤 dominicdoty
Whoa this is weird timing - just this weekend I did a little exploration of using Svelte to create documents and eventually PDFs.

Its really just a proof of concept at this point, but it might be of interest to you (and others).

Code: https://github.com/dominicdoty/sveltedoc

Rendered: https://sveltedoc.pages.dev/

Writeup: https://www.dominicdoty.com/2025/03/02/sveltedoc/

TLDR - I've been using Asciidoc a lot at work recently and was dissatisfied with it. This was an attempt at using Svelte to generate a document as a webpage that formats well when printed (or printed to PDF). All the power of HTML+CSS+JS when you want it, but the ease of use to just write markdown when you don't.


👤 jppope
typora.io is what I use

👤 Yoric
Have you tried mdbooks?

👤 geor9e
Another option is Google Docs via Tools > Preferences > Enable Markdown