print warnings, some minor fixes (still doesn't fully work)

This commit is contained in:
Breval Ferrari 2025-04-05 19:43:22 -04:00
parent 7d5c860d05
commit df2c3747b7
No known key found for this signature in database
GPG key ID: F71E304D6400AB8E

View file

@ -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}; use crate::{Bendable, IntoDataBytes, TryFromDataBytes};
fn clean_up_warnings(warnings: &mut Vec<PdfWarnMsg>) {
warnings.retain(|warning| {
warning.severity == PdfParseErrorSeverity::Warning
|| warning.severity == PdfParseErrorSeverity::Error
});
}
impl TryFromDataBytes for PdfDocument { impl TryFromDataBytes for PdfDocument {
type Error = String; type Error = String;
type Format = (); type Format = ();
@ -14,27 +23,37 @@ impl TryFromDataBytes for PdfDocument {
where where
Self: Sized, Self: Sized,
{ {
PdfDocument::parse( let mut warnings = Vec::new();
let result = PdfDocument::parse(
&bytes, &bytes,
&PdfParseOptions { &PdfParseOptions {
fail_on_error: false, 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 { impl IntoDataBytes for PdfDocument {
fn into_data_bytes(self) -> crate::Bytes { fn into_data_bytes(self) -> crate::Bytes {
self.save( let mut warnings = Vec::new();
let result = self.save(
&PdfSaveOptions { &PdfSaveOptions {
optimize: false,
subset_fonts: false,
secure: false,
image_optimization: None, 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] #[test]
fn map_integrity() { fn map_integrity() {
let mut warnings = Vec::new();
let original = PdfDocument::parse( let original = PdfDocument::parse(
include_bytes!("../../../testing material/doc/pdf/basic-text.pdf"), include_bytes!("../../../testing material/doc/pdf/basic-text.pdf"),
&PdfParseOptions { &PdfParseOptions {
fail_on_error: true, fail_on_error: true,
}, },
&mut Vec::new(), &mut warnings,
) )
.unwrap(); .unwrap();
clean_up_warnings(&mut warnings);
for warning in warnings {
println!("Warning: {:#?}", warning);
}
assert_eq!( assert_eq!(
format!("{:?}", original), format!("{:?}", original),
format!("{:?}", original.clone().map(|u| u.clone())) format!("{:?}", original.clone().map(|u| u.clone()))