Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Images with alpha channel not rendered #98

Closed
robinkrahl opened this issue Jun 15, 2021 · 1 comment
Closed

Images with alpha channel not rendered #98

robinkrahl opened this issue Jun 15, 2021 · 1 comment

Comments

@robinkrahl
Copy link
Contributor

Images with an alpha channel are not rendered by printpdf. See for example this modification of the images.rs example:

extern crate printpdf;

use printpdf::*;
use image::{GenericImage, GenericImageView};
use std::fs::File;
use std::io::BufWriter;

fn main() {
    let (doc, page1, layer1) = PdfDocument::new("printpdf graphics test", Mm(20.0), Mm(20.0), "Layer 1");

    let (mut page, mut layer) = (page1, layer1);
    let image_constructors: &[(&str, fn(u32, u32) -> image::DynamicImage)] = &[
        ("bgr8", image::DynamicImage::new_bgr8),
        ("bgra8", image::DynamicImage::new_bgra8),
        ("luma16", image::DynamicImage::new_luma16),
        ("luma8", image::DynamicImage::new_luma8),
        ("luma_a16", image::DynamicImage::new_luma_a16),
        ("luma_a8", image::DynamicImage::new_luma_a8),
        ("rgb16", image::DynamicImage::new_rgb16),
        ("rgb8", image::DynamicImage::new_rgb8),
        ("rgba16", image::DynamicImage::new_rgba16),
        ("rgba8", image::DynamicImage::new_rgba8),
    ];
    let red = image::Rgba([255, 0, 0, 255]);
    let font = doc.add_builtin_font(BuiltinFont::Helvetica).unwrap();

    for (i, (label, image_constructor)) in image_constructors.into_iter().enumerate() {
        if i > 0 {
            let (new_page, new_layer) = doc.add_page(Mm(20.0), Mm(20.0), "Layer 1");
            page = new_page;
            layer = new_layer;
        }

        let mut image = image_constructor(100, 100);
        for x in 0..image.width() {
            for y in 0..image.height() {
                image.put_pixel(x, y, red);
            }
        }

        let image = Image::from_dynamic_image(&image);

        let current_layer = doc.get_page(page).get_layer(layer);
        current_layer.use_text(*label, 12.0, Mm(0.0), Mm(10.0), &font);
        image.add_to_layer(current_layer, None, None, None, None, None, None);
    }

    doc.save(&mut BufWriter::new(File::create("test_image.pdf").unwrap())).unwrap();
}

Only the images without an alpha channel are present in the generated PDF file:
test_image.pdf

(This was initially reported against genpdf-rs).

@robinkrahl
Copy link
Contributor Author

Sorry, I just realized that this is a duplicate of #84.

@fschutt fschutt pinned this issue Jun 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant