{"record":{"id":"6475f4fd26b75395","repo":"tonhowtf/omniget","slug":"n-o-abri-o-pdf-write","errorCode":null,"errorMessage":"não abri o PDF: {}","messagePattern":"não abri o PDF: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src-tauri/omniget-core/src/core/tools/pdf_write.rs","lineNumber":116,"sourceCode":"        p |= Permissions::COPYABLE;\n    }\n    if opts.allow_modify {\n        p |= Permissions::MODIFIABLE | Permissions::ASSEMBLABLE;\n    }\n    if opts.allow_annotate {\n        p |= Permissions::ANNOTABLE | Permissions::FILLABLE;\n    }\n    p\n}\n\nfn load(input: &str, password: &str) -> anyhow::Result<Document> {\n    let doc = if password.is_empty() {\n        Document::load(input)\n    } else {\n        let bytes = std::fs::read(input)?;\n        Document::load_mem_with_options(&bytes, lopdf::LoadOptions::with_password(password))\n    };\n    doc.map_err(|e| anyhow!(\"não abri o PDF: {}\", e))\n}\n\nfn password_one(opts: &PasswordOptions, input: &str) -> anyhow::Result<WriteItem> {\n    let mut doc = load(input, &opts.current_password)?;\n    let pages = doc.get_pages().len();\n    let note;\n    if opts.mode == \"remove\" {\n        if !doc.is_encrypted() && opts.current_password.is_empty() {\n            return Err(anyhow!(\"esse PDF não tem senha\"));\n        }\n        // `load_mem_with_password` já devolve o documento decifrado; basta\n        // apagar o dicionário de cifra para o arquivo sair aberto.\n        doc.encryption_state = None;\n        doc.trailer.remove(b\"Encrypt\");\n        note = \"senha removida\".to_string();\n    } else {\n        if opts.user_password.is_empty() && opts.owner_password.is_empty() {\n            return Err(anyhow!(\"defina pelo menos uma senha\"));","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/tonhowtf/omniget/blob/8600b91f4246848bac346874daa9e61c1fc5677a/src-tauri/omniget-core/src/core/tools/pdf_write.rs#L98-L134","documentation":"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:'.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the input path exists and points to a real PDF before calling (fs::metadata + check %PDF- header)","If the PDF is encrypted, pass the correct password via the current_password option","Try opening the file with another viewer ( Acrobat/pdftotext) to confirm it is not corrupted","Re-export or repair the PDF if the xref is broken (qpdf --check / qpdf --decode)"],"exampleFix":"// before\nlet doc = load(input, &opts.current_password)?;\n// after\nif !std::path::Path::new(input).exists() {\n    anyhow::bail!(\"arquivo não encontrado: {}\", input);\n}\nlet doc = load(input, &opts.current_password)\n    .map_err(|e| anyhow!(\"verifique o caminho e a senha do PDF: {}\", e))?;","handlingStrategy":"validation","validationCode":"let path = std::path::Path::new(input);\nif !path.is_file() { anyhow::bail!(\"PDF não encontrado: {}\", input); }\nlet head = std::fs::read(input)?;\nif !head.starts_with(b\"%PDF-\") { anyhow::bail!(\"não é um PDF válido\"); }","typeGuard":"fn is_loadable_pdf(input: &str) -> bool {\n    std::fs::File::open(input).map(|mut f| {\n        let mut b = [0u8; 5];\n        std::io::Read::read_exact(&mut f, &mut b).is_ok() && &b == b\"%PDF-\"\n    }).unwrap_or(false)\n}","tryCatchPattern":"match load(input, password) {\n    Ok(doc) => doc,\n    Err(e) => { eprintln!(\"falha ao abrir PDF: {e}\"); return Err(e); }\n}","preventionTips":["Validate the path and %PDF- header before every write operation","Prompt users for the password when is_encrypted() reports an encrypted file","Use absolute paths to avoid cwd-relative surprises","Pre-flight the file with qpdf --check for corrupt xref tables"],"tags":["pdf","lopdf","file-read","encryption"],"backgroundTag":"file-read-failed","analyzedSha":"8600b91f4246848bac346874daa9e61c1fc5677a","analyzedAt":"2026-09-12T14:29:19.317Z","contentChangedAt":"2026-09-12T14:29:19.317Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}