Skip to main content

Convert Markdown with LaTeX math to PDF using KaTeX and headless Chrome

Project description

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.

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.
  • Polished Typography: Features premium, print-optimized font pairings (Source Serif 4, Source Sans 3, and Source Code Pro) loaded dynamically from Google Fonts.
  • 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

md2pdf_tex-0.1.1.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

md2pdf_tex-0.1.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file md2pdf_tex-0.1.1.tar.gz.

File metadata

  • Download URL: md2pdf_tex-0.1.1.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for md2pdf_tex-0.1.1.tar.gz
Algorithm Hash digest
SHA256 34ce9ad89db43dcc7d38efdbf2bb2de183884408140bdbb4aec4646ac197f027
MD5 56bbf29f8e88f7a529b2941da4db1dfb
BLAKE2b-256 e2b10d7352a363113567ae747253d103cee4d77613766f617d5621907cda97f4

See more details on using hashes here.

Provenance

The following attestation bundles were made for md2pdf_tex-0.1.1.tar.gz:

Publisher: publish.yml on robertoffmoura/md2pdf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file md2pdf_tex-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: md2pdf_tex-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for md2pdf_tex-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d1bc72f74461582ae2f1394d139e8fdc571372365a8e00f5c187c0a3cdeb1526
MD5 d9b990b93241803f4acf0f05f6c3d1b1
BLAKE2b-256 f8f69c389f99e6721ba4338569489017085d44727752d30c3926f6438f8db627

See more details on using hashes here.

Provenance

The following attestation bundles were made for md2pdf_tex-0.1.1-py3-none-any.whl:

Publisher: publish.yml on robertoffmoura/md2pdf

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page