1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::fs;
use std::path::Path;
use headless_chrome::protocol::page::PrintToPdfOptions;
use headless_chrome::{Browser, LaunchOptionsBuilder};
use log::info;
mod cli;
pub use cli::*;
pub fn run(opt: CliOptions) {
let input = fs::canonicalize(opt.input()).unwrap();
let output = if let Some(out) = opt.output() {
out.clone()
} else {
let mut out = opt.input().clone();
out.set_extension("pdf");
out
};
let pdf_options = PrintToPdfOptions {
landscape: Some(opt.landscape()),
display_header_footer: None,
print_background: Some(opt.background()),
scale: None,
paper_width: None,
paper_height: None,
margin_top: None,
margin_bottom: None,
margin_left: None,
margin_right: None,
page_ranges: None,
ignore_invalid_page_ranges: None,
header_template: None,
footer_template: None,
prefer_css_page_size: None,
};
html_to_pdf(input, output, pdf_options);
}
pub fn html_to_pdf<I, O>(input: I, output: O, pdf_options: PrintToPdfOptions)
where
I: AsRef<Path>,
O: AsRef<Path>,
{
let input = format!("file://{}", input.as_ref().as_os_str().to_str().unwrap());
info!("Input file: {}", input);
let local_pdf = print_to_pdf(&input, pdf_options);
info!("Output file: {:?}", output.as_ref());
fs::write(output, &local_pdf).unwrap();
}
fn print_to_pdf(file_path: &str, pdf_options: PrintToPdfOptions) -> Vec<u8> {
let options = LaunchOptionsBuilder::default().build().unwrap();
let browser = Browser::new(options).unwrap();
let tab = browser.wait_for_initial_tab().unwrap();
tab.navigate_to(file_path)
.unwrap()
.wait_until_navigated()
.unwrap()
.print_to_pdf(Some(pdf_options))
.unwrap()
}