Skip to Content
prasasti

Rust

The prasasti crate, its features, and when to reach for a format crate instead.

let doc = prasasti::Document::open("report.pdf")?;
for text in doc.extract_text() {
    println!("{text}");
}
cargo add prasasti

It needs Rust 1.85 or later and has no C dependencies.

Document

CallReturnsWhat it does
Document::open(path)Result<Document>Open a file and detect its format
Document::open_with_password(path, pw)Result<Document>Same, with a PDF password as &[u8]
Document::from_bytes(bytes)Result<Document>Open from a Vec<u8>
Document::from_bytes_with_password(bytes, pw)Result<Document>Same, with a password
doc.format()FormatFormat::Pdf or Format::Txt, printed as pdf or txt
doc.page_count()usizeNumber of pages
doc.is_encrypted()boolThe file was encrypted and opened with the password
doc.was_recovered()boolThe file was damaged and its structure was rebuilt
doc.metadata()Vec<(String, String)>PDF document info, or the encoding of a text file
doc.extract_text()Vec<String>Every page, in reading order
doc.extract_text_parallel(n)Vec<String>Same on n threads, 0 for every core
doc.page_text(i)Result<String>One page, counting from 0
doc.as_pdf()Option<&prasasti_pdf::Document>The PDF engine's own document, for its full API

Two free functions sit next to it: prasasti::extract_text(path) returns every page joined by a form feed, and prasasti::detect(head) tells you the format of a file from its first bytes without opening it.

Errors

pub enum Error {
    Io(std::io::Error),     // the file can't be read
    Parse(String),          // the file can't be parsed
    Encrypted(String),      // wrong or unsupported password
    PageOutOfRange(usize),  // page_text past the last page
    Unsupported(String),    // unknown format, or its feature is off
}

Error implements std::error::Error, so ? works in a function that returns Box<dyn Error> or an anyhow::Result.

Features

All three are on by default.

FeatureWhat it adds
pdfPDF support, through prasasti-pdf
txtPlain text support, through prasasti-txt
parallelextract_text_parallel on more than one thread. Without it, the call reads pages one by one.

To drop a format you don't need:

[dependencies]
prasasti = { version = "0.1", default-features = false, features = ["pdf"] }

Format crates

Each format is its own crate, re-exported as prasasti::pdf and prasasti::txt. Depend on one directly when you only need that format or want its full API.

CrateWhat it is
prasasti-pdfThe PDF engine: parser, decryption, recovery and reading-order layout
prasasti-txtPlain text decoding with encoding detection

On this page