anydoc in Rust - cargo Document to Markdown
API ReferenceRust
The Rust crate is simply anydoc — the native form of the whole library. Every other binding (Node/Python/WASM) is compiled from this code, so in Rust you get the ceiling of performance.
Install
cargo add anydocThe three core APIs
use anydoc::ConvertError;
fn main() -> Result<(), ConvertError> {
// 1. From a path
let markdown = anydoc::to_markdown("report.docx")?;
println!("{markdown}");
// 2. From bytes
let bytes = std::fs::read("slides.pptx")?;
let md = anydoc::to_markdown_bytes(&bytes, None)?;
// 3. Stop at the document model (keep embedded assets)
let document = anydoc::to_document(&bytes, None)?;
// document keeps raw bytes and media types of images/objects
Ok(())
}Error handling
match anydoc::to_markdown("scanned.pdf") {
Ok(md) => println!("{md}"),
Err(ConvertError::Unsupported) => println!("Unsupported format or scanned PDF"),
Err(ConvertError::Malformed) => println!("Corrupt file"),
Err(e) => println!("Other error: {e:?}"),
}The ConvertError enum has six variants: Unsupported, Malformed, Encrypted, ResourceLimit, MissingPart, Io — details in Error Handling & Limits.
Writing embedded assets to disk
let document = anydoc::to_document(&bytes, None)?;
// Walk the document's asset nodes (sketch)
for asset in document.assets() {
// asset.bytes: raw bytes
// asset.media_type: e.g. "image/png"
std::fs::write(format!("assets/{}", asset.name), &asset.bytes)?;
}In the Markdown output these assets render as alt text; the raw bytes live only on the document model.
Common scenarios
A tiny CLI tool
let args: Vec<String> = std::env::args().collect();
let md = anydoc::to_markdown(&args[1])?;
print!("{md}"); // pipeline-friendly, redirect with your shellThe Rust code behind WebAssembly
Compiling this crate to wasm produces the browser build @firecrawl/anydoc-wasm. Want to customize the wasm behavior (e.g. asset policy)? Compile your own from source.
Tips
- The second argument of
to_markdown_bytes/to_documentis the options slot — passNonefor defaults - The same code compiles to both native and wasm: Rust is the "write once, ship everywhere" source
- For server-side concurrency, pair with
rayonortokio::spawn_blockingas you like