From df2c3747b76a2c1977c813e3348d8b5f1d7454bd Mon Sep 17 00:00:00 2001 From: Breval Ferrari Date: Sat, 5 Apr 2025 19:43:22 -0400 Subject: [PATCH] print warnings, some minor fixes (still doesn't fully work) --- bingus/src/doc/pdf.rs | 46 ++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/bingus/src/doc/pdf.rs b/bingus/src/doc/pdf.rs index 4d64099..81de293 100644 --- a/bingus/src/doc/pdf.rs +++ b/bingus/src/doc/pdf.rs @@ -1,7 +1,16 @@ -use printpdf::{Op, PdfDocument, PdfPage, PdfParseOptions, PdfSaveOptions}; +use printpdf::{ + Op, PdfDocument, PdfPage, PdfParseErrorSeverity, PdfParseOptions, PdfSaveOptions, PdfWarnMsg, +}; use crate::{Bendable, IntoDataBytes, TryFromDataBytes}; +fn clean_up_warnings(warnings: &mut Vec) { + warnings.retain(|warning| { + warning.severity == PdfParseErrorSeverity::Warning + || warning.severity == PdfParseErrorSeverity::Error + }); +} + impl TryFromDataBytes for PdfDocument { type Error = String; type Format = (); @@ -14,27 +23,37 @@ impl TryFromDataBytes for PdfDocument { where Self: Sized, { - PdfDocument::parse( + let mut warnings = Vec::new(); + let result = PdfDocument::parse( &bytes, &PdfParseOptions { fail_on_error: false, }, - &mut Vec::new(), - ) + &mut warnings, + ); + clean_up_warnings(&mut warnings); + for warning in warnings { + println!("Warning: {:#?}", warning); + } + result } } impl IntoDataBytes for PdfDocument { fn into_data_bytes(self) -> crate::Bytes { - self.save( + let mut warnings = Vec::new(); + let result = self.save( &PdfSaveOptions { - optimize: false, - subset_fonts: false, - secure: false, image_optimization: None, + ..Default::default() }, - &mut Vec::new(), - ) + &mut warnings, + ); + clean_up_warnings(&mut warnings); + for warning in warnings { + println!("Warning: {:#?}", warning); + } + result } } @@ -74,14 +93,19 @@ mod tests { #[test] fn map_integrity() { + let mut warnings = Vec::new(); let original = PdfDocument::parse( include_bytes!("../../../testing material/doc/pdf/basic-text.pdf"), &PdfParseOptions { fail_on_error: true, }, - &mut Vec::new(), + &mut warnings, ) .unwrap(); + clean_up_warnings(&mut warnings); + for warning in warnings { + println!("Warning: {:#?}", warning); + } assert_eq!( format!("{:?}", original), format!("{:?}", original.clone().map(|u| u.clone()))