print warnings, some minor fixes (still doesn't fully work)
This commit is contained in:
parent
7d5c860d05
commit
df2c3747b7
1 changed files with 35 additions and 11 deletions
|
@ -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<PdfWarnMsg>) {
|
||||
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()))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue