PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust
M1: merge upstream main into claude/fix/2670-cli-owner-password
enes yıldız882b5a33bc85
diff --cc apps/pdfcraft-cli/src/main.rsindex 69c4371c0,be2df7735..0a4256564--- a/apps/pdfcraft-cli/src/main.rs+++ b/apps/pdfcraft-cli/src/main.rs@@@ -303,9 -298,9 +303,11 @@@ fn info(args: &[String]) -> Result<(),let path = *positional(args).first().ok_or("info: missing file")?;let bytes = read(path)?;let password = flag(args, "--password");- let mut info = inspect(bytes.clone(), password).map_err(|e| e.to_string())?;+ // The renderer takes only the user password of R2–R4 files; `Session::open` takes either.+ let render_password = pdfcraft_engine::render_password_for(bytes.clone(), password);- let info = inspect(bytes.clone(), render_password.as_deref()).map_err(|e| e.to_string())?;++ let mut info = inspect(bytes.clone(), render_password.as_deref()).map_err(|e| e.to_string())?;+ let pdf_pages = info.pages.len();+ let acroform_fields = info.fields.len();// Dynamic XFA forms: say what laying the template out gives (what the app shows).let xfa_layout = (info.xfa == Some(pdfcraft_render::Xfa::Dynamic)).then(|| {@@@ -355,10 -357,20 +364,23 @@@ fn text(args: &[String]) -> Result<(),}}let password = flag(args, "--password");- let bytes = read(path)?;+ let mut bytes = read(path)?;+ // The renderer takes only the user password of R2–R4 files; `Session::open` takes either.++ // Worked out from the file as read: `bytes` may become the session's display copy below.+ let render_password = pdfcraft_engine::render_password_for(bytes.clone(), password);+ // Printing the text is copying it, so ask what `text_extract` asks (#1645): without the+ // permissions password, a document that doesn't allow copying gives no text.+ let mut session = pdfcraft_engine::Session::new();+ if let Some(doc) = session.open(path, None, bytes.clone(), password).ok().and_then(|id| session.get(id)) {+ // The engine's hint is about editing in the app; on the command line the way in is+ // `--password`.+ doc.check_copying()+ .map_err(|_| "text: the document's permissions don't allow copying its text; pass the owner password with --password to extract it")?;+ if doc.xfa.is_some() {+ bytes = doc.display.clone();+ }+ }- let mut r = PageRenderer::new(bytes.clone(), RenderConfig { password: password.map(Arc::from), ..Default::default() });+ let mut r = PageRenderer::new(bytes.clone(), RenderConfig { password: render_password.as_deref().map(Arc::from), ..Default::default() });// A document the renderer could not open (wrong or missing password, unparseable file) has// no pages, and "extract every page" of nothing would print nothing and exit 0 (#132). Ask// `inspect` why instead, so a protected file fails the same way `info` does.@@@ -598,12 -601,14 +611,18 @@@ fn render(args: &[String]) -> Result<()// must agree with it (and with independent viewers), while keeping the file it reads intact.let bytes = read(path)?;let password = flag(args, "--password");- // The renderer takes only the user password of R2–R4 files; the display copy takes either.- let display = if inspect(bytes.clone(), password).is_ok_and(|info| info.xfa == Some(pdfcraft_render::Xfa::Dynamic)) {++ // The renderer takes only the user password of R2–R4 files; `Session::open` and the display++ // copy take either.+ let render_password = pdfcraft_engine::render_password_for(bytes.clone(), password);- let mut r = PageRenderer::new(- pdfcraft_engine::display_bytes_for(bytes.clone(), password),- RenderConfig { password: render_password.as_deref().map(Arc::from), reject_oversize: true, ..Default::default() },- );++ let display = if inspect(bytes.clone(), render_password.as_deref()).is_ok_and(|info| info.xfa == Some(pdfcraft_render::Xfa::Dynamic)) {+ let mut session = pdfcraft_engine::Session::new();+ let id = session.open(path, None, bytes, password).map_err(|e| e.to_string())?;+ session.get(id).ok_or("render: the document could not be opened")?.display.clone()+ } else {+ pdfcraft_engine::display_bytes_for(bytes, password)+ };- let mut r = PageRenderer::new(display, RenderConfig { password: password.map(Arc::from), reject_oversize: true, ..Default::default() });++ let mut r =++ PageRenderer::new(display, RenderConfig { password: render_password.as_deref().map(Arc::from), reject_oversize: true, ..Default::default() });let p = r.render(RenderRequest { page: page_index, kind: RequestKind::Pixels, tile: None, scale: dpi / 72.0, tag: 0, preview: false });if let Some(e) = p.error {return Err(e.into());