1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#![doc(html_root_url = "https://docs.rs/jpeg-to-pdf/0.2.0")]
//! Creates PDFs from JPEG images.
//!
//! Images are embedded directly in the PDF, without any re-encoding.
//!
//! # Example
//!
//! ```no_run
//! use std::fs::{self, File};
//! use std::io::BufWriter;
//!
//! use jpeg_to_pdf::JpegToPdf;
//!
//! let out_file = File::create("out.pdf").unwrap();
//!
//! JpegToPdf::new()
//!     .add_image(fs::read("one.jpg").unwrap())
//!     .add_image(fs::read("two.jpg").unwrap())
//!     .add_image(fs::read("three.jpg").unwrap())
//!     .create_pdf(&mut BufWriter::new(out_file))
//!     .unwrap();
//! ```

use errors::Error;
pub use errors::*;
use exif::{Field, In, Reader as ExifReader, Tag, Value};
use img_parts::{jpeg::Jpeg, ImageEXIF};
use jpeg_decoder::{Decoder as JpegDecoder, PixelFormat};
use ori::Orientation;
use printpdf::*;
use std::io::{prelude::*, BufWriter, Cursor};

#[macro_use]
extern crate lazy_static;

mod errors;
mod ori;

lazy_static! {
    static ref DEFAULT_ORIENTATION: Field = Field {
        tag: Tag::Orientation,
        ifd_num: In::PRIMARY,
        value: Value::Short(vec![1]),
    };
}

/// Creates a PDF from JPEG images.
pub struct JpegToPdf {
    images: Vec<Vec<u8>>,
    dpi: f64,
    strip_exif: bool,
}

impl JpegToPdf {
    pub fn new() -> JpegToPdf {
        JpegToPdf {
            images: Vec::new(),
            dpi: 300.0,
            strip_exif: false,
        }
    }

    /// Add an image to the PDF output.
    pub fn add_image(mut self, image: Vec<u8>) -> JpegToPdf {
        self.images.push(image);
        self
    }

    /// Add one or more images to the PDF output.
    pub fn add_images(mut self, images: impl IntoIterator<Item = Vec<u8>>) -> JpegToPdf {
        self.images.extend(images);
        self
    }

    /// Set the DPI scaling of the PDF output.
    pub fn set_dpi(mut self, dpi: f64) -> JpegToPdf {
        self.dpi = dpi;
        self
    }

    /// Strip EXIF metadata from the provided images.
    ///
    /// Some PDF renderers have issues rendering JPEG images that still have EXIF metadata.
    pub fn strip_exif(mut self, strip_exif: bool) -> JpegToPdf {
        self.strip_exif = strip_exif;
        self
    }

    pub fn create_pdf(self, out: &mut BufWriter<impl Write>) -> Result<(), Error> {
        let doc = PdfDocument::empty("");
        for (index, image) in self.images.into_iter().enumerate() {
            if let Err(cause) = add_page(image, &doc, self.dpi, self.strip_exif) {
                return Err(Error { index, cause });
            }
        }
        doc.save(out).map_err(|e| Error {
            index: 0,
            cause: Cause::PdfWrite(e),
        })
    }
}

fn add_page(
    image: Vec<u8>,
    doc: &PdfDocumentReference,
    dpi: f64,
    strip_exif: bool,
) -> Result<(), Cause> {
    let mut decoder = JpegDecoder::new(Cursor::new(&image));
    decoder.read_info()?;

    match decoder.info() {
        None => Err(Cause::UnexpectedImageInfo),
        Some(info) => {
            let mut image = Jpeg::from_bytes(image.into())?;

            let ori = match image.exif() {
                None => 1,
                Some(exif) => match ExifReader::new().read_raw(exif.into_iter().collect()) {
                    Err(_) => 1,
                    Ok(exif) => match &exif
                        .get_field(Tag::Orientation, In::PRIMARY)
                        .unwrap_or(&DEFAULT_ORIENTATION)
                        .value
                    {
                        Value::Short(v) => *v.first().unwrap_or(&1),
                        _ => 1,
                    },
                },
            };

            let ori = Orientation {
                value: ori,
                width: info.width,
                height: info.height,
            };

            if strip_exif {
                image.set_exif(None);
            }

            let mut image_data = Vec::new();
            image.encoder().write_to(&mut image_data).unwrap();

            let (page, layer) = doc.add_page(
                Px(ori.display_width() as usize).into_pt(dpi).into(),
                Px(ori.display_height() as usize).into_pt(dpi).into(),
                "",
            );

            let image = Image::from(ImageXObject {
                width: Px(info.width as usize),
                height: Px(info.height as usize),
                color_space: match info.pixel_format {
                    PixelFormat::L8 => ColorSpace::Greyscale,
                    PixelFormat::RGB24 => ColorSpace::Rgb,
                    PixelFormat::CMYK32 => ColorSpace::Cmyk,
                },
                bits_per_component: ColorBits::Bit8,
                interpolate: false,
                image_data,
                image_filter: Some(ImageFilter::DCT),
                clipping_bbox: None,
            });

            image.add_to_layer(
                doc.get_page(page).get_layer(layer),
                Some(Px(ori.translate_x() as usize).into_pt(dpi).into()),
                Some(Px(ori.translate_y() as usize).into_pt(dpi).into()),
                Some(ori.rotate_cw()),
                Some(ori.scale_x()),
                None,
                Some(dpi),
            );

            Ok(())
        }
    }
}

/// Creates a PDF file from the provided JPEG data.
///
/// PDF data is written to `out`.
///
/// `dpi` defaults to `300.0`.
///
/// Please use [`JpegToPdf`] instead.
#[deprecated]
pub fn create_pdf_from_jpegs(
    jpegs: Vec<Vec<u8>>,
    out: &mut BufWriter<impl Write>,
    dpi: Option<f64>,
) -> Result<(), Error> {
    JpegToPdf::new()
        .add_images(jpegs)
        .set_dpi(dpi.unwrap_or(300.0))
        .create_pdf(out)
}