ovlach_pdf/src/routes/pdf.rs

132 lines
5.4 KiB
Rust

use headless_chrome::Browser;
use headless_chrome::types::PrintToPdfOptions;
use nanobyte_opentelemetry::rocket::{TracingSpan, OtelReqwestClient};
use nanobyte_tera::l18n::LanguageDescription;
use ovlach_data::cv::data::CV;
use reqwest::StatusCode;
use rocket::fs::NamedFile;
use rocket::serde::json::json;
use ::rocket::{State, http::Status};
use ::rocket::get;
use tempfile::NamedTempFile;
use tera::Context;
use tracing::{info_span, error, debug, Instrument, info};
use crate::tools::cache::NanoCache;
use crate::{chromium::rocket::BrowserHolder, tools::{tera::NanoTera, pdf::PdfStream, rocket::RequestLanguage}, services::cv::fetch_cv_data_from_backend, CVBackendConfig};
// TODO: request-id
fn generate_pdf(browser: Browser, file: &NamedTempFile) -> Vec<u8> {
let tab = browser.new_tab().unwrap();
let path = format!("file://{}", file.path().to_str().unwrap());
info_span!("open_pdf").in_scope(|| {
debug!("Render pdf from {}", &path);
tab.navigate_to(&path).unwrap().wait_until_navigated().unwrap();
});
let options = PrintToPdfOptions{
margin_bottom: Some(0.0),
margin_left: Some(0.0),
margin_right: Some(0.0),
margin_top: Some(0.0),
print_background: Some(true),
paper_width: Some(8.3),
paper_height: Some(11.7),
landscape: Some(false),
..PrintToPdfOptions::default()
};
//thread::sleep(Duration::from_secs(10));
info_span!("print_pdf").in_scope(|| {
tab.print_to_pdf(Some(options)).unwrap()
})
}
#[tracing::instrument]
fn render_template(template_name: &str, file: &NamedTempFile, tera: NanoTera, cv: CV, language: String) {
// TODO: handle errors
let mut tera_context = Context::new();
tera_context.insert("cv", &cv);
tera_context.insert("lang", &LanguageDescription::new(&language, "ovlach_pdf"));
match tera.0.render_to("two_column.html.tera", &tera_context, file) {
Ok(_) => {
debug!("Rendered template to {}", file.path().to_str().unwrap());
},
Err(e) => {
error!("Error rendering template {}: {}", &template_name, e);
}
}
}
#[allow(clippy::too_many_arguments)]
#[get("/cv/<username>/<language>/output.pdf?<force_rebuild>")]
pub async fn render_pdf_cv(username: &str, tera: NanoTera, tracing: TracingSpan, request_client: OtelReqwestClient,
cv_config: &State<CVBackendConfig>, language: RequestLanguage, browser: BrowserHolder, cache: &State<NanoCache<String, PdfStream>>,
force_rebuild: Option<bool>) -> Result<PdfStream, Status> {
async move {
match fetch_cv_data_from_backend(&cv_config.cv_backend_path, &username.to_string(), &request_client.0).await {
Ok(cv_data) => {
// TODO: CV hasher
let data = json!(cv_data).to_string();
match cache.get(&data, force_rebuild).await {
Some(data) => {
Ok(data.clone())
}
None => {
let file = tempfile::Builder::new().suffix(".html").tempfile().unwrap();
render_template("two_column", &file, tera, cv_data, language.language);
let span = info_span!("render_pdf", username = username);
let pdf = span.in_scope(||{
generate_pdf(browser.browser, &file)
});
let result = PdfStream::new(pdf);
cache.insert(data, result.clone()).await;
Ok(result)
}
}
},
Err(e) => {
match e {
crate::services::cv::FetchError::ReqwestError(re) => {
match re.status() {
Some(status) => {
if status == StatusCode::NOT_FOUND {
info!("CV not found for user {}", username);
Err(Status::NotFound)
} else {
Err(Status::InternalServerError)
}
}
None => {
error!("Error fetching cv data: {:?}", re);
Err(Status::InternalServerError)
},
}
}
}
}
}
}.instrument(tracing.0).await
}
/// Route only for debuging
#[get("/cv/<username>/<language>/output.html")]
pub async fn render_html_cv(username: &str, tera: NanoTera, tracing: TracingSpan, request_client: OtelReqwestClient,
cv_config: &State<CVBackendConfig>, language: RequestLanguage) -> Result<NamedFile, Status> {
async move {
match fetch_cv_data_from_backend(&cv_config.cv_backend_path, &username.to_string(), &request_client.0).await {
Ok(cv_data) => {
let file = tempfile::Builder::new().suffix(".html").tempfile().unwrap();
render_template("two_column", &file, tera, cv_data, language.language);
Ok(NamedFile::open(file.path()).await.unwrap())
},
Err(e) => {
error!("Error fetching cv data: {:?}", e);
Err(Status::InternalServerError)
}
}
}.instrument(tracing.0).await
}