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