1mod docx;
2mod error;
3mod fonts;
4mod model;
5mod pdf;
6
7pub use error::Error;
8
9use std::path::Path;
10use std::time::Instant;
11
12pub fn convert_docx_to_pdf(input: impl AsRef<Path>, path: impl AsRef<Path>) -> Result<(), Error> {
13 let path = path.as_ref().with_extension("pdf");
14 let t0 = Instant::now();
15
16 let doc = docx::parse(input.as_ref())?;
17 let t_parse = t0.elapsed();
18
19 let bytes = pdf::render(&doc)?;
20 let t_render = t0.elapsed();
21
22 std::fs::write(&path, &bytes).map_err(Error::Io)?;
23 let t_total = t0.elapsed();
24
25 log::info!(
26 "Timing: parse={:.1}ms, render={:.1}ms, write={:.1}ms, total={:.1}ms (output {} bytes)",
27 t_parse.as_secs_f64() * 1000.0,
28 (t_render - t_parse).as_secs_f64() * 1000.0,
29 (t_total - t_render).as_secs_f64() * 1000.0,
30 t_total.as_secs_f64() * 1000.0,
31 bytes.len(),
32 );
33
34 Ok(())
35}
36
37pub fn convert_docx_bytes_to_pdf(input: &[u8], path: impl AsRef<Path>) -> Result<(), Error> {
38 let path = path.as_ref().with_extension("pdf");
39 let t0 = Instant::now();
40
41 let doc = docx::parse_bytes(input)?;
42 let t_parse = t0.elapsed();
43
44 let bytes = pdf::render(&doc)?;
45 let t_render = t0.elapsed();
46
47 std::fs::write(&path, &bytes).map_err(Error::Io)?;
48 let t_total = t0.elapsed();
49
50 log::info!(
51 "Timing: parse={:.1}ms, render={:.1}ms, write={:.1}ms, total={:.1}ms (output {} bytes)",
52 t_parse.as_secs_f64() * 1000.0,
53 (t_render - t_parse).as_secs_f64() * 1000.0,
54 (t_total - t_render).as_secs_f64() * 1000.0,
55 t_total.as_secs_f64() * 1000.0,
56 bytes.len(),
57 );
58
59 Ok(())
60}