[BE] Use imagemagick instead of printpdf

master
Araozu 2023-11-17 15:16:37 -05:00
parent 57f69a6096
commit f86131d3a5
3 changed files with 43 additions and 44 deletions

View File

@ -2,4 +2,11 @@
Internal system for EEGSAC's internal processes.
## Requirements
This system requires the following:
- Imagequick, available through the command `convert`

View File

@ -1,13 +1,10 @@
use crate::{json_result::JsonResult, model::register::Register};
use printpdf::{Mm, PdfDocument};
use rocket::{http::Status, serde::json::Json};
use serde::{Deserialize, Serialize};
use std::process::Command;
use std::{
f32::consts::E,
fs::{self, File},
io::BufWriter,
fs,
path::PathBuf,
result,
time::{SystemTime, UNIX_EPOCH},
};
@ -164,6 +161,8 @@ fn get_image_info(path: PathBuf) -> ScanInfo {
// crop image
let cropped_img = img.crop_imm(0, third_point_height, mid_point_width, remaining_height);
// TODO: threshold image before getting qr
// get qr from cropped image
let results = bardecoder::default_decoder().decode(&cropped_img);
@ -335,47 +334,41 @@ async fn convert_scans_from_data(data: &Vec<(PathBuf, ScanInfo)>) -> Result<(),
}
fn convert_to_pdf(image_path: &PathBuf, output_path: &PathBuf) -> Result<(), String> {
let img = match image::open(image_path) {
Ok(i) => i,
// Just use imagemagick to convert the image to pdf
// -interlace Plane -gaussian-blur 0.05 -quality 75% -rotate 270 doc.jpg doc.jpg.pdf
let call = Command::new("convert")
.arg("-strip")
.arg("-interlace")
.arg("Plane")
.arg("-gaussian-blur")
.arg("0.05")
.arg("-quality")
.arg("75%")
.arg("-rotate")
.arg("270")
.arg(image_path)
.arg(output_path)
.spawn();
let mut call = match call {
Ok(r) => r,
Err(err) => {
log::error!("Error opening image: {:?}", err);
return Err(format!("Error abriendo imagen"));
log::error!("Error starting imagemagick: {:?}", err);
return Err(format!("Error convirtiendo 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");
PdfDocument::new("Certificado - EEGSAC", Mm(210.0), Mm(297.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()));
// TODO: Delete image
match result {
Ok(_) => Ok(()),
Err(reason) => {
log::error!("Error saving pdf: {:?}", reason);
Err(format!("Error guardando pdf"))
match call.wait() {
Ok(exit_status) if exit_status.success() => Ok(()),
Ok(exit_status) => {
log::error!("Error calling imagemagick: {:?}", exit_status);
Err(format!(
"Error ejecutando imagemagick: codigo de salida no exitoso"
))
}
Err(err) => {
log::error!("Error calling imagemagick: {:?}", err);
Err(format!("Error ejecutando imagemagick"))
}
}
}

View File

@ -225,4 +225,3 @@ impl Register {
Ok(data)
}
}