1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
pub mod bookmarks;
pub mod compress;
pub mod create;
pub mod crop;
pub mod decrypt;
pub mod delete;
pub mod encrypt;
pub mod flatten;
pub mod forms;
pub mod html;
pub mod images;
pub mod info;
pub mod markdown;
pub mod merge;
pub mod metadata;
pub mod paths;
pub mod render;
pub mod reorder;
pub mod rotate;
pub mod search;
pub mod split;
pub mod text;
pub mod watermark;
use pdf_oxide::PdfDocument;
use std::path::Path;
/// Open a PDF, optionally authenticating with a password.
pub fn open_doc(path: &Path, password: Option<&str>) -> pdf_oxide::Result<PdfDocument> {
let doc = PdfDocument::open(path)?;
if let Some(pw) = password {
doc.authenticate(pw.as_bytes())?;
}
Ok(doc)
}
/// Get page indices to process: either from --pages flag or all pages.
pub fn resolve_pages(pages_arg: Option<&str>, page_count: usize) -> pdf_oxide::Result<Vec<usize>> {
match pages_arg {
Some(ranges) => {
super::pages::parse_page_ranges(ranges).map_err(pdf_oxide::Error::InvalidOperation)
},
None => Ok((0..page_count).collect()),
}
}
/// Write output to file or stdout.
pub fn write_output(content: &str, output: Option<&Path>) -> pdf_oxide::Result<()> {
use std::io::Write;
match output {
Some(path) => Ok(std::fs::write(path, content)?),
None => {
let stdout = std::io::stdout();
let mut handle = stdout.lock();
handle.write_all(content.as_bytes())?;
// Ensure trailing newline for terminal
if !content.ends_with('\n') {
handle.write_all(b"\n")?;
}
Ok(())
},
}
}