diff --git a/Cargo.toml b/Cargo.toml index e70bb94..ee26b65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] -printpdf = "0.8.2" +printpdf = { version = "0.8.2", features = [ "png", "jpeg"] } diff --git a/image_example.pdf b/image_example.pdf new file mode 100644 index 0000000..c8d39ea Binary files /dev/null and b/image_example.pdf differ diff --git a/linux_antifa.png b/linux_antifa.png new file mode 100644 index 0000000..ef1f1a9 Binary files /dev/null and b/linux_antifa.png differ diff --git a/src/layout.rs b/src/layout.rs index a5eb6ea..73cb363 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -3,7 +3,7 @@ use crate::stickers::*; #[derive(Debug)] pub struct Page { - stickers: Vec, + pub stickers: Vec, } pub fn generate_page(sticker_details: Vec) -> Vec { @@ -15,6 +15,10 @@ pub fn generate_page(sticker_details: Vec) -> Vec { }; for sticker_detail in sticker_details { + if !sticker_detail.path.is_file() { + panic!("Erreur d'ouverture du fichier : {:?}", sticker_detail.path) + } + let total_width = total_width(&config, sticker_detail.width); let total_height = total_height(&config, sticker_detail.height); @@ -27,10 +31,6 @@ pub fn generate_page(sticker_details: Vec) -> Vec { while remaining > 0 { let index = current_page.stickers.len(); - if index == stickers_per_page { - pages.push(current_page); - current_page = Page { stickers: vec![] }; - } let i = index / cols; let j = index % cols; @@ -40,6 +40,14 @@ pub fn generate_page(sticker_details: Vec) -> Vec { current_page.stickers.push(create_sticker(sticker_detail.clone(), position_x, position_y)); remaining -= 1; + + let index = current_page.stickers.len(); + + if index == stickers_per_page { + pages.push(current_page); + current_page = Page { stickers: vec![] }; + } + } } @@ -53,7 +61,7 @@ pub fn generate_page(sticker_details: Vec) -> Vec { fn create_sticker(sticker_detail: StickerDetail, x: f64, y: f64) -> Sticker { Sticker { path: sticker_detail.path, - position: (x, y), + position: Position { x, y }, width: sticker_detail.width, height: sticker_detail.width } diff --git a/src/main.rs b/src/main.rs index f844897..17642d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,22 @@ use printstick::layout::generate_page; use printstick::stickers::StickerDetail; use printstick::layout::Page; +use printstick::pdf::generate_pdf; +use std::path::PathBuf; fn main() { let stickers_order: Vec = vec![ StickerDetail { number: 52, - path: String::from("/test"), + path: PathBuf::from("/Users/torpenn/projects/personal/PrintStick/linux_antifa.png"), height: 50.0, width: 50.0 - }, - StickerDetail { - number: 10, - path: String::from("/nouveau_sticker"), - height: 40.0, - width: 30.0 - }, + } ]; let pages: Vec = generate_page(stickers_order); println!("Generated positions in one page: {:?}", pages); + + generate_pdf(PathBuf::from("./image_example.pdf"), pages) } diff --git a/src/pdf.rs b/src/pdf.rs index 9a0d41e..e5610ff 100644 --- a/src/pdf.rs +++ b/src/pdf.rs @@ -1,3 +1,47 @@ -// pub fn generate_pdf(output_path: &str, positions: Vec<(Mm, Mm)>, stickers: &[Sticker]) { +use crate::layout::Page; +use printpdf::*; +use std::path::PathBuf; -// } +pub fn generate_pdf(output_path: PathBuf, pages: Vec) { + let mut doc = PdfDocument::new("Planches"); + + let mut final_pages = Vec::new(); + + for page in pages { + let mut ops = Vec::new(); + + for sticker in page.stickers { + let image_bytes = std::fs::read(sticker.path).unwrap(); + let image = RawImage::decode_from_bytes(&image_bytes, &mut Vec::new()).unwrap(); + + let image_id = doc.add_image(&image); + + ops.push(Op::UseXobject { + id: image_id.clone(), + transform: XObjectTransform { + translate_x: Some(Mm(sticker.position.x as f32).into()), + translate_y: Some(Mm(sticker.position.y as f32).into()), + rotate: Some(XObjectRotation { + angle_ccw_degrees: 0.0, + rotation_center_x: Px(0), + rotation_center_y: Px(0), + }), + scale_x: Some(1.0), + scale_y: Some(1.0), + dpi: Some(300.0), + }, + }); + } + + let page = PdfPage::new(Mm(420.0), Mm(297.0), ops); + + final_pages.push(page) + } + + let bytes = doc + .with_pages(final_pages) + .save(&PdfSaveOptions::default(), &mut Vec::new()); + + std::fs::write(output_path, bytes).unwrap(); + println!("Created image_example.pdf"); +} diff --git a/src/stickers.rs b/src/stickers.rs index 4fd8ba6..5066712 100644 --- a/src/stickers.rs +++ b/src/stickers.rs @@ -1,17 +1,25 @@ +use std::path::PathBuf; + // Use for one stickers #[derive(Debug)] pub struct Sticker { - pub path: String, - pub position: (f64, f64), // (x, y) + pub path: PathBuf, + pub position: Position, pub width: f64, pub height: f64 } -// User for many different sticker +#[derive(Debug)] +pub struct Position { + pub x: f64, + pub y: f64 +} + +// Use for many different sticker #[derive(Clone, Debug)] pub struct StickerDetail { pub number: u32, - pub path: String, + pub path: PathBuf, pub width: f64, pub height: f64, }