fschutt / printpdf

A fully-featured PDF library for Rust, WASM-ready
https://fschutt.github.io/printpdf/
MIT License
829 stars 98 forks source link

add_to_layer problem #192

Closed bothorsen closed 2 weeks ago

bothorsen commented 3 weeks ago

I'm trying to add a logo file to my pdf, but can't seem to make it work. As soon as I add the add_to_layer() call, the generated PDF is just empty. This is the code I'm trying:

let (pdf, page1, layer1) = printpdf::PdfDocument::new(&self.title, printpdf::Mm(page_width), printpdf::Mm(page_height), "Page 1");
let layer = pdf.get_page(page1).get_layer(layer1);
let fonts = Fonts::new(&pdf)?;

let image_bytes = include_bytes!("../../../web-assets/logo-160x100-white-area.png");
let mut reader = Cursor::new(image_bytes.as_ref());
let decoder = PngDecoder::new(&mut reader).unwrap();
let image = Image::try_from(decoder).unwrap();
image.add_to_layer(layer.clone(), ImageTransform{ translate_x: Some(printpdf::Mm(30.0)), translate_y: Some(printpdf::Mm(40.0)), ..Default::default()});
layer.use_text(...
...
Ok(pdf.save_to_bytes()?)

If I comment out the add_to_layer call, then I get my PDF with all the text added. But when it's not commented out, I just get a white PDF.

I'm sorry to report this as a bug if it's a problem in my code. But it's quite hard to find examples of how to add images to the pdf online.

fschutt commented 2 weeks ago

Can you give me the PDF / image? I'm currently rewriting the entire library on the version2 branch, so it's kind of useless to fix this bug now in the old version.

add_to_layer only adds a /Do {image_id} command to the PDF layer stream, where the image ID is registered on the pages /Resource dictionary. It would be best to solve problems now.

fschutt commented 2 weeks ago

Duplicate of https://github.com/fschutt/printpdf/issues/119

bothorsen commented 2 weeks ago

Sorry for the late response. The problem is indeed that the image was semi transparent. I changed my image to one that doesn't have any transparency, and then it works.

Sorry for the duplicate.