Do the right computations when finding the number of pages

This commit is contained in:
Alexis Métaireau 2025-04-04 02:34:46 +02:00
parent 68981092ad
commit 5295ca0c96
No known key found for this signature in database
GPG key ID: 1C21B876828E5FF2

16
main.rs
View file

@ -8,12 +8,13 @@ use printpdf::{
use structopt::StructOpt; use structopt::StructOpt;
use ab_glyph::{FontRef, PxScale}; use ab_glyph::{FontRef, PxScale};
use image::{EncodableLayout, ImageBuffer, Rgba}; use image::{ImageBuffer, Rgba};
use imageproc::drawing::draw_text_mut; use imageproc::drawing::draw_text_mut;
// A4 dimensions // A4 dimensions
const A4_WIDTH: f32 = 210.0; const A4_WIDTH: f32 = 210.0;
const A4_HEIGHT: f32 = 297.0; const A4_HEIGHT: f32 = 297.0;
// Configuration for the label // Configuration for the label
const DPI: f32 = 300.0; const DPI: f32 = 300.0;
const MM_TO_PX: f32 = DPI / 25.4; const MM_TO_PX: f32 = DPI / 25.4;
@ -169,7 +170,8 @@ fn combine_labels(
labels: &[ImageBuffer<Rgba<u8>, Vec<u8>>], labels: &[ImageBuffer<Rgba<u8>, Vec<u8>>],
output_dir: &str, output_dir: &str,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
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 // Create a new PDF document
let mut doc = PdfDocument::new("Labels Document"); let mut doc = PdfDocument::new("Labels Document");
@ -182,8 +184,6 @@ fn combine_labels(
let mut y_position = A4_HEIGHT - LABEL_HEIGHT; let mut y_position = A4_HEIGHT - LABEL_HEIGHT;
for label in chunk { 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 pixels = label.clone().into_raw();
let image = RawImage { let image = RawImage {
pixels: RawImageData::U8(pixels), pixels: RawImageData::U8(pixels),
@ -194,10 +194,12 @@ fn combine_labels(
}; };
let image_id = doc.add_image(&image); let image_id = doc.add_image(&image);
// Add image to page at current y position
page_contents.push(Op::UseXobject { page_contents.push(Op::UseXobject {
id: image_id, id: image_id,
transform: XObjectTransform::default(), transform: XObjectTransform {
translate_y: Some(printpdf::Pt(y_position)),
..Default::default()
},
}); });
// Move y position up for next label // Move y position up for next label
@ -216,7 +218,6 @@ fn combine_labels(
// Write PDF bytes to file // Write PDF bytes to file
std::fs::write("test.pdf", pdf_bytes)?; std::fs::write("test.pdf", pdf_bytes)?;
Ok(()) Ok(())
} }
@ -238,5 +239,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
for record in records { for record in records {
labels.push(generate_label(record)); labels.push(generate_label(record));
} }
println!("labels: {}", labels.len());
combine_labels(&labels, &opts.output_dir) combine_labels(&labels, &opts.output_dir)
} }