Repository: fschutt/printpdf (language: rust)

Issue:
BMP images don't show correctly in production
If I run
```rust
extern crate printpdf;

fn main() {
    println!("Hello, world!");
    use printpdf::*;
    use std::fs::File;
    use std::io::{self, BufRead, BufWriter};

    let (doc, page1, layer1) = PdfDocument::new("printpdf graphics test", Mm(210.0), Mm(297.0), "Layer 1");
    let current_layer = doc.get_page(page1).get_layer(layer1);
    let font = doc.add_external_font(File::open("OCRAStd.ttf").unwrap()).unwrap();
    let mut image_file = File::open("card-retro.bmp").unwrap();
    let image = Image::try_from(image::bmp::BMPDecoder::new(&mut image_file)).unwrap();
    image.add_to_layer(current_layer.clone(), Some(Mm(15.0)), Some(Mm(232.0)), None, None, None, Some(600.0));
    let fill_color = Color::Cmyk(Cmyk::new(0.0, 0.0, 0.0, 0.0, None));
    current_layer.set_fill_color(fill_color);
    let stdin = io::stdin();
    let nome = stdin.lock().lines().next().unwrap().unwrap();
    let lunghezza_nome = nome.len();
    current_layer.use_text(nome, 12, Mm(15.0+(85.6-85.6*(lunghezza_nome as f64)/28.0)/2.0), Mm(240.0), &font);
    current_layer.use_text("00001", 12, Mm(31.5), Mm(248.5), &font);
    current_layer.use_text("124912", 12, Mm(56.5), Mm(248.5), &font);
    // translate x, translate y, rotate, scale x, scale y
    // by default, an image is optimized to 300 DPI (if scale is None)
    // rotations and translations are always in relation to the lower left corner

    let mut image_file_retro = File::open("card-retro.bmp").unwrap();
    let image_retro = Image::try_from(image::bmp::BMPDecoder::new(&mut image_file_retro)).unwrap();

    // translate x, translate y, rotate, scale x, scale y
    // by default, an image is optimized to 300 DPI (if scale is None)
    // rotations and translations are always in relation to the lower left corner
    image_retro.add_to_layer(current_layer.clone(), Some(Mm(100.6)), Some(Mm(232.0)), None, None, None, Some(600.0));
    doc.save(&mut BufWriter::new(File::create("test_working.pdf").unwrap())).unwrap();
}
```
the image looks wrong (macOS Preview app loads it but it looks like it encoded about 10% of the image while Adobe Acrobat X complains that there isn't enough image data)
![image](https://user-images.githubusercontent.com/10284268/51184933-0bb52680-18d6-11e9-899c-f6563ba9ba80.png)
The image above is what is shown in Preview.app
[test_working.pdf](https://github.com/fschutt/printpdf/files/2759898/test_working.pdf)
[card-retro.bmp.zip](https://github.com/fschutt/printpdf/files/2759901/card-retro.bmp.zip)


Discussion / hints:
Ah, I was forgetting: the issue refers to production, in development it works fine, although the file is very big
Yeah, well, I did investigate it last year and it doesn't really have to do much with the PDF, it's just that the encoder simply stops reading bytes from the image at some point. I don't know why that is happening. It's more of an issue with the image crate instead of this library. The decoder simply stops decoding the BMP image half-way through. Originally, this only happened with JPEG images, not BMP. And yes, in debug mode even JPEGs work, but not in release mode.

Try adding the image using raw bytes as you can and maybe use a different version of the `image` library. I can't really fix it here, it is an issue that should be reported to the `image` repository (with the given JPEG file).
The strange thing is that I’m using BMP… I’ll check the image crate and see if it’s fixed in a more recent release.
Thanks,

Eugenio Tampieri

> Il giorno 1 feb 2019, alle ore 20:35, fschutt <notifications@github.com> ha scritto:
> 
> Yeah, well, I did investigate it last year and it doesn't really have to do much with the PDF, it's just that the encoder simply stops reading bytes from the image at some point. I don't know why that is happening. It's more of an issue with the image crate instead of this library. The decoder simply stops decoding the BMP image half-way through. Originally, this only happened with JPEG images, not BMP. And yes, in debug mode even JPEGs work, but not in release mode.
> 
> Try adding the image using raw bytes as you can and maybe use a different version of the image library. I can't really fix it here, it is an issue that should be reported to the image repository (with the given JPEG file).
> 
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub, or mute the thread.

Following this, i found out that upgrading lopdf to version 0.23.0 solves this problem at least with jpg images. I havent tested if it breaks anything else.

Fix patch:
diff --git a/Cargo.toml b/Cargo.toml
index 4be6eff..10831fe 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -9,7 +9,7 @@ readme = "README.md"
 description = "Rust library for writing PDF files"
 
 [dependencies]
-lopdf = { version = "0.17.0", default-features = false }
+lopdf = { version = "0.23.0", default-features = false }
 rusttype = { version = "0.7.2", default-features = false }
 time = { version = "0.1.40", default-features = false }
 log = { version = "0.4.1", optional = true }
@@ -34,4 +34,4 @@ webp = ["image/webp"]
 
 [badges]
 travis-ci = { repository = "fschutt/printpdf" }
-appveyor = { repository = "fschutt/printpdf" }
\ No newline at end of file
+appveyor = { repository = "fschutt/printpdf" }


Test patch: