pdf_forms/load_options.rs
1use crate::Object;
2
3/// Type alias for the filter function used during PDF loading.
4///
5/// The function receives an object ID and a mutable reference to the object,
6/// and returns `Some((id, object))` to keep it or `None` to discard it.
7pub type FilterFunc = fn((u32, u16), &mut Object) -> Option<((u32, u16), Object)>;
8
9/// Options for loading PDF documents.
10///
11/// Use this struct to configure password, object filtering, and strictness
12/// when loading a PDF. The default is lenient parsing with no password or filter.
13///
14/// # Examples
15///
16/// ```ignore
17/// use lopdf::{Document, LoadOptions};
18///
19/// // Load with a password
20/// let doc = Document::load_with_options(
21/// "encrypted.pdf",
22/// LoadOptions::with_password("secret"),
23/// );
24///
25/// // Load with strict parsing
26/// let doc = Document::load_with_options(
27/// "document.pdf",
28/// LoadOptions { strict: true, ..Default::default() },
29/// );
30/// ```
31#[derive(Clone, Default)]
32pub struct LoadOptions {
33 /// Password for encrypted PDFs.
34 pub password: Option<String>,
35 /// Object filter applied during loading.
36 pub filter: Option<FilterFunc>,
37 /// When `true`, reject non-conforming PDFs instead of silently accepting them.
38 /// Defaults to `false` (lenient parsing).
39 pub strict: bool,
40}
41
42impl std::fmt::Debug for LoadOptions {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 f.debug_struct("LoadOptions")
45 .field("password", &self.password.as_ref().map(|_| "***"))
46 .field("filter", &self.filter.map(|_| "fn(..)"))
47 .field("strict", &self.strict)
48 .finish()
49 }
50}
51
52impl LoadOptions {
53 /// Create options with a password for encrypted PDFs.
54 pub fn with_password(password: &str) -> Self {
55 Self {
56 password: Some(password.to_string()),
57 ..Default::default()
58 }
59 }
60
61 /// Create options with an object filter.
62 pub fn with_filter(filter: FilterFunc) -> Self {
63 Self {
64 filter: Some(filter),
65 ..Default::default()
66 }
67 }
68}