Skip to main content

md2pdf

A minimalist, high-fidelity Markdown-to-PDF converter written in Python. It parses Markdown files containing LaTeX mathematical formulas, renders them using KaTeX, and outputs a print-perfect PDF using a headless Chrome instance via Selenium.

Sample PDF output from md2pdf

Generated from sample.md — try it yourself!

Features

  • High-Fidelity PDF Output: Employs headless Google Chrome to print the rendered HTML layout to PDF, preserving pagination, fonts, margins, and alignments.
  • LaTeX Math Support: Seamlessly renders inline math ($...$) and block math ($$...$$) via KaTeX.
  • Minimal Dependencies: Requires only Python, Chrome/Chromium, and the Python selenium library.
  • Clean Typography: Uses the system sans-serif font stack for a clean, familiar look, with Source Code Pro for inline code.
  • Clean Markdown Parser: A custom regex-based parser that translates standard Markdown elements into clean HTML while protecting LaTeX delimiters from markdown interference.

Installation

1. Prerequisites

  • Python 3.x
  • Google Chrome or Chromium browser installed on your system.

2. Install Package

You can install the package directly from PyPI:

pip install md2pdf-tex

Or install it globally as a standalone command-line tool using pipx (recommended):

pipx install md2pdf-tex

Note: Selenium Manager will automatically locate and download the appropriate driver (chromedriver) for your Chrome version. No manual driver setup is needed.


Usage

Run the converter from your terminal:

md2pdf <input.md> [output.pdf]
  • <input.md>: The path to your input Markdown file.
  • [output.pdf] (Optional): The path for the output PDF file. If omitted, it defaults to the input file's name with a .pdf extension in the same directory.

Example

Given a file document.md containing:

# Physics Report

Let's discuss the Maxwell's equations. In differential form, Faraday's law of induction is:

$$\nabla \times \mathbf{E} = -\frac{\partial \mathbf{B}}{\partial t}$$

Where:
- $\mathbf{E}$ is the electric field.
- $\mathbf{B}$ is the magnetic field.

Convert it using:

md2pdf document.md

This generates document.pdf with properly formatted headers, bulleted lists, and beautiful, high-resolution mathematical equations.


Supported Markdown Elements

  • Headings: # (H1), ## (H2), and ### (H3).
  • Inline Math: $ ... $ for inline formulas (e.g. $E = mc^2$).
  • Block Math: $$ ... $$ for centered display equations.
  • Unordered Lists: - item and indented - subitem.
  • Text Styling: **bold**, *italic*, and inline `code` fragments.
  • Paragraphs: Contiguous lines are automatically joined into standard justified paragraphs.

How It Works

  1. Preprocessing: The script reads the input Markdown, identifies math blocks, and translates markdown elements (headers, lists, styling) to semantic HTML tags.
  2. HTML Generation: It constructs a self-contained HTML document loading KaTeX stylesheets/scripts and applying typography rules.
  3. Rendering & Math Processing: Selenium starts a headless Chrome browser, loads the HTML, and waits for KaTeX's auto-render extension to process the mathematical formatting.
  4. PDF Printing: Headless Chrome's print-to-PDF functionality (Page.printToPDF via Chrome DevTools Protocol) is triggered with A4 measurements and standard margins to produce a high-fidelity document.

Metadata

Release files for md2pdf-tex 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for md2pdf-tex 0.1.3
File Size Uploaded
md2pdf_tex-0.1.3.tar.gz 8.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for md2pdf-tex 0.1.3
File Interpreter ABI Platform
md2pdf_tex-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 18.3 kB

Release files / md2pdf_tex-0.1.3.tar.gz

Download URL md2pdf_tex-0.1.3.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f1a0696f23c3d61ef3e36cfe680431287455b6dc3360232765270e91dfe4d8c9
BLAKE2b-256 checksum
How to use checksums
248a13fdab13d9375122225e274cd295fff00002bcf30960d545dad72adf503f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 23, 2026.

Transparency log

Release files / md2pdf_tex-0.1.3-py3-none-any.whl

Download URL md2pdf_tex-0.1.3-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7c6dc016e53ee7c18e3c0d239c67d2c9df00aa03a9eb9adad440acd93f167fba
BLAKE2b-256 checksum
How to use checksums
fb4769f3befd4a7ea082b6e8bc55060b4796a4a63858ec91a85afc7bbeda84f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 23, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page