Skip to content

Commit

Permalink
Fix pdf thumbs having incorrect colors (#1826)
Browse files Browse the repository at this point in the history
Fix BGR being interpreted as RGB when generation pdf thumbs
 - Disabled some pdf render configs for better performance
  • Loading branch information
HeavenVolkoff authored Nov 25, 2023
1 parent d0f318b commit 2f01b21
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions crates/images/src/pdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use crate::{
};
use image::DynamicImage;
use once_cell::sync::Lazy;
use pdfium_render::prelude::{PdfRenderConfig, Pdfium};
use pdfium_render::{
color::PdfColor,
prelude::{PdfPageRenderRotation, PdfRenderConfig, Pdfium},
};
use tracing::error;

// This path must be relative to the running binary
Expand Down Expand Up @@ -52,18 +55,26 @@ static PDFIUM_LIB: Lazy<String> = Lazy::new(|| {
})
});

static PORTRAIT_CONFIG: Lazy<PdfRenderConfig> = Lazy::new(|| {
PdfRenderConfig::new()
.set_target_width(PDF_PORTRAIT_RENDER_WIDTH)
.render_form_data(false)
fn thumbnail_config(config: PdfRenderConfig) -> PdfRenderConfig {
// From: https://github.com/ajrcarey/pdfium-render/blob/82c10b2d59b04a8413acd31892eb28822e60e06a/src/render_config.rs#L159
config
.rotate(PdfPageRenderRotation::None, false)
.use_print_quality(false)
.set_image_smoothing(false)
.render_annotations(false)
.render_form_data(false)
// Required due to: https://github.com/ajrcarey/pdfium-render/issues/119
.set_reverse_byte_order(false)
.set_clear_color(PdfColor::new(0, 0, 0, 0))
.clear_before_rendering(true)
}

static PORTRAIT_CONFIG: Lazy<PdfRenderConfig> = Lazy::new(|| {
thumbnail_config(PdfRenderConfig::new().set_target_width(PDF_PORTRAIT_RENDER_WIDTH))
});

static LANDSCAPE_CONFIG: Lazy<PdfRenderConfig> = Lazy::new(|| {
PdfRenderConfig::new()
.set_target_width(PDF_LANDSCAPE_RENDER_WIDTH)
.render_form_data(false)
.render_annotations(false)
thumbnail_config(PdfRenderConfig::new().set_target_width(PDF_LANDSCAPE_RENDER_WIDTH))
});

pub struct PdfHandler {}
Expand Down

0 comments on commit 2f01b21

Please sign in to comment.