Skip to content

Commit

Permalink
use Reader trait for providing a better performance (ARK-Builders#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
j4w3ny authored and tareknaser committed Apr 9, 2024
1 parent 43f8fa5 commit c9e10b1
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/pdf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{env, path::PathBuf};
use std::{
env,
io::{Read, Seek},
path::PathBuf,
};

use image::DynamicImage;

Expand Down Expand Up @@ -28,7 +32,10 @@ fn initialize_pdfium() -> Box<dyn PdfiumLibraryBindings> {
}
}
}
pub fn render_preview_page(data: &[u8], quailty: PDFQuality) -> DynamicImage {
pub fn render_preview_page<R>(data: R, quailty: PDFQuality) -> DynamicImage
where
R: Read + Seek + 'static,
{
let render_cfg = PdfBitmapConfig::new();
let render_cfg = match quailty {
PDFQuality::High => render_cfg.set_target_width(2000),
Expand All @@ -37,7 +44,7 @@ pub fn render_preview_page(data: &[u8], quailty: PDFQuality) -> DynamicImage {
}
.rotate_if_landscape(PdfBitmapRotation::Degrees90, true);
Pdfium::new(initialize_pdfium())
.load_pdf_from_bytes(data, None)
.load_pdf_from_reader(data, None)
.unwrap()
.pages()
.get(0)
Expand All @@ -54,14 +61,11 @@ fn test_multi_pdf_generate() {
let tmp_path = dir.path();
println!("temp path: {}", tmp_path.display());
for i in 0..2 {
use std::{fs::File, io::Read};
let mut pdf_reader = File::open("tests/test.pdf").unwrap();

let mut bytes = Vec::new();
pdf_reader.read_to_end(&mut bytes).unwrap();
use std::fs::File;
let pdf_reader = File::open("tests/test.pdf").unwrap();

println!("Rendering {}", &i);
let img = render_preview_page(bytes.as_slice(), PDFQuality::High);
let img = render_preview_page(pdf_reader, PDFQuality::High);

img.save(tmp_path.join(format!("test{}.png", &i)))
.expect("cannot save image");
Expand Down

0 comments on commit c9e10b1

Please sign in to comment.