{"record":{"id":"609298bfa9bf96e9","repo":"flxzt/rnote","slug":"creating-pdf-instance-failed-err-err","errorCode":null,"errorMessage":"Creating Pdf instance failed, Err: {err:?}","messagePattern":"Creating Pdf instance failed, Err: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/rnote-engine/src/strokes/bitmapimage.rs","lineNumber":141,"sourceCode":"            affine: transform,\n        };\n\n        Ok(Self { image, rectangle })\n    }\n\n    pub fn from_pdf_bytes(\n        to_be_read: &[u8],\n        pdf_import_prefs: PdfImportPrefs,\n        insert_pos: Vector2,\n        page_range: Option<Range<usize>>,\n        format: &Format,\n        password: Option<String>,\n    ) -> Result<Vec<Self>, anyhow::Error> {\n        // TODO: how to avoid this allocation without lifetime issues?\n        let data = Arc::new(to_be_read.to_vec());\n        let pdf = if let Some(password) = password {\n            hayro_syntax::Pdf::new_with_password(data, &password)\n                .map_err(|err| anyhow!(\"Creating Pdf instance failed, Err: {err:?}\"))?\n        } else {\n            hayro_syntax::Pdf::new(data)\n                .map_err(|err| anyhow!(\"Creating Pdf instance failed, Err: {err:?}\"))?\n        };\n        let interpreter_settings = hayro_interpret::InterpreterSettings::default();\n        let pages = pdf.pages();\n        let page_range = page_range.unwrap_or(0..pages.len());\n        let page_width = if pdf_import_prefs.adjust_document {\n            format.width()\n        } else {\n            format.width() * (pdf_import_prefs.page_width_perc / 100.0)\n        };\n\n        // calculate the page zoom based on the width of the first page.\n        let page_zoom = if let Some(first_page) = pages.first() {\n            page_width / first_page.render_dimensions().0 as f64\n        } else {\n            return Ok(vec![]);","sourceCodeStart":123,"sourceCodeEnd":159,"githubUrl":"https://github.com/flxzt/rnote/blob/bbc5354502ba2fc83eec2670b535348825e679a6/crates/rnote-engine/src/strokes/bitmapimage.rs#L123-L159","documentation":"from_pdf_bytes could not construct a hayro_syntax::Pdf from the supplied bytes when a password was given: Pdf::new_with_password rejected the data, meaning the bytes are not a valid PDF or the password is wrong/unsupported. The library throws this before any page rendering can start.","triggerScenarios":"Calling the public `from_pdf_bytes` with `Some(password)` where the byte buffer is not a parseable PDF, is corrupted/truncated, or the provided password fails to decrypt the document.","commonSituations":"Importing an encrypted PDF into rnote with an incorrect password; importing a partially downloaded or otherwise corrupted PDF file; passing non-PDF bytes.","solutions":["Re-enter the correct password and retry the import.","Verify the file is a complete, valid PDF (open it in another viewer); re-download or re-export it if corrupted.","Try importing without a password if the document is not actually encrypted.","Check the wrapped hayro error `{err:?}` for whether it is a parse error vs. a decryption failure."],"exampleFix":"// before\nlet pdf = hayro_syntax::Pdf::new_with_password(data, &password).map_err(|err| anyhow!(\"Creating Pdf instance failed, Err: {err:?}\"))?;\n// after\nlet pdf = hayro_syntax::Pdf::new_with_password(data, &password)\n    .map_err(|err| anyhow!(\"Creating Pdf instance failed (wrong password or corrupt file?), Err: {err:?}\"))\n    .context(\"check the password and that the PDF is not corrupted\")?;","handlingStrategy":"validation","validationCode":"// Rust: verify the file starts with the PDF magic and is complete before import\nfn looks_like_pdf(bytes: &[u8]) -> bool {\n    bytes.starts_with(b\"%PDF-\") && bytes.windows(5).any(|w| w == b\"%%EOF\")\n}","typeGuard":"fn is_nonempty_pdf_input(bytes: &[u8]) -> bool {\n    !bytes.is_empty() && bytes.starts_with(b\"%PDF-\")\n}","tryCatchPattern":"match BitmapImage::from_pdf_bytes(&bytes, &prefs, &format, Some(password), range) {\n    Ok(pages) => ..., \n    Err(e) => prompt_user(\"Import failed: check the password and that the PDF is not corrupted.\"),\n}","preventionTips":["Prompt users for the password up front for encrypted PDFs instead of guessing.","Check the %PDF- magic and %%EOF trailer before passing bytes to the importer.","Avoid truncating large PDFs during file reads or transfers."],"tags":["rust","pdf","import","parsing"],"backgroundTag":"pdf-parse-failed","analyzedSha":"bbc5354502ba2fc83eec2670b535348825e679a6","analyzedAt":"2026-09-08T13:20:33.747Z","contentChangedAt":"2026-09-08T13:20:33.747Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}