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.
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
seleniumlibrary. - 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.pdfextension 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:
- itemand indented- subitem. - Text Styling:
**bold**,*italic*, and inline`code`fragments. - Paragraphs: Contiguous lines are automatically joined into standard justified paragraphs.
How It Works
- Preprocessing: The script reads the input Markdown, identifies math blocks, and translates markdown elements (headers, lists, styling) to semantic HTML tags.
- HTML Generation: It constructs a self-contained HTML document loading KaTeX stylesheets/scripts and applying typography rules.
- 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.
- PDF Printing: Headless Chrome's print-to-PDF functionality (
Page.printToPDFvia Chrome DevTools Protocol) is triggered with A4 measurements and standard margins to produce a high-fidelity document.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file md2pdf_tex-0.1.2.tar.gz.
File metadata
- Download URL: md2pdf_tex-0.1.2.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
606b8f1b3126911b497ff51cebeb33d583bb08c5814e356d7bcb8171853ab0c2
|
|
| MD5 |
d70e6b3133e47c2fa41d9059992b5341
|
|
| BLAKE2b-256 |
bbdb94ebf9b23d2962484da793a5c3cc174127f12dd169860a19ef91a99eecd3
|
Provenance
The following attestation bundles were made for md2pdf_tex-0.1.2.tar.gz:
Publisher:
publish.yml on robertoffmoura/md2pdf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
md2pdf_tex-0.1.2.tar.gz -
Subject digest:
606b8f1b3126911b497ff51cebeb33d583bb08c5814e356d7bcb8171853ab0c2 - Sigstore transparency entry: 2049864684
- Sigstore integration time:
-
Permalink:
robertoffmoura/md2pdf@3031347ae9d395240abb06fef645a94581c63a84 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/robertoffmoura
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3031347ae9d395240abb06fef645a94581c63a84 -
Trigger Event:
release
-
Statement type:
File details
Details for the file md2pdf_tex-0.1.2-py3-none-any.whl.
File metadata
- Download URL: md2pdf_tex-0.1.2-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ef18fbe0fcd2d5320a2505d6ebb0db9ef08470076dce72a3ddc491b928f8c28
|
|
| MD5 |
c65fa913ee9bef835ff9097ef35f6125
|
|
| BLAKE2b-256 |
68f7d2fe486ba9e762fccfb76cc0dd78e992c8f6d9ac4395ee88d638bd731842
|
Provenance
The following attestation bundles were made for md2pdf_tex-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on robertoffmoura/md2pdf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
md2pdf_tex-0.1.2-py3-none-any.whl -
Subject digest:
5ef18fbe0fcd2d5320a2505d6ebb0db9ef08470076dce72a3ddc491b928f8c28 - Sigstore transparency entry: 2049865110
- Sigstore integration time:
-
Permalink:
robertoffmoura/md2pdf@3031347ae9d395240abb06fef645a94581c63a84 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/robertoffmoura
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3031347ae9d395240abb06fef645a94581c63a84 -
Trigger Event:
release
-
Statement type: