[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,46 @@
|
||||
use crate::PdfError;
|
||||
use pdfium_render::prelude::*;
|
||||
use std::path::Path;
|
||||
use std::sync::OnceLock;
|
||||
|
||||
/// The one pdfium instance of the process (pdfium is not thread-safe; pdfium-render's `thread_safe` feature
|
||||
/// serialises calls). Load it once with [`Engine::load`].
|
||||
pub struct Engine {
|
||||
pub(crate) pdfium: Pdfium,
|
||||
}
|
||||
|
||||
static ENGINE: OnceLock<Engine> = OnceLock::new();
|
||||
static LOAD_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
|
||||
|
||||
impl Engine {
|
||||
/// Loads pdfium.dll from `dir` (or from the system search path when `None`). Later calls return the same engine.
|
||||
pub fn load(dir: Option<&Path>) -> Result<&'static Engine, PdfError> {
|
||||
if let Some(e) = ENGINE.get() {
|
||||
return Ok(e);
|
||||
}
|
||||
let _guard = LOAD_LOCK.lock().unwrap_or_else(|p| p.into_inner());
|
||||
if let Some(e) = ENGINE.get() {
|
||||
return Ok(e);
|
||||
}
|
||||
let bindings = match dir {
|
||||
Some(d) => Pdfium::bind_to_library(Pdfium::pdfium_platform_library_name_at_path(d)),
|
||||
None => Pdfium::bind_to_system_library(),
|
||||
}
|
||||
.map_err(|e| PdfError::EngineMissing(e.to_string()))?;
|
||||
let _ = ENGINE.set(Engine { pdfium: Pdfium::new(bindings) });
|
||||
Ok(ENGINE.get().expect("just set"))
|
||||
}
|
||||
|
||||
pub(crate) fn open<'a>(&'a self, bytes: &'a [u8], password: Option<&str>) -> Result<PdfDocument<'a>, PdfError> {
|
||||
self.pdfium.load_pdf_from_byte_slice(bytes, password).map_err(|e| PdfError::from_pdfium_open(e, password.is_some()))
|
||||
}
|
||||
|
||||
pub(crate) fn new_document(&self) -> Result<PdfDocument<'_>, PdfError> {
|
||||
self.pdfium.create_new_pdf().map_err(PdfError::from_pdfium)
|
||||
}
|
||||
}
|
||||
|
||||
/// pdfium's save, with the security handler removed (Pictor re-encrypts with lopdf when asked).
|
||||
pub(crate) fn pdfium_save(doc: &PdfDocument) -> Result<Vec<u8>, PdfError> {
|
||||
doc.save_to_bytes().map_err(|e| PdfError::Write(e.to_string()))
|
||||
}
|
||||
Reference in New Issue
Block a user