pdf_oxide 0.3.38

The fastest Rust PDF library with text extraction: 0.8ms mean, 100% pass rate on 3,830 PDFs. 5× faster than pdf_extract, 17× faster than oxidize_pdf. Extract, create, and edit PDFs.
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! PPTX to PDF conversion.
//!
//! Parses Microsoft PowerPoint presentations (.pptx) and converts them to PDF.
//!
//! PPTX files are ZIP archives containing XML files in Open XML format.
//! Slides are in `ppt/slides/slideN.xml`.

use super::OfficeConfig;
use crate::error::{Error, Result};
use crate::writer::{DocumentBuilder, DocumentMetadata, PageSize};
use quick_xml::events::Event;
use quick_xml::Reader;
use std::io::{Cursor, Read};
use zip::ZipArchive;

/// PPTX to PDF converter.
pub struct PptxConverter {
    config: OfficeConfig,
}

impl PptxConverter {
    /// Create a new PPTX converter.
    pub fn new(config: OfficeConfig) -> Self {
        Self { config }
    }

    /// Convert PPTX bytes to PDF bytes.
    pub fn convert(&self, bytes: &[u8]) -> Result<Vec<u8>> {
        let cursor = Cursor::new(bytes);
        let mut archive = ZipArchive::new(cursor)
            .map_err(|e| Error::InvalidPdf(format!("Failed to open PPTX archive: {}", e)))?;

        // Get slide count and parse slides
        let slide_count = self.get_slide_count(&mut archive)?;
        let mut slides: Vec<SlideContent> = Vec::new();

        for i in 1..=slide_count {
            if let Ok(slide) = self.parse_slide(&mut archive, i) {
                slides.push(slide);
            }
        }

        if slides.is_empty() {
            return Err(Error::InvalidPdf("No slides found in presentation".to_string()));
        }

        // Get presentation title from metadata
        let title = self.parse_metadata(&mut archive).unwrap_or_default();

        self.build_pdf(&title, &slides)
    }

    /// Get the number of slides in the presentation.
    fn get_slide_count<R: Read + std::io::Seek>(
        &self,
        archive: &mut ZipArchive<R>,
    ) -> Result<usize> {
        // Count slide files in ppt/slides/
        let mut count = 0;
        for i in 0..archive.len() {
            if let Ok(file) = archive.by_index(i) {
                let name = file.name();
                if name.starts_with("ppt/slides/slide") && name.ends_with(".xml") {
                    count += 1;
                }
            }
        }
        Ok(count)
    }

    /// Parse a single slide.
    fn parse_slide<R: Read + std::io::Seek>(
        &self,
        archive: &mut ZipArchive<R>,
        slide_num: usize,
    ) -> Result<SlideContent> {
        let slide_path = format!("ppt/slides/slide{}.xml", slide_num);

        let xml_content = match archive.by_name(&slide_path) {
            Ok(mut file) => {
                let mut content = String::new();
                file.read_to_string(&mut content)
                    .map_err(|e| Error::InvalidPdf(format!("Failed to read slide: {}", e)))?;
                content
            },
            Err(e) => return Err(Error::InvalidPdf(format!("Slide not found: {}", e))),
        };

        let mut slide = SlideContent {
            number: slide_num,
            title: None,
            text_boxes: Vec::new(),
        };

        // Parse XML
        let mut reader = Reader::from_str(&xml_content);
        reader.config_mut().trim_text(true);

        let mut buf = Vec::new();
        let mut in_shape = false;
        let mut in_text_body = false;
        let mut _in_paragraph = false;
        let mut in_text = false;
        let mut current_text = String::new();
        let mut current_paragraph = String::new();
        let mut is_title = false;

        loop {
            match reader.read_event_into(&mut buf) {
                Ok(Event::Start(ref e)) => {
                    let local_name = e.local_name();
                    match local_name.as_ref() {
                        b"sp" => {
                            in_shape = true;
                            is_title = false;
                        },
                        b"txBody" => {
                            in_text_body = true;
                        },
                        b"p" => {
                            _in_paragraph = true;
                            current_paragraph.clear();
                        },
                        b"t" => {
                            in_text = true;
                        },
                        b"ph" => {
                            // Placeholder type - check if title
                            for attr in e.attributes().flatten() {
                                if attr.key.local_name().as_ref() == b"type" {
                                    let val = String::from_utf8_lossy(&attr.value);
                                    if val == "title" || val == "ctrTitle" {
                                        is_title = true;
                                    }
                                }
                            }
                        },
                        _ => {},
                    }
                },
                Ok(Event::End(ref e)) => {
                    let local_name = e.local_name();
                    match local_name.as_ref() {
                        b"sp" => {
                            in_shape = false;
                            is_title = false;
                        },
                        b"txBody" => {
                            in_text_body = false;
                            if !current_text.is_empty() {
                                if is_title && slide.title.is_none() {
                                    slide.title = Some(current_text.trim().to_string());
                                } else {
                                    slide.text_boxes.push(TextBox {
                                        text: current_text.trim().to_string(),
                                    });
                                }
                                current_text.clear();
                            }
                        },
                        b"p" => {
                            _in_paragraph = false;
                            if !current_paragraph.is_empty() {
                                if !current_text.is_empty() {
                                    current_text.push('\n');
                                }
                                current_text.push_str(&current_paragraph);
                            }
                        },
                        b"t" => {
                            in_text = false;
                        },
                        _ => {},
                    }
                },
                Ok(Event::Text(e)) => {
                    if in_text && in_text_body && in_shape {
                        let text = e.xml_content().unwrap_or_default();
                        current_paragraph.push_str(&text);
                    }
                },
                Ok(Event::Eof) => break,
                Err(_) => break,
                _ => {},
            }
            buf.clear();
        }

        Ok(slide)
    }

    /// Parse presentation metadata.
    fn parse_metadata<R: Read + std::io::Seek>(
        &self,
        archive: &mut ZipArchive<R>,
    ) -> Option<String> {
        let xml_content = match archive.by_name("docProps/core.xml") {
            Ok(mut file) => {
                let mut content = String::new();
                file.read_to_string(&mut content).ok()?;
                content
            },
            Err(_) => return None,
        };

        let mut reader = Reader::from_str(&xml_content);
        reader.config_mut().trim_text(true);

        let mut buf = Vec::new();
        let mut in_title = false;
        let mut title = None;

        loop {
            match reader.read_event_into(&mut buf) {
                Ok(Event::Start(ref e)) => {
                    if e.local_name().as_ref() == b"title" {
                        in_title = true;
                    }
                },
                Ok(Event::End(ref e)) => {
                    if e.local_name().as_ref() == b"title" {
                        in_title = false;
                    }
                },
                Ok(Event::Text(e)) => {
                    if in_title {
                        title = Some(e.xml_content().unwrap_or_default().to_string());
                    }
                },
                Ok(Event::Eof) => break,
                Err(_) => break,
                _ => {},
            }
            buf.clear();
        }

        title
    }

    /// Build PDF from parsed slides.
    fn build_pdf(&self, title: &str, slides: &[SlideContent]) -> Result<Vec<u8>> {
        let metadata = DocumentMetadata::new()
            .title(if title.is_empty() {
                "Presentation"
            } else {
                title
            })
            .creator("pdf_oxide");

        let mut builder = DocumentBuilder::new().metadata(metadata);

        // Use landscape orientation for slides (swap width/height from config)
        let (config_width, config_height) = self.config.page_size.dimensions();
        let slide_size =
            PageSize::Custom(config_height.max(config_width), config_height.min(config_width));
        let (page_width, page_height) = slide_size.dimensions();
        let margin = self.config.margins.left.min(self.config.margins.top); // Use smaller margin for slides

        // Font configuration from config
        let title_size = self.config.default_font_size * 2.5; // Title is 2.5x body text
        let text_size = self.config.default_font_size;
        let slide_number_size = self.config.default_font_size * 0.9;

        // Pre-process into render operations
        #[derive(Clone)]
        enum RenderOp {
            SlideNumber {
                num: usize,
                total: usize,
                x: f32,
                y: f32,
            },
            Title {
                text: String,
                y: f32,
            },
            Text {
                text: String,
                x: f32,
                y: f32,
            },
        }

        let mut all_ops: Vec<Vec<RenderOp>> = Vec::new();
        let total_slides = slides.len();

        for slide in slides {
            let mut ops: Vec<RenderOp> = Vec::new();
            let mut current_y = page_height - margin;

            // Slide number at bottom right
            ops.push(RenderOp::SlideNumber {
                num: slide.number,
                total: total_slides,
                x: page_width - margin - 50.0,
                y: self.config.margins.bottom,
            });

            // Slide title
            if let Some(ref title) = slide.title {
                ops.push(RenderOp::Title {
                    text: title.clone(),
                    y: current_y,
                });
                current_y -= title_size * 1.7; // Title height + spacing
            }

            // Text boxes
            let line_height = text_size * self.config.line_height;

            for text_box in &slide.text_boxes {
                if text_box.text.is_empty() {
                    continue;
                }

                // Split into lines
                for line in text_box.text.lines() {
                    if line.trim().is_empty() {
                        current_y -= line_height * 0.5;
                        continue;
                    }

                    // Simple word wrap
                    let max_width = page_width - margin * 2.0;
                    let avg_char_width = text_size * 0.5;
                    let max_chars = (max_width / avg_char_width) as usize;

                    if line.len() <= max_chars {
                        ops.push(RenderOp::Text {
                            text: line.to_string(),
                            x: margin,
                            y: current_y,
                        });
                        current_y -= line_height;
                    } else {
                        // Word wrap long lines
                        let words: Vec<&str> = line.split_whitespace().collect();
                        let mut current_line = String::new();

                        for word in words {
                            let test_line = if current_line.is_empty() {
                                word.to_string()
                            } else {
                                format!("{} {}", current_line, word)
                            };

                            if test_line.len() <= max_chars {
                                current_line = test_line;
                            } else {
                                if !current_line.is_empty() {
                                    ops.push(RenderOp::Text {
                                        text: current_line,
                                        x: margin,
                                        y: current_y,
                                    });
                                    current_y -= line_height;
                                }
                                current_line = word.to_string();
                            }
                        }

                        if !current_line.is_empty() {
                            ops.push(RenderOp::Text {
                                text: current_line,
                                x: margin,
                                y: current_y,
                            });
                            current_y -= line_height;
                        }
                    }
                }

                // Add spacing between text boxes
                current_y -= line_height * 0.5;
            }

            all_ops.push(ops);
        }

        // Render all operations
        let font_name = &self.config.default_font;
        let bold_font = format!("{}-Bold", font_name);

        for ops in &all_ops {
            let mut page_builder = builder.page(slide_size);

            for op in ops {
                match op {
                    RenderOp::SlideNumber { num, total, x, y } => {
                        page_builder = page_builder
                            .at(*x, *y)
                            .font(font_name, slide_number_size)
                            .text(&format!("{}/{}", num, total));
                    },
                    RenderOp::Title { text, y } => {
                        page_builder = page_builder
                            .at(margin, *y)
                            .font(&bold_font, title_size)
                            .text(text);
                    },
                    RenderOp::Text { text, x, y } => {
                        page_builder = page_builder
                            .at(*x, *y)
                            .font(font_name, text_size)
                            .text(text);
                    },
                }
            }

            page_builder.done();
        }

        builder.build()
    }
}

/// Parsed content from a slide.
#[derive(Debug)]
struct SlideContent {
    number: usize,
    title: Option<String>,
    text_boxes: Vec<TextBox>,
}

/// A text box on a slide.
#[derive(Debug)]
struct TextBox {
    text: String,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pptx_converter_new() {
        let config = OfficeConfig::default();
        let converter = PptxConverter::new(config);
        assert_eq!(converter.config.default_font, "Helvetica");
    }

    #[test]
    fn test_slide_content_default() {
        let slide = SlideContent {
            number: 1,
            title: Some("Test Title".to_string()),
            text_boxes: vec![],
        };
        assert_eq!(slide.number, 1);
        assert_eq!(slide.title, Some("Test Title".to_string()));
    }
}