tonhowtf/omniget · error

pagina fora do documento

Error message

pagina {} fora do documento

What it means

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.

Solutions

  1. Check the page number is between 1 and the document's page count before calling the render function
  2. Clamp or normalize 0-based indices from other tools by adding 1 before calling
  3. Validate the input PDF opens and reports pages() > 0 before rendering
  4. If the page comes from user input, clamp to the valid range and inform the user

Example fix

// before
render(input, password, 0, dpi, quality)?; // error: pagina 0 fora do documento
// after
let total = page_count(input, password)?;
if !(1..=total).contains(&page) { bail!("page {} out of 1..={}", page, total); }
render(input, password, page, dpi, quality)?;
Defensive patterns

Strategy: validation

Validate before calling

let total = page_count(input, password)?; // or track from a prior read
if page == 0 || page > total {
    return Err(anyhow!("page {} out of range 1..={}", page, total));
}

Type guard

fn is_valid_page(page: u32, total: u32) -> bool { page >= 1 && page <= total }

Try / catch

match render_page(input, password, page, dpi, quality) {
    Err(e) if e.to_string().contains("fora do documento") => {
        eprintln!("invalid page {} — document has {} pages", page, total);
    }
    Ok((bytes, w, h)) => { /* use bytes */ }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at src-tauri/omniget-core/src/core/tools/pdf.rs:1761

        });
    }
    Ok(out)
}

/// Renderiza uma página avulsa em JPEG, para quem precisa trocar o conteúdo
/// da página por pixel (a tarja usa isso quando não dá para editar o texto).
pub fn page_jpeg(
    input: &str,
    password: Option<&str>,
    page: usize,
    dpi: u32,
    quality: u8,
) -> anyhow::Result<(Vec<u8>, f32, f32)> {
    let api = api()?;
    let _g = OPS.lock().unwrap_or_else(|p| p.into_inner());
    let doc = Document::open(api, Path::new(input.trim()), password)?;
    if page == 0 || page > doc.pages() {
        return Err(anyhow!("pagina {} fora do documento", page));
    }
    let p = doc.page(page - 1)?;
    let (w, h) = p.size_pt();
    let img = p.render(dpi.clamp(48, 400))?;
    Ok((encode(&img, "jpg", quality)?, w, h))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ranges() {
        assert_eq!(parse_ranges("", 3).unwrap(), vec![1, 2, 3]);
        assert_eq!(parse_ranges("1-2, 5", 5).unwrap(), vec![1, 2, 5]);
        assert_eq!(parse_ranges("4-", 5).unwrap(), vec![4, 5]);
        assert_eq!(parse_ranges("3-1", 5).unwrap(), vec![3, 2, 1]);
        assert!(parse_ranges("9", 5).is_err());

View on GitHub (pinned to 8600b91f42)