41 lines
1.4 KiB
Rust
41 lines
1.4 KiB
Rust
|
use std::{fs, thread};
|
||
|
use std::{fs::File, time::Duration};
|
||
|
use std::io::prelude::*;
|
||
|
|
||
|
use headless_chrome::Browser;
|
||
|
use headless_chrome::{types::PrintToPdfOptions, LaunchOptions};
|
||
|
use log::error;
|
||
|
use rocket::{get, Response, futures::Stream, tokio::net::UnixStream, fs::NamedFile};
|
||
|
|
||
|
fn generate_pdf() {
|
||
|
let browser = Browser::new(LaunchOptions::default()).unwrap();
|
||
|
let tab = browser.new_tab().unwrap();
|
||
|
let tab = tab.navigate_to("file:///home/6a6996c0-1609-48b6-8ca6-affbef1b4d1d/Devel/Nanobyte/ovlach/ovlach_pdf/template.html").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(29.7),
|
||
|
//paper_height: Some(21.0),
|
||
|
..PrintToPdfOptions::default()
|
||
|
};
|
||
|
|
||
|
//thread::sleep(Duration::from_secs(10));
|
||
|
|
||
|
let bytes = tab.print_to_pdf(Some(options)).unwrap();
|
||
|
let mut file = fs::OpenOptions::new()
|
||
|
// .create(true) // To create a new file
|
||
|
.write(true)
|
||
|
// either use the ? operator or unwrap since it returns a Result
|
||
|
.open("/tmp/foo.pdf").unwrap();
|
||
|
file.write_all(&bytes).unwrap();
|
||
|
}
|
||
|
|
||
|
#[get("/cv/<username>/pdf")]
|
||
|
pub async fn render_pdf_cv(username: &str) -> NamedFile {
|
||
|
generate_pdf();
|
||
|
"foo!".to_string();
|
||
|
NamedFile::open("/tmp/foo.pdf").await.expect("failed to open foo.pdf")
|
||
|
}
|