@dudko.dev/pdf-to-md-core
v0.3.6
Published
PDF → Markdown (Rust/WASM parsing core with a typed TypeScript API). Works in browsers, Node and Cloudflare Workers.
Maintainers
Readme
@dudko.dev/pdf-to-md-core
PDF → Markdown, locally. A Rust parser compiled to WebAssembly, wrapped in a typed TypeScript API that loads in a browser, in Node, and in a Cloudflare Worker without any of them needing special configuration.
The bytes never leave the process: this package calls nothing and needs no account, and that is the point of it.
npm install @dudko.dev/pdf-to-md-coreThe hosted service, if you would rather not install anything
pdf2md.dudko.dev is this core running in your browser — drop a PDF, get the Markdown, and the file never leaves the tab. It is the quickest way to see what this package does.
For a pipeline there is an HTTP API at
pdf2md.dudko.dev/api, with the same options as
here plus a ZIP of the Markdown and its pictures, and an MCP endpoint at
https://pdf2md.dudko.dev/mcp for an assistant that should run the conversion
rather than read the document. Both need an account at
auth.dudko.dev, which is free to make and comes with
an allowance the site shows you.
A paid account carries commercial permission for that service. It is not a licence for this package — running the software yourself needs the written licence described below.
Pictures rather than documents:
vectorize.dudko.dev traces a raster image into
SVG, PDF, EPS or DXF the same way, with
@dudko.dev/vectorize-core
as its package.
Use
import { convert } from '@dudko.dev/pdf-to-md-core';
const pdf = new Uint8Array(await (await fetch('/annual-report.pdf')).arrayBuffer());
const result = await convert(pdf);
console.log(result.pdfType); // "TextBased"
console.log(result.markdown); // "# Annual report\n\n…"The module loads itself on the first call. init() is available when you would
rather pay the ~5 MB instantiation before the user asks for anything:
import { init } from '@dudko.dev/pdf-to-md-core';
await init(); // browser: resolves the .wasm next to the glue
await init(myOwnWasmUrlOrBytes); // or point it wherever you keep itOptions
await convert(pdf, {
pages: [1, 3, 5], // 1-indexed
password: 'secret', // encrypted PDFs
profile: 'compact', // or 'fidelity' (default)
includePageMarkers: true, // <!-- Page N --> between pages
stripHeadersFooters: true, // drop running headers/footers
textOnImagePages: true, // default: a caption under a full-page figure is still text
});Every option is listed in ConvertOptions, and an option name this build does
not know is an error rather than a silent default — a misspelled detectCod
should not quietly leave code blocks on.
The rest of the surface
| Function | What it gives you |
| --- | --- |
| convert(bytes, options?) | Markdown plus the full result |
| detect(bytes, {password}?) | type, page count, which pages need OCR — no extraction |
| classify(bytes) | the same, in the parser's native 0-indexed shape |
| extractText(bytes) | plain text in reading order, no Markdown |
| looksLikePdf(bytes) | cheap %PDF- check before you bother the parser |
| parsePages('1,3,5-7') | the pages option from a string, as the CLI and the API read it; throws a RangeError you can show |
| OPTION_TYPES, OPTION_NAMES | every option and the kind of value it takes, for building a flag table or a query parser |
| VERSION, version() | this package's version; bindingsVersion() and coreVersion() for the module and the parser inside it |
Images
The pictures inside a document, as files, and a conversion whose Markdown links to them — from a second entry point, so the main bundle does not carry the pixel work unless you ask for it:
import { convertWithImages } from '@dudko.dev/pdf-to-md-core/images';
const { result, assets, skipped } = await convertWithImages(pdfBytes);
// result.markdown links , , …
for (const { path, image } of assets) await writeFile(path, image.bytes); // next to the .mdextractImages(bytes, { pages?, password?, jpx?, codecs?, vectors? }) gives
the files alone. JPEG streams pass through byte for byte, except a
four-component one — that is CMYK, which no browser renders, so it is decoded
and written again as RGB. Everything else — Flate, LZW, RunLength, ASCIIHex
samples in gray, RGB, CMYK, Lab, indexed, Separation or DeviceN, with or
without a soft mask, and CCITT fax, JBIG2 and JPEG 2000 — becomes a PNG,
encoded with the runtime's own CompressionStream. Whatever the picture was
measured in, the file that comes out is one a browser can show: Lab is
converted to sRGB, CMYK composited to RGB, and a space the file does not name
is read by its component count. Separation and DeviceN are ink, and without
evaluating a tint transform their colour is not recoverable — the amount is,
so a plate becomes gray, and the image says so in its notes. jpx: 'keep' keeps a JPEG 2000 as the
.jp2 it already is. Whatever cannot be turned into a file is in skipped
with the reason, and the Markdown says so where the picture would have been.
convert() itself is unchanged.
Those decoders live in a second WebAssembly module (wasm/pdf_to_md_codecs_bg.wasm,
about 150 KB over the wire) that is loaded the first time a document turns out
to need one; most documents never do. It loads next to the JS glue in a browser
and from disk in Node; a bundler that does not resolve new URL(…, import.meta.url)
inside a dependency gets its URL through codecs (or initCodecs(url)), the
way init() takes the main module's.
Three kinds of picture are found: XObjects drawn by the page content, which
have a placeholder in the Markdown; inline images written into the content
stream; and the pictures inside annotation appearances — stamps, signatures,
form fields. The last two have no placeholder, so convertWithImages() links
them at the end of their page, and each refs[].source says which kind it is.
A fourth kind is not a picture in the file at all. A PDF has no vector image:
a chart, a diagram or a logo is drawing operators in the page content, with
nothing to say where it begins and ends. The module plays the content back,
clusters the painted paths, keeps the clusters that look like a figure rather
than a table's rules or a box around a paragraph — curves, colour, or enough
filled shapes, and not mostly text — and writes each as an SVG
(format: 'svg', source: 'vector'), with the page's text inside its box as
<text> labels a reader can search. A shading — a gradient — becomes a flat
fill of the box it is clipped to, in the middle colour of its range, and a
shading pattern likewise; tiling patterns are a grey stand-in, and clipping of
paths, blend modes and images inside a figure are not carried over.
vectors: false turns it off.
Running it off the main thread
convert is synchronous inside the module: a large document holds the calling
thread for as long as it takes. In a browser, call it from a Web Worker:
// worker.ts
import { convert } from '@dudko.dev/pdf-to-md-core';
self.onmessage = async (e: MessageEvent<Uint8Array>) => {
self.postMessage(await convert(e.data));
};In Node, a worker_threads worker does the same job. Both
@dudko.dev/pdf-to-md-cli and the bundled HTTP API do this.
Bundlers
The .wasm sits next to the JS glue in wasm/, and the default loader
resolves it with import.meta.url — which Vite, Wrangler, webpack 5 and esbuild
all understand. If your bundler does not, import the file yourself and hand the
URL to init():
import wasmUrl from '@dudko.dev/pdf-to-md-core/wasm/pdf_to_md_bg.wasm?url';
await init(wasmUrl);The module is single-threaded on purpose. It needs no cross-origin isolation, no
SharedArrayBuffer and no filesystem — the CJK CMaps are compiled in — which is
what lets the same build run on any page and inside a Worker.
What it does not do
OCR. A scanned document comes back as pdfType: "Scanned" with
pagesNeedingOcr populated and no Markdown. That is the answer, not a failure:
run the file through an OCR tool and convert the result.
Watch hasEncodingIssues too — it means text came out but the fonts declare an
encoding the bytes do not honour, so the characters are probably wrong. Falling
back to OCR is the right move there as well.
Licence
Free for noncommercial use under PolyForm Noncommercial 1.0.0, with a 32-day trial for evaluation at work. Commercial use needs a licence: [email protected].
Your rights to this package come from LICENSE and, for commercial use, from a
written agreement. No web page grants them, so nothing at
pdf2md.dudko.dev or anywhere else widens or narrows
what LICENSE says; a paid account there is permission to use that service.
The notices for the third-party code compiled into the module are in
THIRD-PARTY-NOTICES.md, which ships in the package.
