Pure-Rust PDF extraction that distills documents into clean, LLM-ready HTML — for LLMs and RAG, built on lopdf
Project description
distillPDF
Turn any PDF into clean, LLM-ready HTML or Markdown — structure-aware, pure-Rust, MIT-licensed.
distillpdf reads a PDF and reconstructs its structure — reading order, headings,
paragraphs, lists, tables, and figures — then emits compact, semantic HTML or
Markdown (or plain text) ready to feed to an LLM or a RAG pipeline. No styling noise, no
layout junk: just the content a model needs. Markdown is produced from the same HTML, so
both formats benefit from every extraction improvement.
It's built on lopdf and shipped to Python via
PyO3 + maturin as a small, self-contained
wheel — a lightweight, permissively licensed alternative to AGPL/heavyweight extractors
(PyMuPDF, pdfminer, Unstructured), with no system dependencies and no Python runtime deps.
🧪 Early release (
0.0.3) — testers wanted. The API is small and may still change. If you have PDFs that come out wrong, please open an issue with the file (or a description) — real-world documents are exactly what this needs to get better.
Install
pip install distillpdf
Prebuilt wheels; no compiler or system libraries required. Installing also puts a
distillpdf command on your PATH.
Command line
Convert a PDF to clean HTML or Markdown in one command:
distillpdf paper.pdf # HTML to stdout
distillpdf paper.pdf -o paper.html # ...or to an HTML file
distillpdf paper.pdf -o paper.md # ...or Markdown (inferred from the .md extension)
distillpdf paper.pdf --markdown # Markdown to stdout
distillpdf *.pdf -o out/ # batch: out/<name>.html per input
distillpdf paper.pdf -o p.html --image-mode external # lean HTML + an img/ folder
distillpdf paper.pdf --image-mode drop # replace images with placeholder text
distillpdf paper.pdf --mode page # page-first HTML (default is section-first)
distillpdf paper.pdf --no-toc # omit the table-of-contents nav
distillpdf paper.pdf --text # plain text instead of HTML
distillpdf paper.pdf --toc # print the table of contents
distillpdf paper.pdf --section abstract
(Also available as python -m distillpdf.)
Quickstart
import distillpdf
doc = distillpdf.open("paper.pdf") # or distillpdf.from_bytes(data)
# By default, these WRITE a file and return 1 (deriving the name from the PDF):
doc.to_html() # → paper.html (one self-contained file)
doc.to_markdown() # → paper.md + an img/ folder of figures
doc.to_html("out.html") # ...or to a specific path / directory
doc.to_html("out.html", image_mode="external") # ...lean HTML + an img/ folder
# Pass return_string=True to get the rendered text back instead of writing:
html = doc.to_html(return_string=True) # self-contained HTML string (inline images)
md = doc.to_markdown(return_string=True) # ...or Markdown (built from the same HTML)
# rendering options work the same on both:
doc.to_html(mode="page", toc=False)
text = doc.extract_text() # plain text, in reading order
toc = doc.toc() # [(level, title, page, anchor_id), ...]
abstract = doc.section("abstract") # targeted section extraction (returns a string)
Markdown
to_markdown() is a transform of the very HTML to_html() produces — so every
processor improvement (clipping, heading detection, tables, front-matter) flows into
Markdown automatically, with no second renderer to keep in sync.
doc.to_markdown() # string (images embedded inline)
doc.to_markdown("paper.md") # writes paper.md
doc.to_markdown("paper.md", image_mode="external") # paper.md + paper's img/fig_NN_slug.ext
doc.to_markdown(image_mode="drop") # drop images (caption-only placeholders)
image_mode controls figures — identically for to_html() and to_markdown():
image_mode |
result |
|---|---|
"embed" (default) |
inline base64 data: URIs — one self-contained string/file |
"external" |
extract each figure to img/fig_NN_slug.ext (vectors as .svg) and reference it; only when writing to a file (a returned string falls back to "embed") |
"drop" |
replace images with placeholder text |
Because both formats run through the same converter, "external" produces the same
img/ layout whether you write .html or .md.
Output modes
By default, logical sections are first-order: every heading becomes its own nested
<section id="sec-…">, so you can pull a whole section as one block (great for RAG / LLM
chunking), and page numbers are dropped.
distillpdf.open("paper.pdf").to_html(return_string=True)
# <section id="sec-abstract"><h2>Abstract</h2><p>…</p></section>
distillpdf.open("paper.pdf").section("methods") # → the <section id="sec-methods"> block
Pass mode="page" for the page-faithful structure instead — each page wrapped in
<section data-page="N" id="page-N">, with page numbers in the TOC:
distillpdf.open("paper.pdf").to_html(mode="page", return_string=True)
Want compact, text-only output? image_mode="drop" replaces each embedded image with a
lightweight <image N> placeholder (captions and figure anchors are kept):
distillpdf.open("paper.pdf").to_html(image_mode="drop", return_string=True)
# <figure id="fig-1"><image 1><figcaption>…</figcaption></figure>
Pass toc=False to skip the auto table-of-contents <nav> (heading anchors are still
emitted, so #section links and doc.section(...) keep working):
distillpdf.open("paper.pdf").to_html(toc=False, return_string=True)
Rendering options
open() only loads the PDF; the rendering options live on to_html() and
to_markdown() (and mode on toc()/section()), since that's where the content is
actually extracted:
| Option | Default | Effect |
|---|---|---|
path= |
None |
where to write: a file, or a directory to place <source-stem>.html/.md in. None writes <source>.html/.md next to the PDF. (Ignored when return_string=True.) |
return_string= |
False |
True returns the rendered string and writes nothing; the default writes a file and returns 1 |
mode= |
"section" |
"page" wraps each page in <section data-page="N"> and numbers TOC entries; the default groups content into nested <section id="sec-…"> and drops page info |
image_mode= |
"embed" |
"embed" inline data: URIs (self-contained); "external" an img/ folder (when writing to a file); "drop" placeholder text |
toc= |
True |
False omits the <nav> table of contents (section/heading anchors still emitted) |
Raw pieces
Need the structured data instead of HTML?
doc.extract_tables() # cell grids (handles multi-level / colspan headers)
doc.extract_images() # embedded images, with raw bytes
doc.extract_links() # hyperlinks with targets
doc.extract_fonts() # font inventory
doc.page_count() # number of pages
Why distillPDF
- Structure, not just text. Two-column reading order, multi-level table headers mapped
onto a single grid (
colspan), vector figures transcoded to inline SVG (including rotated axis labels), an auto-generated table of contents, and named section extraction (doc.section("methods")). - LLM-ready output. Lean, class-free HTML — semantic markup a model can read directly,
with anchor ids so
toc()entries link straight into the document. - Small & permissive. Pure Rust on
lopdf, MIT-licensed, no system dependencies, no Python runtime dependencies. Drops into any pipeline without license headaches. - Fast. Native Rust extraction with a release build tuned for speed (LTO, single codegen unit).
Scope
In scope: text, table, image, and font extraction, plus an HTML/markdown output layer for RAG and LLM ingestion.
Out of scope (for now): page rendering, PDF generation, OCR.
Comparison
| distillPDF | PyMuPDF | pdfminer.six | Unstructured | |
|---|---|---|---|---|
| License | MIT | AGPL / commercial | MIT | Apache (heavy deps) |
| Structure-aware HTML | ✅ | partial | ❌ | ✅ |
| System deps | none | none | none | many |
| Implementation | Rust | C | Python | Python |
Contributing & feedback
This is a young project and feedback is the fastest way to improve it. The most useful things you can do:
- Try it on your PDFs and tell me where the output is wrong — open an issue.
- Star the repo if it's useful, so others can find it.
- PRs welcome — see the development notes below.
Development
The test suite lives in tests/ (pytest) and runs on CI. It needs only
distillpdf installed. CI runs entirely on data we own — a self-contained demo PDF
(tests/demo/, end-to-end structure check) and a synthetic table corpus
(tests/corpus_tables/). The third-party PDF corpora (tests/corpus*/) are gitignored, so
their tests self-skip on a fresh clone and run only when the corpora are present locally for
deeper coverage.
Build from source with maturin:
git clone https://github.com/kkollsga/distillpdf
cd distillpdf
maturin develop --release # build + install into the current venv
bash tests/run.sh # build distillpdf + run pytest
pytest tests/ -q # or just run the tests against an installed build
License
MIT — see LICENSE. Use it anywhere, including commercial and closed-source projects.
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 Distributions
Built Distributions
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 distillpdf-0.0.18-cp38-abi3-win_amd64.whl.
File metadata
- Download URL: distillpdf-0.0.18-cp38-abi3-win_amd64.whl
- Upload date:
- Size: 1.5 MB
- Tags: CPython 3.8+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
34c9d305c4bdda8585c69ace84fb0b19394696e91209655570a791044dbb6674
|
|
| MD5 |
bb7016c197812a42930cac58feddb643
|
|
| BLAKE2b-256 |
35460fb00f9bca76c5dce8b51bd6cfa29dcb8f2dd800dffe9c2b618f402366d7
|
Provenance
The following attestation bundles were made for distillpdf-0.0.18-cp38-abi3-win_amd64.whl:
Publisher:
publish.yml on kkollsga/distillpdf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
distillpdf-0.0.18-cp38-abi3-win_amd64.whl -
Subject digest:
34c9d305c4bdda8585c69ace84fb0b19394696e91209655570a791044dbb6674 - Sigstore transparency entry: 1739366923
- Sigstore integration time:
-
Permalink:
kkollsga/distillpdf@d6829f55ae46ee689e1a601b214f6358a4be4ea1 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/kkollsga
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d6829f55ae46ee689e1a601b214f6358a4be4ea1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file distillpdf-0.0.18-cp38-abi3-manylinux_2_34_x86_64.whl.
File metadata
- Download URL: distillpdf-0.0.18-cp38-abi3-manylinux_2_34_x86_64.whl
- Upload date:
- Size: 1.6 MB
- Tags: CPython 3.8+, manylinux: glibc 2.34+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
407ca40f2c501e17be1f1c3a070d99ac4516257980d26dc57991d3a3e68b72fa
|
|
| MD5 |
3a95a7dde78d8ef5e5f6e43ca597ca6d
|
|
| BLAKE2b-256 |
27d3d1217565a2f80fa328ee63e9c4d0c313a28d7e7f9792fff222e8272347a8
|
Provenance
The following attestation bundles were made for distillpdf-0.0.18-cp38-abi3-manylinux_2_34_x86_64.whl:
Publisher:
publish.yml on kkollsga/distillpdf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
distillpdf-0.0.18-cp38-abi3-manylinux_2_34_x86_64.whl -
Subject digest:
407ca40f2c501e17be1f1c3a070d99ac4516257980d26dc57991d3a3e68b72fa - Sigstore transparency entry: 1739366940
- Sigstore integration time:
-
Permalink:
kkollsga/distillpdf@d6829f55ae46ee689e1a601b214f6358a4be4ea1 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/kkollsga
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d6829f55ae46ee689e1a601b214f6358a4be4ea1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file distillpdf-0.0.18-cp38-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: distillpdf-0.0.18-cp38-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 1.4 MB
- Tags: CPython 3.8+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8df3ce29ab2d15236ec3b4c47ec80cb855baf4ed581d1cd43a9a0da14f07975
|
|
| MD5 |
eb7704f33e308f3170eaf0919d472ad2
|
|
| BLAKE2b-256 |
0eab86f144d6b9155b0c39200ff07393ef8d9d3c06c79dc8604e196553c094a8
|
Provenance
The following attestation bundles were made for distillpdf-0.0.18-cp38-abi3-macosx_11_0_arm64.whl:
Publisher:
publish.yml on kkollsga/distillpdf
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
distillpdf-0.0.18-cp38-abi3-macosx_11_0_arm64.whl -
Subject digest:
f8df3ce29ab2d15236ec3b4c47ec80cb855baf4ed581d1cd43a9a0da14f07975 - Sigstore transparency entry: 1739366910
- Sigstore integration time:
-
Permalink:
kkollsga/distillpdf@d6829f55ae46ee689e1a601b214f6358a4be4ea1 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/kkollsga
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d6829f55ae46ee689e1a601b214f6358a4be4ea1 -
Trigger Event:
push
-
Statement type: