diff --git a/src/utils/pdf_utils.rs b/src/utils/pdf_utils.rs index 9b46d46..0d980dd 100644 --- a/src/utils/pdf_utils.rs +++ b/src/utils/pdf_utils.rs @@ -2,17 +2,22 @@ //! //! @author Pierre Hubert +use pdf::backend::Backend; + use crate::data::error::{ExecError, ResultBoxError}; /// Check out whether a PDF is a valid PDF or not, by trying to open it pub fn is_valid_pdf(file: &bytes::Bytes) -> ResultBoxError { - let pdf = pdf::file::File::new(Vec::from(file.as_ref())); + let backend = file.to_vec(); - match pdf.get_num_pages() { - Ok(num) if num < 1 => - Err(ExecError::boxed_string(format!("Detected a PDF with {} pages!", num))), - - Ok(_) => Ok(true), + match backend.read_xref_table_and_trailer() { + Ok((refs, _)) => { + if refs.is_empty() { + Err(ExecError::boxed_string(format!("Detected a PDF with 0 references (file size: {})!", file.len()))) + } else { + Ok(true) + } + } Err(e) => { println!("Error while parsing PDF: {}", e);