tonhowtf/omniget · error
não abri o PDF
Error message
não abri o PDF: {} What it means
This error wraps any failure from the lopdf Document::load / load_mem_with_options call when opening the input PDF in pdf_form.rs, producing a single user-facing message 'não abri o PDF: {inner}'. It is raised by the private load helper, which chooses memory-load with a password when one is given, or plain load otherwise.
Solutions
- Verify the input path exists and is a readable regular file before calling read_fields/fill
- If the PDF is encrypted, pass the correct password so load_mem_with_options is used; empty password forces an unencrypted load attempt
- Open the PDF in a viewer to confirm it is valid and not corrupt; re-obtain the file if damaged
- Inspect the inner error in the message ('não abri o PDF: ...') to distinguish io::Error (path) vs lopdf parse/decrypt errors and fix accordingly
Example fix
// before
read_fields("/tmp/missing.pdf", "")?; // não abri o PDF: No such file or directory
// after
let path = Path::new(input);
anyhow::ensure!(path.is_file(), "arquivo não encontrado: {}", input);
read_fields(input, &password)?; Defensive patterns
Strategy: validation
Validate before calling
let path = Path::new(input);
anyhow::ensure!(path.is_file(), "not a file: {}", input);
// optionally probe encryption first:
// if pdf_is_encrypted(input) && password.is_empty() { prompt for password } Type guard
fn readable_pdf(path: &str) -> bool {
std::fs::File::open(path).map(|mut f| {
let mut m = [0u8; 5];
std::io::Read::read_exact(&mut f, &mut m).is_ok() && &m == b"%PDF-"
}).unwrap_or(false)
} Try / catch
match read_fields(input, password) {
Err(e) if e.to_string().starts_with("não abri o PDF") => {
eprintln!("could not open PDF (check path/encryption): {e}");
}
Ok(fields) => { /* use fields */ }
Err(e) => return Err(e),
} Prevention
- Check the path exists and is a regular file before calling
- Supply the password for encrypted PDFs; empty password only works on unencrypted files
- Validate the file starts with %PDF- to catch corrupt/misnamed files
- Keep paths trimmed and as-is from the file picker to avoid whitespace/encoding surprises
When it happens
Trigger: Calling read_fields or fill with a path that does not exist, is a directory, or is unreadable; the file is not a valid PDF (corrupt download, wrong extension); the file is encrypted and either no password was supplied (empty password → plain Document::load fails) or the supplied password is wrong; unsupported PDF features cause lopdf to reject the file.
Common situations: User picks a moved/renamed/deleted file; encrypted PDF from a bank/government portal with a password the app wasn't given; passwords with encoding mismatches (e.g. non-ASCII handled differently by the source app); file truncated mid-upload; path containing whitespace not trimmed.
Understand the failure class
Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.
Related errors
AI-assisted analysis of tonhowtf/omniget@8600b91f42 (2026-09-12).
Data as JSON: /api/errors/d3b8c5b798f30086.
Report an issue: GitHub.
Appendix: source
Thrown at src-tauri/omniget-core/src/core/tools/pdf_form.rs:308
kind,
value,
options,
page,
rect,
readonly: ff & FF_READONLY != 0,
required: ff & FF_REQUIRED != 0,
multiline: ff & FF_MULTILINE != 0,
});
}
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))
}
/// Lista os campos do formulário.
pub fn read_fields(input: &str, password: &str) -> anyhow::Result<Vec<Field>> {
let doc = load(input, password)?;
Ok(fields_of(&doc))
}
fn fields_of(doc: &Document) -> Vec<Field> {
let Some(form) = acroform(doc) else {
return Vec::new();
};
let pages = page_index(doc);
let roots: Vec<ObjectId> = form
.get(b"Fields")
.and_then(Object::as_array)
.map(|a| a.iter().filter_map(as_ref).collect())
.unwrap_or_default();View on GitHub (pinned to 8600b91f42)