oxidize_pdf/parser/mod.rs
1//! PDF Parser Module - Complete PDF parsing and rendering support
2//!
3//! This module provides a comprehensive, 100% native Rust implementation for parsing PDF files
4//! according to the ISO 32000-1 (PDF 1.7) and ISO 32000-2 (PDF 2.0) specifications.
5//!
6//! # Overview
7//!
8//! The parser is designed to support building PDF renderers, content extractors, and analysis tools.
9//! It provides multiple levels of API access:
10//!
11//! - **High-level**: `PdfDocument` for easy document manipulation
12//! - **Mid-level**: `ParsedPage`, content streams, and resources
13//! - **Low-level**: Direct access to PDF objects and streams
14//!
15//! # Quick Start
16//!
17//! ```rust,no_run
18//! use oxidize_pdf::parser::{PdfDocument, PdfReader};
19//! use oxidize_pdf::parser::content::ContentParser;
20//!
21//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
22//! // Open a PDF document
23//! let reader = PdfReader::open("document.pdf")?;
24//! let document = PdfDocument::new(reader);
25//!
26//! // Get document information
27//! println!("Pages: {}", document.page_count()?);
28//! println!("Version: {}", document.version()?);
29//!
30//! // Process first page
31//! let page = document.get_page(0)?;
32//! println!("Page size: {}x{} points", page.width(), page.height());
33//!
34//! // Parse content streams
35//! let streams = page.content_streams_with_document(&document)?;
36//! for stream in streams {
37//! let operations = ContentParser::parse(&stream)?;
38//! println!("Operations: {}", operations.len());
39//! }
40//!
41//! // Extract text
42//! let text = document.extract_text_from_page(0)?;
43//! println!("Text: {}", text.text);
44//! # Ok(())
45//! # }
46//! ```
47//!
48//! # Architecture
49//!
50//! ```text
51//! ┌─────────────────────────────────────────────────┐
52//! │ PdfDocument │ ← High-level API
53//! │ ┌──────────┐ ┌──────────┐ ┌────────────────┐ │
54//! │ │PdfReader │ │PageTree │ │ResourceManager │ │
55//! │ └──────────┘ └──────────┘ └────────────────┘ │
56//! └─────────────────────────────────────────────────┘
57//! │ │ │
58//! ↓ ↓ ↓
59//! ┌─────────────────────────────────────────────────┐
60//! │ ParsedPage │ ← Page API
61//! │ ┌──────────┐ ┌──────────┐ ┌────────────────┐ │
62//! │ │Properties│ │Resources │ │Content Streams │ │
63//! │ └──────────┘ └──────────┘ └────────────────┘ │
64//! └─────────────────────────────────────────────────┘
65//! │ │ │
66//! ↓ ↓ ↓
67//! ┌─────────────────────────────────────────────────┐
68//! │ ContentParser & PdfObject │ ← Low-level API
69//! │ ┌──────────┐ ┌──────────┐ ┌────────────────┐ │
70//! │ │Tokenizer │ │Operators │ │Object Types │ │
71//! │ └──────────┘ └──────────┘ └────────────────┘ │
72//! └─────────────────────────────────────────────────┘
73//! ```
74//!
75//! # Features
76//!
77//! - **Complete PDF Object Model**: All PDF object types supported
78//! - **Content Stream Parsing**: Full operator support for rendering
79//! - **Resource Management**: Fonts, images, color spaces, patterns
80//! - **Text Extraction**: With position and formatting information
81//! - **Page Navigation**: Efficient page tree traversal
82//! - **Stream Filters**: Decompression support (FlateDecode, ASCIIHex, etc.)
83//! - **Reference Resolution**: Automatic handling of indirect objects
84//!
85//! # Example: Building a Simple Renderer
86//!
87//! ```rust,no_run
88//! use oxidize_pdf::parser::{PdfDocument, PdfReader};
89//! use oxidize_pdf::parser::content::{ContentParser, ContentOperation};
90//!
91//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
92//! struct SimpleRenderer {
93//! current_path: Vec<(f32, f32)>,
94//! }
95//!
96//! impl SimpleRenderer {
97//! fn render_page(document: &PdfDocument<std::fs::File>, page_idx: u32) -> Result<(), Box<dyn std::error::Error>> {
98//! let page = document.get_page(page_idx)?;
99//! let streams = page.content_streams_with_document(&document)?;
100//!
101//! let mut renderer = SimpleRenderer {
102//! current_path: Vec::new(),
103//! };
104//!
105//! for stream in streams {
106//! let operations = ContentParser::parse(&stream)?;
107//! for op in operations {
108//! match op {
109//! ContentOperation::MoveTo(x, y) => {
110//! renderer.current_path.clear();
111//! renderer.current_path.push((x, y));
112//! }
113//! ContentOperation::LineTo(x, y) => {
114//! renderer.current_path.push((x, y));
115//! }
116//! ContentOperation::Stroke => {
117//! println!("Draw path with {} points", renderer.current_path.len());
118//! renderer.current_path.clear();
119//! }
120//! ContentOperation::ShowText(text) => {
121//! println!("Draw text: {:?}", String::from_utf8_lossy(&text));
122//! }
123//! _ => {} // Handle other operations
124//! }
125//! }
126//! }
127//! Ok(())
128//! }
129//! }
130//! # Ok(())
131//! # }
132//! ```
133
134pub mod content;
135pub mod document;
136pub mod encoding;
137pub mod encryption_handler;
138pub mod filter_impls;
139pub mod filters;
140pub mod header;
141pub mod lexer;
142pub mod object_stream;
143pub mod objects;
144pub mod optimized_reader;
145pub mod page_tree;
146pub mod reader;
147pub mod stack_safe;
148pub mod stack_safe_tests;
149pub mod trailer;
150pub mod xref;
151pub mod xref_stream;
152pub mod xref_types;
153
154#[cfg(test)]
155mod stream_length_tests;
156#[cfg(test)]
157pub mod test_helpers;
158
159use crate::error::OxidizePdfError;
160
161// Re-export main types for convenient access
162pub use self::content::{ContentOperation, ContentParser, TextElement};
163pub use self::document::{PdfDocument, ResourceManager};
164pub use self::encoding::{
165 CharacterDecoder, EncodingOptions, EncodingResult, EncodingType, EnhancedDecoder,
166};
167pub use self::encryption_handler::{
168 ConsolePasswordProvider, EncryptionHandler, EncryptionInfo, InteractiveDecryption,
169 PasswordProvider, PasswordResult,
170};
171pub use self::objects::{PdfArray, PdfDictionary, PdfName, PdfObject, PdfStream, PdfString};
172pub use self::optimized_reader::OptimizedPdfReader;
173pub use self::page_tree::ParsedPage;
174pub use self::reader::{DocumentMetadata, PdfReader};
175
176/// Result type for parser operations
177pub type ParseResult<T> = Result<T, ParseError>;
178
179/// Options for parsing PDF files with different levels of strictness
180///
181/// # Example
182///
183/// ```rust
184/// use oxidize_pdf::parser::ParseOptions;
185///
186/// // Create tolerant options for handling corrupted PDFs
187/// let options = ParseOptions::tolerant();
188/// assert!(!options.strict_mode);
189/// assert!(options.recover_from_stream_errors);
190///
191/// // Create custom options
192/// let custom = ParseOptions {
193/// strict_mode: false,
194/// recover_from_stream_errors: true,
195/// ignore_corrupt_streams: false, // Still report errors but try to recover
196/// partial_content_allowed: true,
197/// max_recovery_attempts: 10, // Try harder to recover
198/// log_recovery_details: false, // Quiet recovery
199/// lenient_streams: true,
200/// max_recovery_bytes: 5000,
201/// collect_warnings: true,
202/// lenient_encoding: true,
203/// preferred_encoding: None,
204/// lenient_syntax: true,
205/// };
206/// ```
207#[derive(Debug, Clone)]
208pub struct ParseOptions {
209 /// Strict mode enforces PDF specification compliance (default: true)
210 pub strict_mode: bool,
211 /// Attempt to recover from stream decoding errors (default: false)
212 ///
213 /// When enabled, the parser will try multiple strategies to decode
214 /// corrupted streams, including:
215 /// - Raw deflate without zlib wrapper
216 /// - Decompression with checksum validation disabled
217 /// - Skipping corrupted header bytes
218 pub recover_from_stream_errors: bool,
219 /// Skip corrupted streams instead of failing (default: false)
220 ///
221 /// When enabled, corrupted streams will return empty data instead
222 /// of causing parsing to fail entirely.
223 pub ignore_corrupt_streams: bool,
224 /// Allow partial content when full parsing fails (default: false)
225 pub partial_content_allowed: bool,
226 /// Maximum number of recovery attempts for corrupted data (default: 3)
227 pub max_recovery_attempts: usize,
228 /// Enable detailed logging of recovery attempts (default: false)
229 ///
230 /// Note: Requires the "logging" feature to be enabled
231 pub log_recovery_details: bool,
232 /// Enable lenient parsing for malformed streams with incorrect Length fields
233 pub lenient_streams: bool,
234 /// Maximum number of bytes to search ahead when recovering from stream errors
235 pub max_recovery_bytes: usize,
236 /// Collect warnings instead of failing on recoverable errors
237 pub collect_warnings: bool,
238 /// Enable lenient character encoding (use replacement characters for invalid sequences)
239 pub lenient_encoding: bool,
240 /// Preferred character encoding for text decoding
241 pub preferred_encoding: Option<encoding::EncodingType>,
242 /// Enable automatic syntax error recovery
243 pub lenient_syntax: bool,
244}
245
246impl Default for ParseOptions {
247 fn default() -> Self {
248 Self {
249 strict_mode: true,
250 recover_from_stream_errors: false,
251 ignore_corrupt_streams: false,
252 partial_content_allowed: false,
253 max_recovery_attempts: 3,
254 log_recovery_details: false,
255 lenient_streams: false, // Strict mode by default
256 max_recovery_bytes: 1000, // Search up to 1KB ahead
257 collect_warnings: false, // Don't collect warnings by default
258 lenient_encoding: true, // Enable lenient encoding by default
259 preferred_encoding: None, // Auto-detect encoding
260 lenient_syntax: false, // Strict syntax parsing by default
261 }
262 }
263}
264
265impl ParseOptions {
266 /// Create options for strict parsing (default)
267 pub fn strict() -> Self {
268 Self {
269 strict_mode: true,
270 recover_from_stream_errors: false,
271 ignore_corrupt_streams: false,
272 partial_content_allowed: false,
273 max_recovery_attempts: 0,
274 log_recovery_details: false,
275 lenient_streams: false,
276 max_recovery_bytes: 0,
277 collect_warnings: false,
278 lenient_encoding: false,
279 preferred_encoding: None,
280 lenient_syntax: false,
281 }
282 }
283
284 /// Create options for tolerant parsing that attempts recovery
285 pub fn tolerant() -> Self {
286 Self {
287 strict_mode: false,
288 recover_from_stream_errors: true,
289 ignore_corrupt_streams: false,
290 partial_content_allowed: true,
291 max_recovery_attempts: 5,
292 log_recovery_details: true,
293 lenient_streams: true,
294 max_recovery_bytes: 5000,
295 collect_warnings: true,
296 lenient_encoding: true,
297 preferred_encoding: None,
298 lenient_syntax: true,
299 }
300 }
301
302 /// Create lenient parsing options for maximum compatibility (alias for tolerant)
303 pub fn lenient() -> Self {
304 Self::tolerant()
305 }
306
307 /// Create options that skip corrupted content
308 pub fn skip_errors() -> Self {
309 Self {
310 strict_mode: false,
311 recover_from_stream_errors: true,
312 ignore_corrupt_streams: true,
313 partial_content_allowed: true,
314 max_recovery_attempts: 1,
315 log_recovery_details: false,
316 lenient_streams: true,
317 max_recovery_bytes: 5000,
318 collect_warnings: false,
319 lenient_encoding: true,
320 preferred_encoding: None,
321 lenient_syntax: true,
322 }
323 }
324}
325
326/// Warnings that can be collected during lenient parsing
327#[derive(Debug, Clone)]
328pub enum ParseWarning {
329 /// Stream length mismatch was corrected
330 StreamLengthCorrected {
331 declared_length: usize,
332 actual_length: usize,
333 object_id: Option<(u32, u16)>,
334 },
335 /// Invalid character encoding was recovered
336 InvalidEncoding {
337 position: usize,
338 recovered_text: String,
339 encoding_used: Option<encoding::EncodingType>,
340 replacement_count: usize,
341 },
342 /// Missing required key with fallback used
343 MissingKeyWithFallback { key: String, fallback_value: String },
344 /// Syntax error was recovered
345 SyntaxErrorRecovered {
346 position: usize,
347 expected: String,
348 found: String,
349 recovery_action: String,
350 },
351 /// Invalid object reference was skipped
352 InvalidReferenceSkipped {
353 object_id: (u32, u16),
354 reason: String,
355 },
356}
357
358/// PDF Parser errors covering all failure modes during parsing.
359///
360/// # Error Categories
361///
362/// - **I/O Errors**: File access and reading issues
363/// - **Format Errors**: Invalid PDF structure or syntax
364/// - **Unsupported Features**: Encryption, newer PDF versions
365/// - **Reference Errors**: Invalid or circular object references
366/// - **Stream Errors**: Decompression or filter failures
367///
368/// # Example
369///
370/// ```rust
371/// use oxidize_pdf::parser::{PdfReader, ParseError};
372///
373/// # fn example() -> Result<(), ParseError> {
374/// match PdfReader::open("missing.pdf") {
375/// Ok(_) => println!("File opened"),
376/// Err(ParseError::Io(e)) => println!("IO error: {}", e),
377/// Err(ParseError::InvalidHeader) => println!("Not a valid PDF"),
378/// Err(e) => println!("Other error: {}", e),
379/// }
380/// # Ok(())
381/// # }
382/// ```
383///
384/// # Error Recovery and Tolerant Parsing
385///
386/// The parser supports different levels of error tolerance for handling corrupted or
387/// non-standard PDF files:
388///
389/// ```rust,no_run
390/// use oxidize_pdf::parser::{PdfReader, ParseOptions};
391/// use std::fs::File;
392///
393/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
394/// // Strict parsing (default) - fails on any deviation from PDF spec
395/// let strict_reader = PdfReader::open("document.pdf")?;
396///
397/// // Tolerant parsing - attempts to recover from errors
398/// let file = File::open("corrupted.pdf")?;
399/// let tolerant_reader = PdfReader::new_with_options(file, ParseOptions::tolerant())?;
400///
401/// // Skip errors mode - ignores corrupt streams and returns partial content
402/// let file = File::open("problematic.pdf")?;
403/// let skip_errors_reader = PdfReader::new_with_options(file, ParseOptions::skip_errors())?;
404/// # Ok(())
405/// # }
406/// ```
407#[derive(Debug, thiserror::Error)]
408pub enum ParseError {
409 /// I/O error during file operations
410 #[error("IO error: {0}")]
411 Io(#[from] std::io::Error),
412
413 /// PDF file doesn't start with valid header (%PDF-)
414 #[error("Invalid PDF header")]
415 InvalidHeader,
416
417 /// PDF version is not supported
418 #[error("Unsupported PDF version: {0}")]
419 UnsupportedVersion(String),
420
421 /// Syntax error in PDF structure
422 #[error("Syntax error at position {position}: {message}")]
423 SyntaxError { position: usize, message: String },
424
425 #[error("Unexpected token: expected {expected}, found {found}")]
426 UnexpectedToken { expected: String, found: String },
427
428 /// Invalid or non-existent object reference
429 #[error("Invalid object reference: {0} {1} R")]
430 InvalidReference(u32, u16),
431
432 /// Required dictionary key is missing
433 #[error("Missing required key: {0}")]
434 MissingKey(String),
435
436 #[error("Invalid xref table")]
437 InvalidXRef,
438
439 #[error("Invalid trailer")]
440 InvalidTrailer,
441
442 #[error("Circular reference detected")]
443 CircularReference,
444
445 /// Error decoding/decompressing stream data
446 #[error("Stream decode error: {0}")]
447 StreamDecodeError(String),
448
449 /// PDF encryption is not currently supported
450 #[error("PDF is encrypted. Decryption is not currently supported in the community edition")]
451 EncryptionNotSupported,
452
453 /// Empty file
454 #[error("File is empty (0 bytes)")]
455 EmptyFile,
456
457 /// Stream length mismatch (only in strict mode)
458 #[error(
459 "Stream length mismatch: declared {declared} bytes, but found endstream at {actual} bytes"
460 )]
461 StreamLengthMismatch { declared: usize, actual: usize },
462
463 /// Character encoding error
464 #[error("Character encoding error at position {position}: {message}")]
465 CharacterEncodingError { position: usize, message: String },
466
467 /// Unexpected character in PDF content
468 #[error("Unexpected character: {character}")]
469 UnexpectedCharacter { character: String },
470}
471
472impl From<ParseError> for OxidizePdfError {
473 fn from(err: ParseError) -> Self {
474 OxidizePdfError::ParseError(err.to_string())
475 }
476}
477
478#[cfg(test)]
479mod tests {
480 use super::*;
481
482 #[test]
483 fn test_module_exports() {
484 // Verify that all important types are properly exported
485
486 // Test that we can create a PdfObject
487 let _obj = PdfObject::Null;
488
489 // Test that we can create a PdfDictionary
490 let _dict = PdfDictionary::new();
491
492 // Test that we can create a PdfArray
493 let _array = PdfArray::new();
494
495 // Test that we can create a PdfName
496 let _name = PdfName::new("Test".to_string());
497
498 // Test that we can create a PdfString
499 let _string = PdfString::new(b"Test".to_vec());
500 }
501
502 #[test]
503 fn test_parse_error_conversion() {
504 let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "File not found");
505 let parse_error = ParseError::Io(io_error);
506 let oxidize_error: OxidizePdfError = parse_error.into();
507
508 match oxidize_error {
509 OxidizePdfError::ParseError(_) => assert!(true),
510 _ => assert!(false, "Expected ParseError variant"),
511 }
512 }
513
514 #[test]
515 fn test_parse_error_messages() {
516 let errors = vec![
517 ParseError::InvalidHeader,
518 ParseError::UnsupportedVersion("2.5".to_string()),
519 ParseError::InvalidXRef,
520 ParseError::InvalidTrailer,
521 ParseError::CircularReference,
522 ParseError::EncryptionNotSupported,
523 ];
524
525 for error in errors {
526 let message = error.to_string();
527 assert!(!message.is_empty());
528 }
529 }
530}