pdfsift 0.1.0

PDF to structured Markdown for LLM agents. Heuristics only: no AI model, no network.
# pdfsift

**PDF → structured Markdown, built for LLM agents.** Headings, paragraphs, lists and tables in reading order, with running headers, footers and line-end hyphens removed. Plain heuristics over what the PDF already contains: no AI model, no OCR, no network.

> **Status: alpha (0.1.0, not released yet).** Text, headings, lists and simple tables work well on the test corpus. Images are not exported yet (see [Limitations]#limitations).

```console
$ pdfsift chrome.pdf          # two-column test document (in Portuguese)
<!-- pdfsift: source=chrome.pdf pages=1-3/3 -->
<!-- page: 1 -->
# Colunas Estreitas, Hifenização e Ligaduras

Projeto pdfsift

Resumo

Este documento é composto em duas colunas estreitas para forçar a hifenização no fim das linhas, …

# Introdução

A extração de texto de um arquivo PDF parece simples até o primeiro documento em duas colunas. …
```

## Install

Coming with 0.1.0. Every channel ships the [PDFium](https://pdfium.googlesource.com/pdfium/) library (the PDF engine of Chrome) next to the executable; nothing is downloaded at run time.

| Channel | Command |
|---|---|
| npm (no install) | `npx pdfsift file.pdf` |
| PyPI (no install) | `uvx pdfsift file.pdf` or `pipx run pdfsift file.pdf` |
| Homebrew | `brew install vitorsilverio/tap/pdfsift` |
| Shell (Linux, macOS) | `curl -LsSf https://github.com/vitorsilverio/pdfsift/releases/latest/download/install.sh \| sh` |
| PowerShell (Windows) | `irm https://github.com/vitorsilverio/pdfsift/releases/latest/download/install.ps1 \| iex` |
| Archives | `.tar.xz`/`.zip` with SHA-256 and build attestations on [GitHub Releases]https://github.com/vitorsilverio/pdfsift/releases |
| Cargo | `cargo install pdfsift`, then get PDFium (below) |

Prebuilt for Linux (x86_64, aarch64; glibc 2.34+ for the archives, 2.17+ for the PyPI wheels), macOS (x86_64, arm64) and Windows (x86_64, arm64). No musl/Alpine build yet.

**PDFium with `cargo install`:** crates.io carries only the source, so download `pdfium-<platform>.tgz` from [bblanchon/pdfium-binaries `chromium/8179`](https://github.com/bblanchon/pdfium-binaries/releases/tag/chromium%2F8179) and put the library (`libpdfium.so`, `libpdfium.dylib` or `pdfium.dll`) next to the `pdfsift` executable, or point `PDFSIFT_PDFIUM` at it. If it is missing, pdfsift says where it looked.

## Usage

```sh
pdfsift file.pdf                  # Markdown on stdout
pdfsift file.pdf -o file.md       # to a file
pdfsift file.pdf --pages 10-12    # a slice ("3", "2-4", "1,5-7"), to fit a context window
pdfsift file.pdf --no-page-markers
```

- `<!-- page: N -->` marks where each page starts, so an agent can cite pages. A header comment names the source file and the pages converted.
- Warnings (for example, a page without a text layer) go to stderr; `-q` silences them.
- Exit codes: 0 success (maybe with warnings), 1 error, 2 invalid usage.
- Output is deterministic: the same PDF always gives the same Markdown.

## What it does

- **Reading order**: columns are read one after the other; a paragraph that continues in the next column or page is joined back.
- **Hyphenation**: words split at line ends are rejoined (`interopera-` / `bility` → `interoperability`), while real hyphens stay.
- **Page furniture**: running headers, footers and page numbers are dropped.
- **Headings**: from the PDF outline, the structure tags, or the typography (size and weight), with the right level.
- **Lists**: bullets, numbers and letters, nested.
- **Tables**: a table whose grid is certain becomes a GFM pipe table, with column alignment; merged cells or an uncertain grid give a fenced code block with the text laid out by position, so nothing is invented. On the test corpus (five producers): every cell right in the simple tables, except one table split across a page break, and no prose mistaken for a table.

## Compared with `pdftotext`

The two-column test document above (15 paragraphs), typeset from the same source by Chrome, LibreOffice, LaTeX and Typst; `pdftotext` is Xpdf 4.00. "Whole paragraphs" counts source paragraphs that come out as one intact paragraph; "split words" counts line-end hyphens left in the text.

| Producer | `pdftotext` | `pdftotext -layout` | `pdfsift` |
|---|---|---|---|
| Chrome | 9/15 whole, 2 split words | 0/15, 2 | **15/15, 0** |
| LibreOffice | 6/15, 3 | 0/15, 56 | **15/15, 0** |
| LaTeX | 0/15, 29 | 0/15, 35 | **13/15, 0** |
| Typst | 0/15, 1 (+ soft hyphens left as spaces) | 0/15, 23 | **15/15, 0** |

Beyond the counts:
- On LaTeX, plain `pdftotext` interleaves the two columns line by line; `-layout` puts them side by side on the same line. pdfsift reads column 1, then column 2.
- `pdftotext` keeps Chrome's header and footer (date, title, `file:///…`, `1/3`) on every page; pdfsift removes them.
- `pdftotext` has no headings (they are plain lines) and drops Chrome's list markers, gluing items together. pdfsift emits `#`/`##`/`###` at the right level and nested lists.
- Size is similar: on a 6-page arXiv paper, 40.2k characters for pdfsift and 40.8k for `pdftotext` (53.1k with `-layout`). Markdown syntax costs about what removing the furniture saves.
- Tables: `pdftotext` flattens them into lines; `-layout` keeps the alignment but, in two of the test tables (LibreOffice, and a Word-made arXiv paper), shifts values into the wrong row. pdfsift gets both right as GFM tables.

Methodology and per-document results (in Portuguese): [text](docs/achados/011-primeira-saida-markdown.md), [tables](docs/achados/013-tabelas-minimas.md).

## Limitations

- **Tables** with merged cells come out as a laid-out code block, not a grid; a table split across pages comes out in two parts.
- **Images** are not exported yet, and there is no OCR: scanned pages without a text layer are reported on stderr.
- No emphasis, links or footnote handling yet; footnotes appear as paragraphs at the end of the page.
- Encrypted PDFs are not supported.
- The output format may still change before 1.0.

## Safety

- No network access, ever. pdfsift reads the input PDF and writes only to stdout or the `-o` path.
- Parsing is done by PDFium, the engine Chrome uses on untrusted web content; pdfsift's own code is written to skip or reject malformed data rather than crash (fuzzing is planned).
- Release binaries come with SHA-256 checksums and GitHub build attestations (`gh attestation verify <file> --repo vitorsilverio/pdfsift`).

## Development

```sh
scripts/fetch-pdfium.sh            # downloads PDFium into .pdfium/ (needs gh)
cargo run -- file.pdf              # convert
cargo run -- dump file.pdf         # inspect what the PDF engine sees
cargo run -- blocks file.pdf       # inspect the layout blocks
scripts/gen-corpus.sh              # regenerate test PDFs (pandoc, lualatex, typst, LibreOffice, Chrome)
```

PDFium is loaded at run time from `PDFSIFT_PDFIUM`, next to the executable (or in `pdfium/` beside it), from `.pdfium/<platform>/` (debug builds only), or from the system library path (not on Windows). The design docs are in [`docs/`](docs/) (Portuguese): [vision](docs/visao.md), [usage](docs/uso.md), [architecture](docs/arquitetura.md), [distribution](docs/distribuicao.md), [roadmap](docs/roadmap.md), [decisions](docs/decisoes/).

## License

Licensed under either of [Apache License 2.0](LICENSE-APACHE) or [MIT license](LICENSE-MIT), at your option. The binaries also contain PDFium and its dependencies under their own permissive licenses; see [THIRD_PARTY_LICENSES](THIRD_PARTY_LICENSES).

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.