Skip to main content

pdfluent_lopdf/
save_options.rs

1use crate::ObjectStreamConfig;
2
3/// Options for saving PDF documents
4#[derive(Debug, Clone, Default)]
5pub struct SaveOptions {
6    /// Enable object streams for compressing non-stream objects
7    pub use_object_streams: bool,
8
9    /// Enable cross-reference streams instead of traditional xref tables
10    pub use_xref_streams: bool,
11
12    /// Enable linearization (fast web view)
13    pub linearize: bool,
14
15    /// Configuration for object streams
16    pub object_stream_config: ObjectStreamConfig,
17}
18
19impl SaveOptions {
20    /// Create a builder for SaveOptions
21    pub fn builder() -> SaveOptionsBuilder {
22        SaveOptionsBuilder::default()
23    }
24}
25
26/// Builder for SaveOptions
27#[derive(Default)]
28pub struct SaveOptionsBuilder {
29    use_object_streams: bool,
30    use_xref_streams: bool,
31    linearize: bool,
32    max_objects_per_stream: usize,
33    compression_level: u32,
34}
35
36impl SaveOptionsBuilder {
37    /// Enable or disable object streams
38    pub fn use_object_streams(mut self, value: bool) -> Self {
39        self.use_object_streams = value;
40        self
41    }
42
43    /// Enable or disable cross-reference streams
44    pub fn use_xref_streams(mut self, value: bool) -> Self {
45        self.use_xref_streams = value;
46        self
47    }
48
49    /// Enable or disable linearization
50    pub fn linearize(mut self, value: bool) -> Self {
51        self.linearize = value;
52        self
53    }
54
55    /// Set maximum objects per stream
56    pub fn max_objects_per_stream(mut self, value: usize) -> Self {
57        self.max_objects_per_stream = value;
58        self
59    }
60
61    /// Set compression level (0-9)
62    pub fn compression_level(mut self, value: u32) -> Self {
63        self.compression_level = value;
64        self
65    }
66
67    /// Build the SaveOptions
68    pub fn build(self) -> SaveOptions {
69        SaveOptions {
70            use_object_streams: self.use_object_streams,
71            use_xref_streams: self.use_xref_streams,
72            linearize: self.linearize,
73            object_stream_config: ObjectStreamConfig {
74                max_objects_per_stream: if self.max_objects_per_stream == 0 {
75                    100
76                } else {
77                    self.max_objects_per_stream
78                },
79                compression_level: self.compression_level,
80            },
81        }
82    }
83}