DEV Community

Cover image for Converting markdown to pdf in Python
Vitaly Bogomolov
Vitaly Bogomolov

Posted on • Edited on

32

Converting markdown to pdf in Python

There are many solutions for the task of automatically converting text in markdown format into a printable pdf file. These solutions based on Pandoc, LaTex, wkhtmltopdf, etc.

I want to share a new (at least for me) solution to this problem in Python.

This solution has the following advantages.

  • All dependencies are in requrments.txt, no external binaries.
  • There are no problems with non-standard encoding, pictures and the most popular markup elements.
  • Ability to use different page sizes within one pdf.
  • Customizable mode for creating a table of contents (bookmarks).

This method is based on the use of the libraries markdown-it-py (conversion from markdown to html) and PyMuPDF (conversion from html to pdf). A small Python class links them together.

Install

pip install markdown-pdf
Enter fullscreen mode Exit fullscreen mode

Create a pdf with TOC (bookmarks) from headings up to level 2.

from markdown_pdf import MarkdownPdf

pdf = MarkdownPdf(toc_level=2)
Enter fullscreen mode Exit fullscreen mode

Add three sections of markdown to the pdf. Each section starts on a new page. Headings from the first section are not included in the TOC.

from markdown_pdf import Section

pdf.add_section(Section("# Title\n", toc=False))
pdf.add_section(Section("# Head1\n\nbody\n"))
pdf.add_section(Section("## Head2\n\n### Head3\n\n"))
Enter fullscreen mode Exit fullscreen mode

Set the properties of the pdf document.

pdf.meta["title"] = "User Guide"
Enter fullscreen mode Exit fullscreen mode

Save to file.

pdf.save("guide.pdf")
Enter fullscreen mode Exit fullscreen mode

Done!

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more