pdf_oxide 0.3.33

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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//! Debug visualizer for rendering PDF pages with element annotations.

use crate::api::Pdf;
use crate::editor::{PdfElement, PdfPage};
use crate::error::{Error, Result};
use crate::geometry::Rect;

#[cfg(feature = "rendering")]
use crate::rendering::{RenderOptions, RenderedImage};

#[cfg(feature = "rendering")]
use tiny_skia::{Color, Paint, PathBuilder, Pixmap, Stroke, Transform};

/// Colors for different element types.
#[derive(Debug, Clone)]
pub struct ElementColors {
    /// Color for text elements (RGBA)
    pub text: [f32; 4],
    /// Color for image elements (RGBA)
    pub image: [f32; 4],
    /// Color for path elements (RGBA)
    pub path: [f32; 4],
    /// Color for table elements (RGBA)
    pub table: [f32; 4],
    /// Color for structure elements (RGBA)
    pub structure: [f32; 4],
}

impl Default for ElementColors {
    fn default() -> Self {
        Self {
            text: [1.0, 0.0, 0.0, 0.5],      // Red with 50% opacity
            image: [0.0, 1.0, 0.0, 0.5],     // Green with 50% opacity
            path: [0.0, 0.0, 1.0, 0.5],      // Blue with 50% opacity
            table: [1.0, 1.0, 0.0, 0.5],     // Yellow with 50% opacity
            structure: [1.0, 0.0, 1.0, 0.5], // Magenta with 50% opacity
        }
    }
}

/// Options for debug visualization.
#[derive(Debug, Clone)]
pub struct DebugOptions {
    /// Whether to show text element bounding boxes
    pub show_text_bounds: bool,
    /// Whether to show image element bounding boxes
    pub show_image_bounds: bool,
    /// Whether to show path element bounding boxes
    pub show_path_bounds: bool,
    /// Whether to show table element bounding boxes
    pub show_table_bounds: bool,
    /// Whether to show structure element bounding boxes
    pub show_structure_bounds: bool,
    /// Whether to label elements with their type
    pub label_elements: bool,
    /// Line width for bounding boxes
    pub line_width: f32,
    /// Colors for different element types
    pub colors: ElementColors,
    /// DPI for rendering
    pub dpi: u32,
}

impl Default for DebugOptions {
    fn default() -> Self {
        Self {
            show_text_bounds: true,
            show_image_bounds: true,
            show_path_bounds: true,
            show_table_bounds: true,
            show_structure_bounds: false,
            label_elements: false,
            line_width: 1.0,
            colors: ElementColors::default(),
            dpi: 150,
        }
    }
}

impl DebugOptions {
    /// Show only text bounds.
    pub fn text_only() -> Self {
        Self {
            show_text_bounds: true,
            show_image_bounds: false,
            show_path_bounds: false,
            show_table_bounds: false,
            show_structure_bounds: false,
            ..Default::default()
        }
    }

    /// Show all element types.
    pub fn all() -> Self {
        Self {
            show_text_bounds: true,
            show_image_bounds: true,
            show_path_bounds: true,
            show_table_bounds: true,
            show_structure_bounds: true,
            ..Default::default()
        }
    }
}

/// Debug visualizer for PDF pages.
pub struct DebugVisualizer {
    options: DebugOptions,
}

impl DebugVisualizer {
    /// Create a new debug visualizer with the given options.
    pub fn new(options: DebugOptions) -> Self {
        Self { options }
    }

    /// Render a debug visualization of a page.
    ///
    /// This renders the page with bounding boxes overlaid for each element type.
    ///
    /// Requires the "rendering" feature.
    #[cfg(feature = "rendering")]
    pub fn render_debug_page(&self, pdf: &mut Pdf, page: usize) -> Result<RenderedImage> {
        // First render the page normally
        let render_options = RenderOptions::with_dpi(self.options.dpi);
        let base_image = pdf.render_page_with_options(page, &render_options)?;

        // Get the page for element analysis
        let page_obj = pdf.page(page)?;

        // Create pixmap from rendered image
        let mut pixmap = self.load_pixmap_from_png(&base_image.data)?;

        // Calculate scale factor
        let scale = self.options.dpi as f32 / 72.0;

        // Overlay bounding boxes
        self.draw_element_bounds(&mut pixmap, &page_obj, scale)?;

        // Encode back to PNG
        let data = pixmap
            .encode_png()
            .map_err(|e| Error::InvalidPdf(format!("PNG encoding failed: {}", e)))?;

        Ok(RenderedImage {
            data,
            width: base_image.width,
            height: base_image.height,
            format: crate::rendering::ImageFormat::Png,
        })
    }

    /// Render a debug visualization and save to file.
    ///
    /// Requires the "rendering" feature.
    #[cfg(feature = "rendering")]
    pub fn render_debug_page_to_file(
        &self,
        pdf: &mut Pdf,
        page: usize,
        path: impl AsRef<std::path::Path>,
    ) -> Result<()> {
        let image = self.render_debug_page(pdf, page)?;
        image.save(path)
    }

    /// Load a pixmap from PNG data.
    #[cfg(feature = "rendering")]
    fn load_pixmap_from_png(&self, png_data: &[u8]) -> Result<Pixmap> {
        // Decode PNG using the image crate
        let img = image::load_from_memory(png_data)
            .map_err(|e| Error::InvalidPdf(format!("Failed to decode PNG: {}", e)))?;

        let rgba = img.to_rgba8();
        let width = rgba.width();
        let height = rgba.height();

        Pixmap::from_vec(rgba.into_raw(), tiny_skia::IntSize::from_wh(width, height).unwrap())
            .ok_or_else(|| Error::InvalidPdf("Failed to create pixmap".to_string()))
    }

    /// Draw element bounding boxes on the pixmap.
    #[cfg(feature = "rendering")]
    fn draw_element_bounds(&self, pixmap: &mut Pixmap, page: &PdfPage, scale: f32) -> Result<()> {
        let height = pixmap.height() as f32;

        // Transform to flip Y axis (PDF origin is bottom-left)
        let transform = Transform::from_scale(scale, -scale).post_translate(0.0, height);

        // Draw bounds for all child elements
        for element in page.children() {
            self.draw_element_bounds_recursive(pixmap, &element, transform)?;
        }

        Ok(())
    }

    /// Recursively draw bounds for an element and its children.
    #[cfg(feature = "rendering")]
    fn draw_element_bounds_recursive(
        &self,
        pixmap: &mut Pixmap,
        element: &PdfElement,
        transform: Transform,
    ) -> Result<()> {
        match element {
            PdfElement::Text(text) => {
                if self.options.show_text_bounds {
                    self.draw_rect(pixmap, &text.bbox(), &self.options.colors.text, transform);
                }
            },
            PdfElement::Image(image) => {
                if self.options.show_image_bounds {
                    self.draw_rect(pixmap, &image.bbox(), &self.options.colors.image, transform);
                }
            },
            PdfElement::Path(path) => {
                if self.options.show_path_bounds {
                    self.draw_rect(pixmap, &path.bbox(), &self.options.colors.path, transform);
                }
            },
            PdfElement::Table(table) => {
                if self.options.show_table_bounds {
                    self.draw_rect(pixmap, &table.bbox(), &self.options.colors.table, transform);
                }
            },
            PdfElement::Structure(structure) => {
                if self.options.show_structure_bounds {
                    self.draw_rect(
                        pixmap,
                        &structure.bbox(),
                        &self.options.colors.structure,
                        transform,
                    );
                }
                // Note: Structure children are not recursed as PdfStructure doesn't expose children
                // The structure bbox already encompasses all child elements
            },
        }

        Ok(())
    }

    /// Draw a rectangle outline on the pixmap.
    #[cfg(feature = "rendering")]
    fn draw_rect(&self, pixmap: &mut Pixmap, rect: &Rect, color: &[f32; 4], transform: Transform) {
        let mut paint = Paint::default();
        paint.set_color(
            Color::from_rgba(color[0], color[1], color[2], color[3]).unwrap_or(Color::BLACK),
        );
        paint.anti_alias = true;

        let stroke = Stroke {
            width: self.options.line_width,
            ..Stroke::default()
        };

        let mut path = PathBuilder::new();
        path.push_rect(
            tiny_skia::Rect::from_xywh(rect.x, rect.y, rect.width, rect.height)
                .unwrap_or(tiny_skia::Rect::from_xywh(0.0, 0.0, 1.0, 1.0).unwrap()),
        );

        if let Some(path) = path.finish() {
            pixmap.stroke_path(&path, &paint, &stroke, transform, None);
        }
    }

    /// Export page elements to JSON format.
    ///
    /// This produces a structured representation of all elements on the page.
    pub fn export_elements_json(&self, page: &PdfPage) -> Result<String> {
        let mut elements = Vec::new();

        for element in page.children() {
            elements.push(self.element_to_json(&element));
        }

        serde_json::to_string_pretty(&elements)
            .map_err(|e| Error::InvalidPdf(format!("JSON serialization failed: {}", e)))
    }

    /// Convert an element to a JSON-serializable structure.
    fn element_to_json(&self, element: &PdfElement) -> serde_json::Value {
        match element {
            PdfElement::Text(text) => {
                serde_json::json!({
                    "type": "text",
                    "content": text.text(),
                    "bbox": self.rect_to_json(&text.bbox()),
                })
            },
            PdfElement::Image(image) => {
                let (width, height) = image.dimensions();
                serde_json::json!({
                    "type": "image",
                    "width": width,
                    "height": height,
                    "bbox": self.rect_to_json(&image.bbox()),
                })
            },
            PdfElement::Path(path) => {
                serde_json::json!({
                    "type": "path",
                    "bbox": self.rect_to_json(&path.bbox()),
                })
            },
            PdfElement::Table(table) => {
                serde_json::json!({
                    "type": "table",
                    "bbox": self.rect_to_json(&table.bbox()),
                })
            },
            PdfElement::Structure(structure) => {
                serde_json::json!({
                    "type": "structure",
                    "structure_type": structure.structure_type(),
                    "bbox": self.rect_to_json(&structure.bbox()),
                })
            },
        }
    }

    /// Convert a Rect to JSON format.
    fn rect_to_json(&self, rect: &Rect) -> serde_json::Value {
        serde_json::json!({
            "x": rect.x,
            "y": rect.y,
            "width": rect.width,
            "height": rect.height,
        })
    }

    /// Export page elements to SVG format.
    ///
    /// This produces an SVG with element bounding boxes overlaid.
    pub fn export_elements_svg(
        &self,
        page: &PdfPage,
        page_width: f32,
        page_height: f32,
    ) -> Result<String> {
        let mut svg = format!(
            r#"<svg xmlns="http://www.w3.org/2000/svg" width="{}" height="{}" viewBox="0 0 {} {}">"#,
            page_width, page_height, page_width, page_height
        );

        // Add element rectangles
        for element in page.children() {
            self.element_to_svg(&mut svg, &element, page_height);
        }

        svg.push_str("</svg>");
        Ok(svg)
    }

    /// Add an element's SVG representation.
    fn element_to_svg(&self, svg: &mut String, element: &PdfElement, page_height: f32) {
        let (rect, color_name, stroke_color) = match element {
            PdfElement::Text(text) => {
                if !self.options.show_text_bounds {
                    return;
                }
                (text.bbox(), "text", self.color_to_svg(&self.options.colors.text))
            },
            PdfElement::Image(image) => {
                if !self.options.show_image_bounds {
                    return;
                }
                (image.bbox(), "image", self.color_to_svg(&self.options.colors.image))
            },
            PdfElement::Path(path) => {
                if !self.options.show_path_bounds {
                    return;
                }
                (path.bbox(), "path", self.color_to_svg(&self.options.colors.path))
            },
            PdfElement::Table(table) => {
                if !self.options.show_table_bounds {
                    return;
                }
                (table.bbox(), "table", self.color_to_svg(&self.options.colors.table))
            },
            PdfElement::Structure(structure) => {
                if self.options.show_structure_bounds {
                    let rect = structure.bbox();
                    let stroke = self.color_to_svg(&self.options.colors.structure);
                    // Flip Y for SVG (SVG origin is top-left)
                    let y = page_height - rect.y - rect.height;
                    svg.push_str(&format!(
                        r#"<rect class="structure" x="{}" y="{}" width="{}" height="{}" fill="none" stroke="{}" stroke-width="{}"/>"#,
                        rect.x, y, rect.width, rect.height, stroke, self.options.line_width
                    ));
                }
                // Note: Structure children are not recursed as PdfStructure doesn't expose children
                return;
            },
        };

        // Flip Y for SVG (SVG origin is top-left)
        let y = page_height - rect.y - rect.height;

        svg.push_str(&format!(
            r#"<rect class="{}" x="{}" y="{}" width="{}" height="{}" fill="none" stroke="{}" stroke-width="{}"/>"#,
            color_name, rect.x, y, rect.width, rect.height, stroke_color, self.options.line_width
        ));
    }

    /// Convert RGBA color to SVG rgba() format.
    fn color_to_svg(&self, color: &[f32; 4]) -> String {
        format!(
            "rgba({},{},{},{})",
            (color[0] * 255.0) as u8,
            (color[1] * 255.0) as u8,
            (color[2] * 255.0) as u8,
            color[3]
        )
    }
}

impl Default for DebugVisualizer {
    fn default() -> Self {
        Self::new(DebugOptions::default())
    }
}

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

    #[test]
    fn test_debug_options_default() {
        let opts = DebugOptions::default();
        assert!(opts.show_text_bounds);
        assert!(opts.show_image_bounds);
        assert!(opts.show_path_bounds);
        assert!(opts.show_table_bounds);
        assert!(!opts.show_structure_bounds);
        assert!(!opts.label_elements);
        assert_eq!(opts.dpi, 150);
    }

    #[test]
    fn test_debug_options_text_only() {
        let opts = DebugOptions::text_only();
        assert!(opts.show_text_bounds);
        assert!(!opts.show_image_bounds);
        assert!(!opts.show_path_bounds);
        assert!(!opts.show_table_bounds);
    }

    #[test]
    fn test_debug_options_all() {
        let opts = DebugOptions::all();
        assert!(opts.show_text_bounds);
        assert!(opts.show_image_bounds);
        assert!(opts.show_path_bounds);
        assert!(opts.show_table_bounds);
        assert!(opts.show_structure_bounds);
    }

    #[test]
    fn test_element_colors_default() {
        let colors = ElementColors::default();
        assert_eq!(colors.text[0], 1.0); // Red
        assert_eq!(colors.image[1], 1.0); // Green
        assert_eq!(colors.path[2], 1.0); // Blue
    }

    #[test]
    fn test_color_to_svg() {
        let visualizer = DebugVisualizer::default();
        let color = [1.0, 0.0, 0.0, 0.5];
        let svg_color = visualizer.color_to_svg(&color);
        assert_eq!(svg_color, "rgba(255,0,0,0.5)");
    }
}