[pdf] Redaction: rebuild redacted pages as one image, scrub hidden data, prune; reader and AES-256 protect/unlock
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
//! 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 edit;
|
||||
mod engine;
|
||||
mod error;
|
||||
pub mod geometry;
|
||||
mod pages;
|
||||
mod reader;
|
||||
mod redact;
|
||||
mod security;
|
||||
|
||||
pub use edit::*;
|
||||
pub use engine::Engine;
|
||||
pub use error::PdfError;
|
||||
pub use geometry::{ViewRect, ViewPoint};
|
||||
pub use pages::*;
|
||||
pub use reader::*;
|
||||
pub use redact::{RedactOptions, Redacted, Redaction, redact};
|
||||
pub use security::{is_encrypted, protect, unlock};
|
||||
|
||||
/// 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::default();
|
||||
opts.max_decompressed_size = Some(256 << 20);
|
||||
if let Some(p) = password {
|
||||
opts.password = Some(p.to_owned());
|
||||
}
|
||||
match lopdf::Document::load_mem_with_options(bytes, opts) {
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user