[BE][Scans] Match records with db & convert to PDF
This commit is contained in:
parent
605ee27b4f
commit
0fe2c53ffd
@ -265,9 +265,105 @@ async fn convert_scans_from_data(data: &Vec<(PathBuf, ScanInfo)>) -> Result<(),
|
||||
}
|
||||
};
|
||||
|
||||
for e in persons_info {
|
||||
println!("\n{:?}", e);
|
||||
// Match the results from DB with the data from the frontend,
|
||||
// & convert the files to PDFs
|
||||
|
||||
for (_, info) in data {
|
||||
let (person_info, timestamp) = match info {
|
||||
ScanInfo::Full(dni, id, timestamp) => {
|
||||
let person_info = persons_info.iter().find(|e| e.register_id == *id);
|
||||
match person_info {
|
||||
Some(p) => (p, timestamp),
|
||||
None => {
|
||||
log::error!("Register not found in data from DB: {} {}", id, dni);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
ScanInfo::Partial(dni, timestamp) => {
|
||||
let person_info = persons_info
|
||||
.iter()
|
||||
.find(|e| e.person_dni == *dni && e.register_id == 0);
|
||||
match person_info {
|
||||
Some(p) => (p, timestamp),
|
||||
None => {
|
||||
log::error!("Register not found in data from DB: {}", dni);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
ScanInfo::Error(reason) => {
|
||||
log::info!("Found an error while matching data with DB: {}", reason);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let filename = format!(
|
||||
"{} {} {} - {} [{:X}].pdf",
|
||||
person_info.person_names,
|
||||
person_info.person_paternal_surname,
|
||||
person_info.person_maternal_surname,
|
||||
person_info.course_name,
|
||||
person_info.register_id,
|
||||
);
|
||||
|
||||
let image_path = PathBuf::from(format!("{}eeg_{}.jpg", SCAN_PATH, timestamp));
|
||||
let output_path = PathBuf::from(format!("{}{}", SCAN_PATH, filename));
|
||||
|
||||
match convert_to_pdf(&image_path, &output_path) {
|
||||
Ok(_) => {
|
||||
log::info!("Converted file: {}", filename)
|
||||
}
|
||||
Err(reason) => {
|
||||
log::error!("Error converting to pdf: {}", reason);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn convert_to_pdf(image_path: &PathBuf, output_path: &PathBuf) -> Result<(), String> {
|
||||
let img = match image::open(image_path) {
|
||||
Ok(i) => i,
|
||||
Err(err) => {
|
||||
log::error!("Error opening image: {:?}", err);
|
||||
return Err(format!("Error abriendo imagen"));
|
||||
}
|
||||
};
|
||||
|
||||
// Rotate image 45 degrees clockwise
|
||||
let img = img.rotate270();
|
||||
|
||||
// Generate PDF
|
||||
let (doc, page1, layer1) =
|
||||
PdfDocument::new("Certificado - EEGSAC", Mm(297.0), Mm(210.0), "Layer 1");
|
||||
|
||||
let current_layer = doc.get_page(page1).get_layer(layer1);
|
||||
|
||||
let pdf_image = printpdf::image::Image::from_dynamic_image(&img);
|
||||
let img_transform = printpdf::ImageTransform::default();
|
||||
|
||||
/*
|
||||
This section of code assumes that the image is scaned at 200 DPI.
|
||||
IF the image is scanned at a different DPI, this will give
|
||||
wrong results.
|
||||
*/
|
||||
let img_transform = printpdf::ImageTransform {
|
||||
dpi: Some(200.0),
|
||||
..img_transform
|
||||
};
|
||||
|
||||
pdf_image.add_to_layer(current_layer, img_transform);
|
||||
|
||||
let result = doc.save(&mut BufWriter::new(File::create(output_path).unwrap()));
|
||||
|
||||
match result {
|
||||
Ok(_) => Ok(()),
|
||||
Err(reason) => {
|
||||
log::error!("Error saving pdf: {:?}", reason);
|
||||
Err(format!("Error guardando pdf"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user