mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-12 13:54:09 +00:00
## What this changes
**File ▸ Open opens Word, Excel, PowerPoint, OpenDocument and RTF files** as new, unsaved PDFs (`Report.docx` → `Report.pdf`), the "LibreOffice sidecar" that `docs/gaps.md` (gap 8) and `docs/file-format-parity.md` planned for `create.from-office`.
- **Conversion** (`crates/create/src/office.rs`): an installed LibreOffice runs headless as a separate process, in a private temporary folder (the input is always `document.<ext>`, so the user's file name never reaches its command line). It gets a fresh profile with `DisableMacrosExecution`, `MacroSecurityLevel` 3 and `BlockUntrustedRefererLinks` set, and Writer's and Calc's `Content/Update/Link` set to *Never* (2 for Writer and 1 for Calc: the two number their values differently), no console window on Windows (`CREATE_NO_WINDOW`), and is killed after 120 s. The output is size-capped (512 MB), must start `%PDF-`, and must open as a PDF before it is used. LibreOffice is found through `PDFCRAFT_SOFFICE`, the `PATH`, then the usual install folders per OS. Without it, the user is told it's needed rather than "not a readable PDF".
- **Off the frame thread:** File ▸ Open converts on the existing open worker (`ResultKind::Converted`); the frame thread only opens the result.
- **Detection:** `SourceKind::Office` requires an Office extension *and* an OLE, ZIP or RTF signature, so `photos.zip`, or a PDF saved with a `.docx` name, are not misread.
- **Open dialog:** PDF (still the default), Images, Office documents, Text, All files. All files is left out on macOS, where rfd merges every filter into one allowed-types list, and on the web; Office documents are left out on the web. The extension lists are now shared constants in `pdfcraft-create` (`IMAGE_EXTENSIONS`, `TEXT_EXTENSIONS`, `OFFICE_EXTENSIONS`), used by both detection and the picker. The duplicate filter list in `open_dialog` is gone. The two new labels are in all 22 catalogues, with their `ATTRIBUTION.toml` hashes updated.
- **Automation:** `doc_create_multiple`, `doc_combine` and batch `convert` go through `convert_to_pdf`, so they take Office documents too; their descriptions say so.
**Deliberately not in this PR:** Insert pages, Combine files and Create ▸ Multiple files convert on the frame thread, so they now refuse Office documents with a pointer to File ▸ Open (`convert_to_pdf_inline` / `as_pdf_inline`) rather than freezing the window for seconds. Moving them onto a worker is the follow-up. `create.from-office` goes planned → **partial**, with that and the rest listed in its notes.
## How you verified it
On Windows 11 Pro with LibreOffice 26.8.1 installed, per crate:
- [x] `cargo fmt --check`
- [x] `cargo clippy -p pdfcraft-create -p pdfcraft-engine -p pdfcraft-ui-egui -p pdfcraft-automation -j 2 --all-targets -- -D warnings`
- [x] `cargo test -p pdfcraft-create -p pdfcraft-engine --lib` (42 and 276 pass); `-p pdfcraft-automation --test automation` (the combine/create tests); `-p pdfcraft-ui-egui --lib open_worker` and `--test editing --test create`
- [x] `cargo xtask layers`, `cargo xtask assets`, `cargo xtask parity --partial`
- [ ] wasm check: the `wasm32-unknown-unknown` target isn't installed here, so I'm leaving it to CI. The web paths are `cfg`'d and were reviewed by eye.
**Tests seen failing before the fix**, by reverting each part:
- Detection off → `office_documents_are_their_own_kind`, `source_kind_tells_pdfs_images_and_text_apart`, and both engine Office tests fail.
- Worker conversion off → `file_open_converts_an_office_document_on_the_worker` fails with the old user-facing message, *"the file is not a readable PDF: no readable page tree or cross-reference data was found"*.
New tests also cover a converter that hangs (it's stopped; the test binary stands in for LibreOffice), one that writes no PDF, a missing converter, temp-folder cleanup, `file://` URL escaping (spaces, drive letters, UTF-8), and the search order.
**Real conversions, end to end:** `crates/automation/tests/it/office.rs` builds a minimal DOCX, an XLSX (with a formula, `=6*7`), a PPTX and an RTF inside the test, so no binary fixtures are committed. It runs them through `doc_create_multiple` in `separate` mode (each output reopened and its text checked, including `42`) and in `combine` mode (page order and bookmarks), and through `doc_combine` beside a PDF, with a page range on one file. These tests, `libreoffice_converts_a_document_when_installed` and the process-tree cleanup test (5 out of 5 reruns) pass with LibreOffice 26.8.1. The Office tests skip where LibreOffice isn't installed, which includes CI.
Three existing tests used a 4-byte `report.docx` as their "file that can't be converted" example. That's no longer true, so they now use `archive.zip`, which keeps what they test.
## Never-crash
- [x] No new `unwrap`/`expect`/`panic!`/`unreachable!`/`todo!` outside tests
- [x] No input-derived indexing or slicing (`rsplit_once` for the extension); the PDF read back is size-capped
- [x] No `unsafe`. The Windows flag uses `std::os::windows::process::CommandExt`
- [x] A crashing, hanging or misbehaving LibreOffice is an error, never a panic or a stuck app: the worker's `catch_unwind`, a 120 s kill, and output checks
## Provenance
- [x] No Adobe assets; no fonts; no copied code
- [x] LibreOffice (MPL-2.0) is only *run* as an external process, as the docs planned; nothing of it is linked or copied. Its command-line options and the profile keys come from LibreOffice's public documentation.
- [x] The translations are contributor-original, written from the plain meaning of "Office documents" and "All files" ("Office" kept as a product name); hashes updated, `cargo xtask assets` passes
## Security
- [x] `--root` confinement unchanged: automation still resolves and reads paths before converting; only the private temp folder is written outside it
- [x] MCP and the control channel untouched
- [ ] **No new outbound network: please decide.** PdfCraft itself opens no connection. The profile blocks untrusted referer links and sets Writer and Calc never to update linked content on load. I converted eight synthetic files that link to a listener on 127.0.0.1: RTF `INCLUDETEXT` and `INCLUDEPICTURE`, a DOCX external picture, an ODT linked section and linked picture, an ODS table source and an external cell reference, and an ODP linked picture. None made a connection, with or without the link settings. Two things remain open: Impress has no link-update setting, and the process's network access isn't sandboxed (documented in `crates/create/README.md` and `docs/gaps.md`).
## Parity and reach
- [x] `create.from-office` planned → partial, with the evidence tests and what's still missing
- [x] Reachable headlessly (`doc_create_multiple`, `doc_combine`, batch `convert`)
- [x] Docs: `docs/file-format-parity.md` (Word/Excel/PowerPoint and RTF rows), `docs/gaps.md` gap 8, `docs/target-app-parity.md` row J (numbers not re-measured), `docs/roadmap.md` M10, the `ROADMAP.md` log, and the `crates/create` README
## Anything a reviewer should look at first
1. **The network question** above. It's the owner's call.
2. **The frame-thread restraint:** Insert, Combine and Create Multiple refuse Office documents rather than block. Is a worker for those wanted in this PR, or as a follow-up?
102 lines
5.3 KiB
Rust
102 lines
5.3 KiB
Rust
//! Other files to combine with or copy pages from: opening them, and why one can't be used.
|
|
|
|
use std::sync::Arc;
|
|
|
|
use crate::{EditError, SourceProblem};
|
|
|
|
/// A file to combine: its name (the bookmark title), bytes, and page range (`None`: all).
|
|
pub type CombineSource = (String, Arc<Vec<u8>>, Option<String>);
|
|
|
|
/// What a combine produced: the new PDF bytes, and the fields renamed to keep colliding names
|
|
/// separate (old fully-qualified name → new one), in order across the sources.
|
|
#[derive(Debug)]
|
|
pub struct Combined {
|
|
pub bytes: Arc<Vec<u8>>,
|
|
pub renamed_fields: Vec<(String, String)>,
|
|
}
|
|
|
|
/// Convert the Office document `bytes` named `name` (Word, Excel, PowerPoint, OpenDocument,
|
|
/// RTF) to PDF with an installed LibreOffice, and check the result opens. It runs a separate
|
|
/// process for seconds, so callers keep it off the frame thread. Desktop only: on the web it
|
|
/// returns an error.
|
|
pub fn office_to_pdf(name: &str, bytes: &Arc<Vec<u8>>) -> Result<Arc<Vec<u8>>, EditError> {
|
|
let pdf = Arc::new(pdfcraft_create::office_to_pdf(name, bytes)?);
|
|
open_source(name, &pdf)?;
|
|
Ok(pdf)
|
|
}
|
|
|
|
/// Parse another PDF to copy pages from.
|
|
pub(crate) fn open_source(name: &str, bytes: &Arc<Vec<u8>>) -> Result<pdfcraft_cos::Document, EditError> {
|
|
open_source_with(name, bytes, None)
|
|
}
|
|
|
|
/// [`open_source`], authenticating with `password` (user or owner) for an encrypted file.
|
|
pub(crate) fn open_source_with(name: &str, bytes: &Arc<Vec<u8>>, password: Option<&str>) -> Result<pdfcraft_cos::Document, EditError> {
|
|
source_document(bytes, password).map_err(|p| EditError::Source(format!("{name}: {p}")))
|
|
}
|
|
|
|
/// [`open_source`] for pages inserted into `dest`. A source that withholds page extraction but
|
|
/// allows assembly is accepted when `dest` will be saved under the very same protection (file
|
|
/// key and `/Encrypt` values), as when pages cut or copied from a document are pasted back into
|
|
/// it: they never leave its protection (#5081).
|
|
pub(crate) fn open_source_into(name: &str, bytes: &Arc<Vec<u8>>, dest: &pdfcraft_cos::Document) -> Result<pdfcraft_cos::Document, EditError> {
|
|
let same_protection = |src: &pdfcraft_cos::Document| match (src.security(), dest.output_handler()) {
|
|
(Some(s), Some(d)) => s.file_key() == d.file_key() && s.dict() == d.dict(),
|
|
_ => false,
|
|
};
|
|
let permitted = |src: &pdfcraft_cos::Document| src.permissions().is_none_or(|p| p.extract_pages() || (p.assemble() && same_protection(src)));
|
|
source_document_if(bytes, None, permitted).map_err(|p| EditError::Source(format!("{name}: {p}")))
|
|
}
|
|
|
|
/// Import the comments of `src` onto the pages of `doc` with the same numbers.
|
|
pub(crate) fn import_pdf_comments(doc: &mut pdfcraft_cos::Document, src: &pdfcraft_cos::Document) -> Result<(), EditError> {
|
|
if pdfcraft_organize::import_comments(doc, src)? == 0 {
|
|
return Err(pdfcraft_xfdf::DataError::NothingImported.into());
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
/// Parse another PDF to import comments from, whatever its permissions allow.
|
|
pub(crate) fn comment_source(bytes: &Arc<Vec<u8>>) -> Result<pdfcraft_cos::Document, SourceProblem> {
|
|
match std::panic::catch_unwind(|| pdfcraft_cos::Document::open(bytes.clone())) {
|
|
Ok(Ok(d)) => Ok(d),
|
|
Ok(Err(pdfcraft_cos::CosError::NeedsPassword)) => Err(SourceProblem::Password),
|
|
Ok(Err(e)) => Err(SourceProblem::Unreadable(e.to_string())),
|
|
Err(_) => Err(SourceProblem::Unreadable("the file could not be read".into())),
|
|
}
|
|
}
|
|
|
|
fn source_document(bytes: &Arc<Vec<u8>>, password: Option<&str>) -> Result<pdfcraft_cos::Document, SourceProblem> {
|
|
source_document_if(bytes, password, |d| d.permissions().is_none_or(|p| p.extract_pages()))
|
|
}
|
|
|
|
/// Open `bytes` as a page source, refused as [`SourceProblem::NotPermitted`] unless `permitted`.
|
|
fn source_document_if(
|
|
bytes: &Arc<Vec<u8>>,
|
|
password: Option<&str>,
|
|
permitted: impl Fn(&pdfcraft_cos::Document) -> bool,
|
|
) -> Result<pdfcraft_cos::Document, SourceProblem> {
|
|
match std::panic::catch_unwind(|| pdfcraft_cos::Document::open_with_password(bytes.clone(), password)) {
|
|
Ok(Ok(d)) if !permitted(&d) => Err(SourceProblem::NotPermitted),
|
|
Ok(Ok(d)) => Ok(d),
|
|
Ok(Err(pdfcraft_cos::CosError::NeedsPassword)) => Err(SourceProblem::Password),
|
|
Ok(Err(pdfcraft_cos::CosError::WrongPassword)) => Err(SourceProblem::WrongPassword),
|
|
Ok(Err(e)) => Err(SourceProblem::Unreadable(e.to_string())),
|
|
Err(_) => Err(SourceProblem::Unreadable("the file could not be read".into())),
|
|
}
|
|
}
|
|
|
|
/// Whether `bytes` can be combined, opened with `password` (user or owner) if given, and if not
|
|
/// why: what Combine would refuse it for, so the Combine files list can say so before Combine
|
|
/// is pressed. The owner (permissions) password lifts the restriction on copying pages.
|
|
pub fn combine_source_check(bytes: &Arc<Vec<u8>>, password: Option<&str>) -> Result<(), SourceProblem> {
|
|
source_document(bytes, password).map(|_| ())
|
|
}
|
|
|
|
/// The number of pages of `bytes`, opened with `password` (user or owner) if given, whatever
|
|
/// its permissions allow; `None` when it can't be read.
|
|
pub fn source_page_count(bytes: &Arc<Vec<u8>>, password: Option<&str>) -> Option<usize> {
|
|
let doc = std::panic::catch_unwind(|| pdfcraft_cos::Document::open_with_password(bytes.clone(), password)).ok()?.ok()?;
|
|
pdfcraft_organize::page_count(&doc).ok()
|
|
}
|