{"record":{"id":"985192e9954296d6","repo":"tonhowtf/omniget","slug":"pagina-fora-do-documento","errorCode":null,"errorMessage":"pagina {} fora do documento","messagePattern":"pagina (.+?) fora do documento","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src-tauri/omniget-core/src/core/tools/pdf.rs","lineNumber":1761,"sourceCode":"        });\n    }\n    Ok(out)\n}\n\n/// Renderiza uma página avulsa em JPEG, para quem precisa trocar o conteúdo\n/// da página por pixel (a tarja usa isso quando não dá para editar o texto).\npub fn page_jpeg(\n    input: &str,\n    password: Option<&str>,\n    page: usize,\n    dpi: u32,\n    quality: u8,\n) -> anyhow::Result<(Vec<u8>, f32, f32)> {\n    let api = api()?;\n    let _g = OPS.lock().unwrap_or_else(|p| p.into_inner());\n    let doc = Document::open(api, Path::new(input.trim()), password)?;\n    if page == 0 || page > doc.pages() {\n        return Err(anyhow!(\"pagina {} fora do documento\", page));\n    }\n    let p = doc.page(page - 1)?;\n    let (w, h) = p.size_pt();\n    let img = p.render(dpi.clamp(48, 400))?;\n    Ok((encode(&img, \"jpg\", quality)?, w, h))\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n\n    #[test]\n    fn ranges() {\n        assert_eq!(parse_ranges(\"\", 3).unwrap(), vec![1, 2, 3]);\n        assert_eq!(parse_ranges(\"1-2, 5\", 5).unwrap(), vec![1, 2, 5]);\n        assert_eq!(parse_ranges(\"4-\", 5).unwrap(), vec![4, 5]);\n        assert_eq!(parse_ranges(\"3-1\", 5).unwrap(), vec![3, 2, 1]);\n        assert!(parse_ranges(\"9\", 5).is_err());","sourceCodeStart":1743,"sourceCodeEnd":1779,"githubUrl":"https://github.com/tonhowtf/omniget/blob/8600b91f4246848bac346874daa9e61c1fc5677a/src-tauri/omniget-core/src/core/tools/pdf.rs#L1743-L1779","documentation":"This error is thrown by render_pdf_page when the requested 1-based page number is zero or exceeds the document's total page count. The library validates `page` against `doc.pages()` right after opening the PDF and refuses to render a page that cannot exist. It is a precondition check to prevent panics or confusing low-level errors from the PDF renderer.","triggerScenarios":"Calling the render function with page=0 (pages are 1-based, so 0 is always invalid), or with a page number greater than doc.pages() — e.g. rendering page 5 of a 3-page PDF, or a page number coming from user input or a loop bound computed from another document.","commonSituations":"Off-by-one bugs mixing 0-based and 1-based indexing; UI lets the user type/scroll to a page number without clamping; a different PDF was loaded than the one metadata (page count) was read from; empty/corrupt PDF reports 0 pages so any page request fails.","solutions":["Check the page number is between 1 and the document's page count before calling the render function","Clamp or normalize 0-based indices from other tools by adding 1 before calling","Validate the input PDF opens and reports pages() > 0 before rendering","If the page comes from user input, clamp to the valid range and inform the user"],"exampleFix":"// before\nrender(input, password, 0, dpi, quality)?; // error: pagina 0 fora do documento\n// after\nlet total = page_count(input, password)?;\nif !(1..=total).contains(&page) { bail!(\"page {} out of 1..={}\", page, total); }\nrender(input, password, page, dpi, quality)?;","handlingStrategy":"validation","validationCode":"let total = page_count(input, password)?; // or track from a prior read\nif page == 0 || page > total {\n    return Err(anyhow!(\"page {} out of range 1..={}\", page, total));\n}","typeGuard":"fn is_valid_page(page: u32, total: u32) -> bool { page >= 1 && page <= total }","tryCatchPattern":"match render_page(input, password, page, dpi, quality) {\n    Err(e) if e.to_string().contains(\"fora do documento\") => {\n        eprintln!(\"invalid page {} — document has {} pages\", page, total);\n    }\n    Ok((bytes, w, h)) => { /* use bytes */ }\n    Err(e) => return Err(e),\n}","preventionTips":["Remember pages are 1-based in this API; convert 0-based indices with +1","Clamp user-supplied page numbers to 1..=total before calling","Re-read page count from the same file you render, never from cached metadata","Guard against empty PDFs (0 pages) before any render call"],"tags":["pdf","argument-validation","out-of-range"],"backgroundTag":"value-out-of-range","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"}