{"record":{"id":"eeace19c8acd051c","repo":"tonhowtf/omniget","slug":"pdf","errorCode":null,"errorMessage":"{}: {}","messagePattern":"\\{\\}: \\{\\}","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src-tauri/omniget-core/src/core/tools/pdf.rs","lineNumber":202,"sourceCode":"}\n\nimpl Document {\n    fn open(api: &'static Api, path: &Path, password: Option<&str>) -> anyhow::Result<Self> {\n        let data = std::fs::read(path).map_err(|e| anyhow!(\"nao leu {}: {}\", path.display(), e))?;\n        let pw = CString::new(password.unwrap_or(\"\")).unwrap_or_default();\n        let doc =\n            unsafe { (api.load_mem)(data.as_ptr() as *const c_void, data.len(), pw.as_ptr()) };\n        if doc.is_null() {\n            let code = unsafe { (api.last_error)() };\n            let why = match code {\n                2 => \"arquivo nao encontrado ou ilegivel\",\n                3 => \"nao e um PDF valido\",\n                4 => \"senha incorreta ou ausente\",\n                5 => \"esquema de seguranca nao suportado\",\n                6 => \"pagina invalida\",\n                _ => \"erro desconhecido\",\n            };\n            return Err(anyhow!(\"{}: {}\", path.display(), why));\n        }\n        Ok(Document {\n            api,\n            doc,\n            _data: data,\n        })\n    }\n\n    fn new(api: &'static Api) -> anyhow::Result<Self> {\n        let doc = unsafe { (api.new_doc)() };\n        if doc.is_null() {\n            return Err(anyhow!(\"nao criou o documento\"));\n        }\n        Ok(Document {\n            api,\n            doc,\n            _data: Vec::new(),\n        })","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/tonhowtf/omniget/blob/8600b91f4246848bac346874daa9e61c1fc5677a/src-tauri/omniget-core/src/core/tools/pdf.rs#L184-L220","documentation":"After FPDF_LoadMemDocument64 returns a null document, Document::open maps FPDF_GetLastError codes to friendly strings (3 = not a valid PDF, 4 = wrong/missing password, 5 = unsupported security scheme, 6 = invalid page) and returns 'path: why'. This means the bytes were read fine but PDFium rejected the document itself — not an I/O problem.","triggerScenarios":"Opening a file that is not a PDF (or corrupt/truncated PDF, code 3); opening an encrypted PDF without a password or with the wrong one (4); a PDF using an unsupported encryption/security handler (5).","commonSituations":"User renames a .docx/.jpg to .pdf; download interrupted leaving a truncated PDF; opening password-protected PDFs without supplying the password; enterprise DRM/encrypted PDFs PDFium cannot handle.","solutions":["Ask the user for the password and retry open with Some(password) when the reason is 'senha incorreta ou ausente'","Verify the file is actually a PDF (check %PDF- header) before calling and reject non-PDFs earlier","Recover/redownload truncated or corrupted PDFs (try a PDF repair step)","For unsupported security schemes, decrypt with another tool or use a PDFium build with more encryption support"],"exampleFix":"// before\nlet doc = pdf::open(&path, None)?; // 'arquivo.pdf: senha incorreta ou ausente'\n// after\nlet doc = match pdf::open(&path, None) {\n    Err(e) if e.to_string().contains(\"senha\") => {\n        let pw = prompt_password()?;\n        pdf::open(&path, Some(&pw))?\n    }\n    r => r?,\n};","handlingStrategy":"try-catch","validationCode":"fn looks_like_pdf(p: &std::path::Path) -> bool {\n    use std::io::Read;\n    std::fs::File::open(p).ok()\n        .and_then(|mut f| {\n            let mut h = [0u8; 5];\n            f.read_exact(&mut h).ok()?;\n            Some(&h == b\"%PDF-\")\n        })\n        .unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":"match pdf::open(&path, password.as_deref()) {\n    Err(e) if e.to_string().contains(\"senha incorreta\") => pdf::open(&path, Some(ask_password()?.as_str())),\n    Err(e) if e.to_string().contains(\"nao e um PDF valido\") => { eprintln!(\"{e}\"); Err(e) }\n    other => other,\n}","preventionTips":["Validate the %PDF- header before attempting to open","Detect encryption upfront and prompt for a password when needed","Verify download integrity (size/checksum) for PDFs fetched over the network","Warn users that DRM/unsupported security schemes cannot be opened by PDFium"],"tags":["rust","pdfium","pdf","invalid-pdf","password-protected"],"backgroundTag":"invalid-argument-value","analyzedSha":"8600b91f4246848bac346874daa9e61c1fc5677a","analyzedAt":"2026-09-12T14:29:19.317Z","contentChangedAt":"2026-09-12T14:29:19.317Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}