fix(cli): accept flags before the PDF path and embed CMaps

Merged
AB
1mo ago
+335-91

Fixes #569

Summary

cargo install binaries failed the reported detect-pdf --json document.pdf command with IO error: No such file or directory (os error 2) because the first argument was always treated as the PDF path. --json was opened as a file, so every real path failed the same way. pdf2md --json document.pdf and pdf2md --password secret document.pdf had the same bug.

  • pdf2md and detect-pdf now take the PDF path as the first non-option argument. Flags may come before or after it. -- ends option parsing.
  • A missing input names the path (and, for a relative path, the working directory), so a genuinely absent file is distinguishable from a flag parsed as a path.
  • Built-in CMaps are compiled into the binary. They were loaded at runtime from CARGO_MANIFEST_DIR/external/bcmaps, which is the crates.io checkout for cargo install and does not exist inside a published wheel. PDF_INSPECTOR_BCMAPS_DIR still overrides the embedded copies.

pdf2md document.pdf already opened that path. If the file is not in the working directory, the error now says so explicitly.

Test plan

  • cargo fmt
  • cargo clippy -- -D warnings
  • cargo test (1537 lib tests, CLI tests, integration tests)
  • detect-pdf --json tests/fixtures/thermo-freon12.pdf returns text_based
  • pdf2md --raw tests/fixtures/thermo-freon12.pdf prints markdown
  • pdf2md document.pdf reports document.pdf (working directory …): No such file or directory when that file is absent
Open in Web Open in Cursor 

Summary by cubic

Fixes detect-pdf --json file.pdf and pdf2md --json file.pdf failing with an IO error for every file, and embeds the built-in CMaps so cargo install binaries work without the crate source on disk.

  • The PDF path is now the first non-option argument, so flags may come before or after it; -- ends option parsing.
  • A missing input error now names the path (plus the working directory for relative paths), so a genuinely absent file is distinguishable from a flag parsed as a path.
  • Built-in CMaps are compiled into the binary instead of loaded at runtime; PDF_INSPECTOR_BCMAPS_DIR still overrides them.