56 lines
2.4 KiB
Rust
56 lines
2.4 KiB
Rust
//! Pictor's PDF engine: pdfium for everything that reads or draws a page, lopdf for structure that must be written
|
|
//! exactly (annotations, stamps, the redaction rebuild, encryption). Which library does which operation, and why,
|
|
//! is recorded in the app's `docs/01_v1.md` and in [`docs/FORMATS.md`](../../docs/FORMATS.md).
|
|
//!
|
|
//! Coordinates: the UI works in **view space** — PDF points (1/72 inch) from the top-left corner of the page as it
|
|
//! is displayed (crop box, `/Rotate` applied). [`geometry`] converts view space to a page's own user space.
|
|
//!
|
|
//! Every function takes the document as bytes plus an optional password and returns new bytes: nothing here touches
|
|
//! the file system. Outside files are hostile; call these only inside `pictor-worker`.
|
|
|
|
mod compress;
|
|
mod edit;
|
|
mod engine;
|
|
mod error;
|
|
pub mod geometry;
|
|
mod pages;
|
|
mod reader;
|
|
mod redact;
|
|
mod security;
|
|
mod word;
|
|
|
|
pub use compress::{CompressPreset, Compressed, EMAIL_TARGET, compress};
|
|
pub use edit::*;
|
|
pub use engine::Engine;
|
|
pub use error::PdfError;
|
|
pub use geometry::{ViewPoint, ViewRect};
|
|
pub use pages::*;
|
|
pub use reader::*;
|
|
pub use redact::{RedactOptions, Redacted, Redaction, redact};
|
|
pub use security::{is_encrypted, protect, unlock};
|
|
pub use word::{WordOptions, WordResult, to_word};
|
|
|
|
/// lopdf loading with the caps Pictor uses everywhere (a stream may decompress to at most 256 MiB).
|
|
pub(crate) fn lopdf_load(bytes: &[u8], password: Option<&str>) -> Result<lopdf::Document, PdfError> {
|
|
let mut opts = lopdf::LoadOptions { max_decompressed_size: Some(256 << 20), ..Default::default() };
|
|
if let Some(p) = password {
|
|
opts.password = Some(p.to_owned());
|
|
}
|
|
match lopdf::Document::load_mem_with_options(bytes, opts) {
|
|
// Protected only by an owner password (permissions): it opens with the empty user password, as in pdfium.
|
|
Ok(doc) if doc.is_encrypted() && password.is_none() => match lopdf_load(bytes, Some("")) {
|
|
Err(PdfError::WrongPassword) => Err(PdfError::NeedsPassword),
|
|
other => other,
|
|
},
|
|
Ok(doc) if doc.is_encrypted() => Err(PdfError::NeedsPassword),
|
|
Ok(doc) => Ok(doc),
|
|
Err(e) => Err(PdfError::from_lopdf(e, password.is_some())),
|
|
}
|
|
}
|
|
|
|
pub(crate) fn lopdf_save(doc: &mut lopdf::Document) -> Result<Vec<u8>, PdfError> {
|
|
let mut out = Vec::new();
|
|
doc.save_to(&mut out).map_err(|e| PdfError::Write(e.to_string()))?;
|
|
Ok(out)
|
|
}
|