Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Rendering PDFs into a PNG with transparent background #90

Closed
LaurenzV opened this issue Jun 17, 2023 · 3 comments
Closed

Rendering PDFs into a PNG with transparent background #90

LaurenzV opened this issue Jun 17, 2023 · 3 comments
Assignees

Comments

@LaurenzV
Copy link

Hey!

Firstly, thanks for creating this library, it works really well so far!

What I would like to do is to render a PDF into a PNG image, similarly to how it's done in the README, but I would like the background to be transparent. I tried adapting the code to the following:

use pdfium_render::prelude::*;

fn export_pdf_to_pngs(path: &str, password: Option<&str>) -> Result<(), PdfiumError> {

    let pdfium = Pdfium::new(
        Pdfium::bind_to_library(Pdfium::pdfium_platform_library_name_at_path("./"))
            .or_else(|_| Pdfium::bind_to_system_library())?,
    );

    let document = pdfium.load_pdf_from_file(path, password)?;

    let render_config = PdfRenderConfig::new()
        .set_target_width(400)
        .set_maximum_height(400)
        .set_clear_color(PdfColor::new(0, 0, 0, 0))
        .clear_before_rendering(true);

    for (index, page) in document.pages().iter().enumerate() {
        page.render_with_config(&render_config)?
            .as_image()
            .as_rgba8()
            .ok_or(PdfiumError::ImageError)?
            .save_with_format(
                format!("test-page-{}.png", index),
                image::ImageFormat::Png
            ) // ... and saves it to a file.
            .map_err(|_| PdfiumError::ImageError)?;
    }
    Ok(())
}

pub fn main() -> Result<(), PdfiumError> {
    export_pdf_to_pngs("test/clip-path-on-child.pdf", None)
}

And doing this indeed changes to background color from white to black, but it seems like the alpha value is completely ignored. I have the feeling though that I'm doing something wrong and set_clear_color isn't intended to be used in this way. Is there some other way to achieve what I want?

@ajrcarey
Copy link
Owner

Hi @LaurenzV , thank you for reporting the issue. At first I assumed rendering to bitmaps with transparent backgrounds was simply unsupported by Pdfium, but on closer examination it turns out your hunch is completely correct: a small bug in PdfColor resulted in the alpha value being completely ignored when applied as a bitmap background color. I have pushed a small patch that should fix the problem. You can test it by using pdfium-render as a git dependency in your Cargo.toml.

@LaurenzV
Copy link
Author

Works like a charm, thank you very much. :)

@ajrcarey
Copy link
Owner

Took this opportunity to add some convenience functions to PdfColor: mix(), mix_with(), from_hex(), to_hex(), and to_hex_with_alpha(). Added basic unit tests to verify from_hex() and to_hex() conversion functions. Deprecated PdfColor::SOLID_* consts in favour of renamed consts with superfluous SOLID_ prefix removed. Added a variety of new color consts. Updated README.md. Ready to be included in release 0.8.6.

ajrcarey pushed a commit that referenced this issue Jun 19, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants