tonhowtf/omniget · error

não abri o PDF

Error message

não abri o PDF: {}

What it means

load() is the shared document-opening helper for all pdf_write write operations. It fails when lopdf's Document::load or Document::load_mem_with_options returns an Err (file missing, unreadable bytes, or — when a password was supplied — decryption failure). The anyhow! wrapper just prefixes the underlying lopdf error with 'não abri o PDF:'.

Solutions

  1. Verify the input path exists and points to a real PDF before calling (fs::metadata + check %PDF- header)
  2. If the PDF is encrypted, pass the correct password via the current_password option
  3. Try opening the file with another viewer ( Acrobat/pdftotext) to confirm it is not corrupted
  4. Re-export or repair the PDF if the xref is broken (qpdf --check / qpdf --decode)

Example fix

// before
let doc = load(input, &opts.current_password)?;
// after
if !std::path::Path::new(input).exists() {
    anyhow::bail!("arquivo não encontrado: {}", input);
}
let doc = load(input, &opts.current_password)
    .map_err(|e| anyhow!("verifique o caminho e a senha do PDF: {}", e))?;
Defensive patterns

Strategy: validation

Validate before calling

let path = std::path::Path::new(input);
if !path.is_file() { anyhow::bail!("PDF não encontrado: {}", input); }
let head = std::fs::read(input)?;
if !head.starts_with(b"%PDF-") { anyhow::bail!("não é um PDF válido"); }

Type guard

fn is_loadable_pdf(input: &str) -> bool {
    std::fs::File::open(input).map(|mut f| {
        let mut b = [0u8; 5];
        std::io::Read::read_exact(&mut f, &mut b).is_ok() && &b == b"%PDF-"
    }).unwrap_or(false)
}

Try / catch

match load(input, password) {
    Ok(doc) => doc,
    Err(e) => { eprintln!("falha ao abrir PDF: {e}"); return Err(e); }
}

Prevention

When it happens

Trigger: Calling password_one, watermark_one, crop_one, read_outline or write_outline with an input path that does not exist, is not a valid PDF (bad header/xref), or whose password is wrong/missing for an encrypted file.

Common situations: Typo'd or relative path resolved from the wrong cwd; user passes a non-PDF file (e.g. .docx renamed); wrong current_password on an AES-encrypted PDF; PDF uses an encryption revision lopdf cannot decrypt.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


AI-assisted analysis of tonhowtf/omniget@8600b91f42 (2026-09-12). Data as JSON: /api/errors/6475f4fd26b75395. Report an issue: GitHub.

Appendix: source

Thrown at src-tauri/omniget-core/src/core/tools/pdf_write.rs:116

        p |= Permissions::COPYABLE;
    }
    if opts.allow_modify {
        p |= Permissions::MODIFIABLE | Permissions::ASSEMBLABLE;
    }
    if opts.allow_annotate {
        p |= Permissions::ANNOTABLE | Permissions::FILLABLE;
    }
    p
}

fn load(input: &str, password: &str) -> anyhow::Result<Document> {
    let doc = if password.is_empty() {
        Document::load(input)
    } else {
        let bytes = std::fs::read(input)?;
        Document::load_mem_with_options(&bytes, lopdf::LoadOptions::with_password(password))
    };
    doc.map_err(|e| anyhow!("não abri o PDF: {}", e))
}

fn password_one(opts: &PasswordOptions, input: &str) -> anyhow::Result<WriteItem> {
    let mut doc = load(input, &opts.current_password)?;
    let pages = doc.get_pages().len();
    let note;
    if opts.mode == "remove" {
        if !doc.is_encrypted() && opts.current_password.is_empty() {
            return Err(anyhow!("esse PDF não tem senha"));
        }
        // `load_mem_with_password` já devolve o documento decifrado; basta
        // apagar o dicionário de cifra para o arquivo sair aberto.
        doc.encryption_state = None;
        doc.trailer.remove(b"Encrypt");
        note = "senha removida".to_string();
    } else {
        if opts.user_password.is_empty() && opts.owner_password.is_empty() {
            return Err(anyhow!("defina pelo menos uma senha"));

View on GitHub (pinned to 8600b91f42)