office2pdf 0.5.0

Convert DOCX, XLSX, and PPTX files to PDF using pure Rust
Documentation
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
use std::collections::HashSet;
use std::time::Instant;

use crate::config::{ConvertOptions, Format};
use crate::error::{ConvertError, ConvertMetrics, ConvertResult, ConvertWarning};
use crate::parser::Parser;
use crate::{ir, parser, render};

fn format_label(format: Format) -> &'static str {
    match format {
        Format::Docx => "DOCX",
        Format::Pptx => "PPTX",
        Format::Xlsx => "XLSX",
    }
}

fn dedup_warnings(warnings: &mut Vec<ConvertWarning>) {
    let mut seen: HashSet<String> = HashSet::new();
    warnings.retain(|warning| seen.insert(warning.to_string()));
}

/// Build a `ConvertResult`, deduplicating warnings automatically so callers
/// don't need to remember to call `dedup_warnings` before every return site.
fn build_convert_result(
    pdf: Vec<u8>,
    mut warnings: Vec<ConvertWarning>,
    metrics: Option<ConvertMetrics>,
) -> ConvertResult {
    dedup_warnings(&mut warnings);
    ConvertResult {
        pdf,
        warnings,
        metrics,
    }
}

fn extract_panic_message(payload: &Box<dyn std::any::Any + Send>) -> String {
    if let Some(s) = payload.downcast_ref::<String>() {
        s.clone()
    } else if let Some(s) = payload.downcast_ref::<&str>() {
        (*s).to_string()
    } else {
        "unknown panic".to_string()
    }
}

const OLE2_MAGIC: [u8; 8] = [0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1];

pub(super) fn is_ole2(data: &[u8]) -> bool {
    data.len() >= OLE2_MAGIC.len() && data[..OLE2_MAGIC.len()] == OLE2_MAGIC
}

#[cfg(not(target_arch = "wasm32"))]
pub(super) fn should_resolve_font_context(
    doc: &ir::Document,
    options: &ConvertOptions,
    has_embedded_fonts: bool,
) -> bool {
    has_embedded_fonts
        || !options.font_paths.is_empty()
        || render::font_subst::document_requests_font_families(doc)
}

#[cfg(not(target_arch = "wasm32"))]
fn resolve_font_context_with_embedded(
    doc: &ir::Document,
    options: &ConvertOptions,
    embedded_font_dir: Option<&parser::embedded_fonts::EmbeddedFontDir>,
) -> Option<render::font_context::FontSearchContext> {
    let has_embedded = embedded_font_dir.is_some_and(|d| !d.is_empty());
    if !should_resolve_font_context(doc, options, has_embedded) {
        return None;
    }
    let mut all_paths: Vec<std::path::PathBuf> = options.font_paths.clone();
    if let Some(dir) = embedded_font_dir
        && !dir.is_empty()
    {
        all_paths.push(dir.path().to_path_buf());
    }
    Some(render::font_context::resolve_font_search_context(
        &all_paths,
    ))
}

#[cfg(not(target_arch = "wasm32"))]
pub(super) fn convert(path: impl AsRef<std::path::Path>) -> Result<ConvertResult, ConvertError> {
    convert_with_options(path, &ConvertOptions::default())
}

#[cfg(not(target_arch = "wasm32"))]
pub(super) fn convert_with_options(
    path: impl AsRef<std::path::Path>,
    options: &ConvertOptions,
) -> Result<ConvertResult, ConvertError> {
    let path = path.as_ref();
    let ext = path
        .extension()
        .and_then(|e| e.to_str())
        .ok_or_else(|| ConvertError::UnsupportedFormat("no file extension".to_string()))?;

    let format = Format::from_extension(ext)
        .ok_or_else(|| ConvertError::UnsupportedFormat(ext.to_string()))?;

    let data = std::fs::read(path)?;
    convert_bytes(&data, format, options)
}

pub(super) fn convert_bytes(
    data: &[u8],
    format: Format,
    options: &ConvertOptions,
) -> Result<ConvertResult, ConvertError> {
    if is_ole2(data) {
        return Err(ConvertError::UnsupportedEncryption);
    }

    #[cfg(feature = "pdf-ops")]
    if options.streaming && format == Format::Xlsx {
        return convert_bytes_streaming_xlsx(data, options);
    }

    let total_start: Instant = Instant::now();
    let input_size_bytes = data.len() as u64;

    // Extract embedded fonts before parsing (PPTX/DOCX only).
    // The EmbeddedFontDir must live until after PDF compilation so Typst can
    // discover the fonts via its search paths.
    #[cfg(not(target_arch = "wasm32"))]
    let embedded_font_dir = parser::embedded_fonts::extract_embedded_fonts(data, format);

    let parser: Box<dyn Parser> = match format {
        Format::Docx => Box::new(parser::docx::DocxParser),
        Format::Pptx => Box::new(parser::pptx::PptxParser),
        Format::Xlsx => Box::new(parser::xlsx::XlsxParser),
    };

    let parse_start: Instant = Instant::now();
    let parse_result =
        std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| parser.parse(data, options)));
    let (doc, mut warnings) = match parse_result {
        Ok(result) => result?,
        Err(panic_info) => {
            return Err(ConvertError::Parse(format!(
                "upstream parser panicked: {}",
                extract_panic_message(&panic_info)
            )));
        }
    };
    let parse_duration = parse_start.elapsed();
    let page_count = doc.pages.len() as u32;

    #[cfg(not(target_arch = "wasm32"))]
    let font_context =
        resolve_font_context_with_embedded(&doc, options, embedded_font_dir.as_ref());

    #[cfg(not(target_arch = "wasm32"))]
    if let Some(font_context) = font_context.as_ref() {
        warnings.extend(
            render::font_subst::detect_missing_font_fallbacks_with_context(&doc, font_context)
                .into_iter()
                .map(|(from, to)| ConvertWarning::FallbackUsed {
                    format: format_label(format).to_string(),
                    from,
                    to,
                }),
        );
    }

    #[cfg(target_arch = "wasm32")]
    warnings.extend(
        render::font_subst::detect_missing_font_fallbacks(&doc, &options.font_paths)
            .into_iter()
            .map(|(from, to)| ConvertWarning::FallbackUsed {
                format: format_label(format).to_string(),
                from,
                to,
            }),
    );

    let codegen_start: Instant = Instant::now();
    #[cfg(not(target_arch = "wasm32"))]
    let output = render::typst_gen::generate_typst_with_options_and_font_context(
        &doc,
        options,
        font_context.as_ref(),
    )?;
    #[cfg(target_arch = "wasm32")]
    let output = render::typst_gen::generate_typst_with_options(&doc, options)?;
    let codegen_duration = codegen_start.elapsed();

    let compile_start: Instant = Instant::now();
    #[cfg(not(target_arch = "wasm32"))]
    let pdf = render::pdf::compile_to_pdf(
        &output.source,
        &output.images,
        options.pdf_standard,
        font_context
            .as_ref()
            .map(|context| context.search_paths())
            .unwrap_or(&[]),
        options.tagged,
        options.pdf_ua,
    )?;
    #[cfg(target_arch = "wasm32")]
    let pdf = render::pdf::compile_to_pdf(
        &output.source,
        &output.images,
        options.pdf_standard,
        &options.font_paths,
        options.tagged,
        options.pdf_ua,
    )?;
    let compile_duration = compile_start.elapsed();

    let total_duration = total_start.elapsed();
    let output_size_bytes = pdf.len() as u64;

    Ok(build_convert_result(
        pdf,
        warnings,
        Some(ConvertMetrics {
            parse_duration,
            codegen_duration,
            compile_duration,
            total_duration,
            input_size_bytes,
            output_size_bytes,
            page_count,
        }),
    ))
}

#[cfg(feature = "pdf-ops")]
fn convert_bytes_streaming_xlsx(
    data: &[u8],
    options: &ConvertOptions,
) -> Result<ConvertResult, ConvertError> {
    let total_start: Instant = Instant::now();
    let input_size_bytes = data.len() as u64;
    let chunk_size = options
        .streaming_chunk_size
        .unwrap_or(crate::defaults::DEFAULT_STREAMING_CHUNK_SIZE);

    let xlsx_parser = parser::xlsx::XlsxParser;

    let parse_start: Instant = Instant::now();
    let parse_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        xlsx_parser.parse_streaming(data, options, chunk_size)
    }));
    let (chunk_docs, warnings) = match parse_result {
        Ok(result) => result?,
        Err(panic_info) => {
            return Err(ConvertError::Parse(format!(
                "upstream parser panicked: {}",
                extract_panic_message(&panic_info)
            )));
        }
    };
    let parse_duration = parse_start.elapsed();

    if chunk_docs.is_empty() {
        let empty_doc = ir::Document {
            metadata: ir::Metadata::default(),
            pages: vec![],
            styles: ir::StyleSheet::default(),
        };
        #[cfg(not(target_arch = "wasm32"))]
        let font_context = resolve_font_context_with_embedded(&empty_doc, options, None);
        #[cfg(not(target_arch = "wasm32"))]
        let output = render::typst_gen::generate_typst_with_options_and_font_context(
            &empty_doc,
            &ConvertOptions::default(),
            font_context.as_ref(),
        )?;
        #[cfg(target_arch = "wasm32")]
        let output = render::typst_gen::generate_typst(&empty_doc)?;
        #[cfg(not(target_arch = "wasm32"))]
        let pdf = render::pdf::compile_to_pdf(
            &output.source,
            &output.images,
            None,
            font_context
                .as_ref()
                .map(|context| context.search_paths())
                .unwrap_or(&[]),
            false,
            false,
        )?;
        #[cfg(target_arch = "wasm32")]
        let pdf =
            render::pdf::compile_to_pdf(&output.source, &output.images, None, &[], false, false)?;
        let total_duration = total_start.elapsed();
        return Ok(build_convert_result(
            pdf,
            warnings,
            Some(ConvertMetrics {
                parse_duration,
                codegen_duration: std::time::Duration::ZERO,
                compile_duration: std::time::Duration::ZERO,
                total_duration,
                input_size_bytes,
                output_size_bytes: 0,
                page_count: 0,
            }),
        ));
    }

    let mut all_pdfs: Vec<Vec<u8>> = Vec::with_capacity(chunk_docs.len());
    let mut codegen_duration_total = std::time::Duration::ZERO;
    let mut compile_duration_total = std::time::Duration::ZERO;
    let mut total_page_count: u32 = 0;

    #[cfg(not(target_arch = "wasm32"))]
    let font_context = if options.font_paths.is_empty()
        && !chunk_docs
            .iter()
            .any(render::font_subst::document_requests_font_families)
    {
        None
    } else {
        Some(render::font_context::resolve_font_search_context(
            &options.font_paths,
        ))
    };

    for chunk_doc in chunk_docs {
        total_page_count += chunk_doc.pages.len() as u32;

        let codegen_start: Instant = Instant::now();
        #[cfg(not(target_arch = "wasm32"))]
        let output = render::typst_gen::generate_typst_with_options_and_font_context(
            &chunk_doc,
            options,
            font_context.as_ref(),
        )?;
        #[cfg(target_arch = "wasm32")]
        let output = render::typst_gen::generate_typst_with_options(&chunk_doc, options)?;
        codegen_duration_total += codegen_start.elapsed();

        let compile_start: Instant = Instant::now();
        #[cfg(not(target_arch = "wasm32"))]
        let pdf = render::pdf::compile_to_pdf(
            &output.source,
            &output.images,
            options.pdf_standard,
            font_context
                .as_ref()
                .map(|context| context.search_paths())
                .unwrap_or(&[]),
            options.tagged,
            options.pdf_ua,
        )?;
        #[cfg(target_arch = "wasm32")]
        let pdf = render::pdf::compile_to_pdf(
            &output.source,
            &output.images,
            options.pdf_standard,
            &options.font_paths,
            options.tagged,
            options.pdf_ua,
        )?;
        compile_duration_total += compile_start.elapsed();

        all_pdfs.push(pdf);
    }

    let final_pdf = if all_pdfs.len() == 1 {
        // Safety: len() == 1 guarantees at least one element
        all_pdfs
            .into_iter()
            .next()
            .expect("all_pdfs is non-empty (len == 1)")
    } else {
        let refs: Vec<&[u8]> = all_pdfs.iter().map(|p| p.as_slice()).collect();
        crate::pdf_ops::merge(&refs)
            .map_err(|e| ConvertError::Render(format!("PDF merge failed: {e}")))?
    };

    let total_duration = total_start.elapsed();
    let output_size_bytes = final_pdf.len() as u64;

    Ok(build_convert_result(
        final_pdf,
        warnings,
        Some(ConvertMetrics {
            parse_duration,
            codegen_duration: codegen_duration_total,
            compile_duration: compile_duration_total,
            total_duration,
            input_size_bytes,
            output_size_bytes,
            page_count: total_page_count,
        }),
    ))
}

pub(super) fn render_document(doc: &ir::Document) -> Result<Vec<u8>, ConvertError> {
    #[cfg(not(target_arch = "wasm32"))]
    {
        let options = ConvertOptions::default();
        let font_context = resolve_font_context_with_embedded(doc, &options, None);
        let output = render::typst_gen::generate_typst_with_options_and_font_context(
            doc,
            &options,
            font_context.as_ref(),
        )?;
        render::pdf::compile_to_pdf(
            &output.source,
            &output.images,
            None,
            font_context
                .as_ref()
                .map(|context| context.search_paths())
                .unwrap_or(&[]),
            false,
            false,
        )
    }
    #[cfg(target_arch = "wasm32")]
    {
        let output = render::typst_gen::generate_typst(doc)?;
        render::pdf::compile_to_pdf(&output.source, &output.images, None, &[], false, false)
    }
}