适用于 AI/RAG 的纯 Rust PDF 库: 结构感知分块,不需要 ML,不依赖 C 语言。
A comprehensive PDF toolkit written in pure Rust. Generate, parse, modify, extract and validate PDFs without C bindings. Use one crate for ordinary PDF workflows or continue into structure-aware chunks for AI/RAG applications.
| I need to… | Start here |
|---|---|
| Generate PDFs with text, graphics, images and tables | Document |
| Parse PDFs and extract text | PdfDocument |
| Split, merge, rotate or reorder pages | operations |
| Validate PDF/A or inspect signatures | PDF/A validation and signatures |
| Build an AI/RAG ingestion pipeline | rag_chunks |
[dependencies]
oxidize-pdf = "5.1.2"Structure-aware RAG remains a first-class workflow:
let chunks = PdfDocument::open("paper.pdf")?.rag_chunks()?;
// Each chunk: text, pages, bounding boxes, element types, heading context, token estimateMost PDF libraries give you a wall of text. oxidize-pdf gives you structured, metadata-rich chunks ready for your vector store:
| What you get | Why it matters |
|---|---|
chunk.full_text |
Heading context prepended -- better embeddings |
chunk.page_numbers |
Citation back to source pages |
chunk.bounding_boxes |
Spatial position for visual grounding |
chunk.element_types |
Filter by "table", "title", "paragraph" |
chunk.token_estimate |
Right-size chunks for your model's context window |
chunk.heading_context |
Section awareness without post-processing |
Performance: Pure Rust, 3,000-4,000 pages/sec generation, 85ms full-text extraction for a 930KB PDF.
[dependencies]
oxidize-pdf = "5.1.2"…use oxidize_pdf::pipeline::HybridChunkConfig;
// Smaller chunks for more precise retrieval
let config = HybridChunkConfig {
max_tokens: 256,
..HybridChunkConfig::default()
};
let chunks = doc.rag_chunks_with(config)?;Prepend a deterministic context snippet to each chunk's full_text before
embedding — the no-ML analogue of Contextual Retrieval
/ Late Chunking, which cut retrieval failures substantially by situating each
chunk in its document. No LLM, no GPU: the prefix is a pure function of the
document metadata and the chunk's heading breadcrumb, so it is fully
reproducible (stable chunk_id). The display text field stays context-free.
use oxidize_pdf::pipeline::{ContextFormat, ContextMode, DocumentSource, HybridChunkConfig};
let config = HybridChunkConfig {
context_mode: ContextMode::Contextual(ContextFormat::Labeled),
..HybridChunkConfig::default()
};
let source = DocumentSource::with_file(Some("annual.pdf".into()), Some("sha256-…".into()));
let chunks = doc.rag_chunks_with_source_and_config(source, config)?;
// chunk.full_text now reads, e.g.:
// Document: Annual Report — Acme Corp
// Section: 1 Introduction › 1.2 Scope (p. 3–4)
//
// ContextFormat::Prose renders the same facts as one natural-language sentence.
Modes: ContextMode::None (bare text), Heading (leaf heading only, the
default), Contextual(_) (document + section prefix).
// Serialize all chunks to JSON (requires `semantic` feature)
let json = doc.rag_chunks_json()?;
std::fs::write("chunks.json", json)?;For fine-grained control, access the typed element pipeline directly:
use oxidize_pdf::pipeline::ExtractionProfile;
let doc = PdfDocument::open("document.pdf")?;
// Partition into typed elements
let elements = doc.partition()?;
for el in &elements {
println!("page {} : {}", el.page(), el.text());
}
// Or with a pre-configured profile
let elements = doc.partition_with_profile(ExtractionProfile::Academic)?;
// Build a relationship graph (parent/child sections)
let (elements, graph) = doc.partition_graph(Default::default())?;
for section in graph.top_level_sections() {
println!("Section: {}", elements[section].text());
}Beyond RAG, the same crate also handles PDF parsing (99.3 % success on 9,000+ real-world PDFs, CJK, lenient recovery), generation (3,000–4,000 pages/sec), encryption (RC4-40/128, AES-128, AES-256 R5/R6 — read and write), digital signatures (PKCS#7 verification), PDF/A validation (8 conformance levels), JBIG2 image decoding (pure-Rust ITU-T T.88), invoice extraction (ES/EN/DE/IT), and split/merge/rotate operations. One dependency for the full pipeline.
See oxidize-pdf-core/examples/ for working samples (133 examples) and docs.rs for the API surface.
RagChunk metadata (pages, bboxes, types, headings)ElementGraph for parent/child section relationships| Operation | Speed |
|---|---|
| PDF generation | 3,000-4,000 pages/sec |
| Full text extraction (930KB) | 85 ms |
| Page text extraction | 546 us |
| File loading | 738 us |
Benchmarked with Criterion. Baseline: v2.0.0-profiling.
7,993 tests across unit, integration, and doc tests. 7-tier corpus (T0-T6) with 9,000+ PDFs.
cargo test --workspace # Full test suite
cargo clippy -- -D warnings # Lint check
cargo run --example rag_pipeline -- path/to/file.pdfMIT -- see LICENSE.
暂无开放 Issues,或尚未同步最近议题。