From 5295ca0c969c17abaead9ec03824357d84aa4873 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Fri, 4 Apr 2025 02:34:46 +0200 Subject: [PATCH] Do the right computations when finding the number of pages --- main.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/main.rs b/main.rs index 65ae471..9fa119f 100644 --- a/main.rs +++ b/main.rs @@ -8,12 +8,13 @@ use printpdf::{ use structopt::StructOpt; use ab_glyph::{FontRef, PxScale}; -use image::{EncodableLayout, ImageBuffer, Rgba}; +use image::{ImageBuffer, Rgba}; use imageproc::drawing::draw_text_mut; // A4 dimensions const A4_WIDTH: f32 = 210.0; const A4_HEIGHT: f32 = 297.0; + // Configuration for the label const DPI: f32 = 300.0; const MM_TO_PX: f32 = DPI / 25.4; @@ -169,7 +170,8 @@ fn combine_labels( labels: &[ImageBuffer, Vec>], output_dir: &str, ) -> Result<(), Box> { - let labels_per_page = (A4_HEIGHT / LABEL_HEIGHT).floor() as usize; + let labels_per_page = (A4_HEIGHT / (LABEL_HEIGHT / MM_TO_PX)) as usize; + println!("Labels per page: {}", labels_per_page); // Create a new PDF document let mut doc = PdfDocument::new("Labels Document"); @@ -182,8 +184,6 @@ fn combine_labels( let mut y_position = A4_HEIGHT - LABEL_HEIGHT; for label in chunk { - // let image = RawImage::decode_from_bytes(label.as_raw(), &mut Vec::new()).unwrap(); - // let bytes = include_bytes!("output/label_GV-Ek9pYRis.png"); let pixels = label.clone().into_raw(); let image = RawImage { pixels: RawImageData::U8(pixels), @@ -194,10 +194,12 @@ fn combine_labels( }; let image_id = doc.add_image(&image); - // Add image to page at current y position page_contents.push(Op::UseXobject { id: image_id, - transform: XObjectTransform::default(), + transform: XObjectTransform { + translate_y: Some(printpdf::Pt(y_position)), + ..Default::default() + }, }); // Move y position up for next label @@ -216,7 +218,6 @@ fn combine_labels( // Write PDF bytes to file std::fs::write("test.pdf", pdf_bytes)?; - Ok(()) } @@ -238,5 +239,6 @@ fn main() -> Result<(), Box> { for record in records { labels.push(generate_label(record)); } + println!("labels: {}", labels.len()); combine_labels(&labels, &opts.output_dir) }