pdf_oxide 0.3.23

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
//! Integration tests for the high-level PDF API.

use pdf_oxide::api::{Pdf, PdfBuilder, PdfConfig};
use pdf_oxide::writer::PageSize;
use tempfile::tempdir;

mod pdf_config_tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = PdfConfig::default();
        assert_eq!(config.margin_left, 72.0);
        assert_eq!(config.margin_right, 72.0);
        assert_eq!(config.margin_top, 72.0);
        assert_eq!(config.margin_bottom, 72.0);
        assert_eq!(config.font_size, 12.0);
        assert_eq!(config.line_height, 1.5);
        assert!(config.title.is_none());
        assert!(config.author.is_none());
    }
}

mod pdf_builder_tests {
    use super::*;

    #[test]
    fn test_builder_creates_pdf() {
        // Test that builder can create a PDF
        let result = PdfBuilder::new().from_text("Test");
        assert!(result.is_ok());
    }

    #[test]
    fn test_builder_with_title() {
        // Test builder with title creates valid PDF
        let result = PdfBuilder::new().title("Test Title").from_text("Content");
        assert!(result.is_ok());
    }

    #[test]
    fn test_builder_with_author() {
        let result = PdfBuilder::new().author("Test Author").from_text("Content");
        assert!(result.is_ok());
    }

    #[test]
    fn test_builder_with_subject() {
        let result = PdfBuilder::new()
            .subject("Test Subject")
            .from_text("Content");
        assert!(result.is_ok());
    }

    #[test]
    fn test_builder_with_keywords() {
        let result = PdfBuilder::new()
            .keywords("test, keywords")
            .from_text("Content");
        assert!(result.is_ok());
    }

    #[test]
    fn test_builder_with_page_size() {
        let result = PdfBuilder::new()
            .page_size(PageSize::A4)
            .from_text("A4 content");
        assert!(result.is_ok());
    }

    #[test]
    fn test_builder_with_margin() {
        let result = PdfBuilder::new().margin(50.0).from_text("Custom margin");
        assert!(result.is_ok());
    }

    #[test]
    fn test_builder_with_margins() {
        let result = PdfBuilder::new()
            .margins(10.0, 20.0, 30.0, 40.0)
            .from_text("Custom margins");
        assert!(result.is_ok());
    }

    #[test]
    fn test_builder_with_font_size() {
        let result = PdfBuilder::new()
            .font_size(14.0)
            .from_text("Custom font size");
        assert!(result.is_ok());
    }

    #[test]
    fn test_builder_with_line_height() {
        let result = PdfBuilder::new()
            .line_height(1.8)
            .from_text("Custom line height");
        assert!(result.is_ok());
    }

    #[test]
    fn test_builder_chain() {
        let result = PdfBuilder::new()
            .title("Title")
            .author("Author")
            .page_size(PageSize::Letter)
            .margin(72.0)
            .font_size(12.0)
            .from_text("Chained builder");

        assert!(result.is_ok());
        let pdf = result.unwrap();
        assert!(!pdf.as_bytes().is_empty());
    }
}

mod pdf_creation_tests {
    use super::*;

    #[test]
    fn test_from_text_simple() {
        let result = Pdf::from_text("Hello, World!");
        assert!(result.is_ok());

        let pdf = result.unwrap();
        assert!(!pdf.as_bytes().is_empty());
        assert!(pdf.as_bytes().starts_with(b"%PDF"));
    }

    #[test]
    fn test_from_text_multiline() {
        let result = Pdf::from_text("Line 1\nLine 2\nLine 3");
        assert!(result.is_ok());

        let pdf = result.unwrap();
        assert!(!pdf.as_bytes().is_empty());
    }

    #[test]
    fn test_from_markdown_heading() {
        let result = Pdf::from_markdown("# Heading 1\n\nSome text.");
        assert!(result.is_ok());

        let pdf = result.unwrap();
        assert!(!pdf.as_bytes().is_empty());
    }

    #[test]
    fn test_from_markdown_multiple_headings() {
        let markdown = r#"
# Chapter 1

Introduction text.

## Section 1.1

More content here.

### Subsection 1.1.1

Even more content.
"#;
        let result = Pdf::from_markdown(markdown);
        assert!(result.is_ok());
    }

    #[test]
    fn test_from_markdown_list() {
        let markdown = r#"
# Shopping List

- Apples
- Bananas
- Oranges
"#;
        let result = Pdf::from_markdown(markdown);
        assert!(result.is_ok());
    }

    #[test]
    fn test_from_markdown_blockquote() {
        let markdown = r#"
# Quote

> This is a quote.
> It spans multiple lines.
"#;
        let result = Pdf::from_markdown(markdown);
        assert!(result.is_ok());
    }

    #[test]
    fn test_from_markdown_code_block() {
        let markdown = r#"
# Code Example

```
fn main() {
    println!("Hello");
}
```
"#;
        let result = Pdf::from_markdown(markdown);
        assert!(result.is_ok());
    }

    #[test]
    fn test_from_html_simple() {
        let result = Pdf::from_html("<h1>Hello</h1><p>World</p>");
        assert!(result.is_ok());

        let pdf = result.unwrap();
        assert!(!pdf.as_bytes().is_empty());
    }

    #[test]
    fn test_from_html_formatting() {
        let html = "<h1>Title</h1><p>This is <b>bold</b> and <i>italic</i>.</p>";
        let result = Pdf::from_html(html);
        assert!(result.is_ok());
    }

    #[test]
    fn test_from_html_list() {
        let html = "<h1>List</h1><ul><li>Item 1</li><li>Item 2</li></ul>";
        let result = Pdf::from_html(html);
        assert!(result.is_ok());
    }

    #[test]
    fn test_into_bytes() {
        let pdf = Pdf::from_text("Test").unwrap();
        let bytes = pdf.into_bytes();

        assert!(!bytes.is_empty());
        assert!(bytes.starts_with(b"%PDF"));
    }

    #[test]
    fn test_config_access() {
        let pdf = PdfBuilder::new()
            .title("My Title")
            .from_text("Content")
            .unwrap();

        let config = pdf.config();
        assert_eq!(config.title, Some("My Title".to_string()));
    }
}

mod pdf_save_tests {
    use super::*;

    #[test]
    fn test_save_text_pdf() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("text.pdf");

        let mut pdf = Pdf::from_text("Hello, World!").unwrap();
        let result = pdf.save(&path);

        assert!(result.is_ok());
        assert!(path.exists());

        // Check file starts with PDF header
        let bytes = std::fs::read(&path).unwrap();
        assert!(bytes.starts_with(b"%PDF"));
    }

    #[test]
    fn test_save_markdown_pdf() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("markdown.pdf");

        let mut pdf = Pdf::from_markdown("# Hello\n\nWorld").unwrap();
        let result = pdf.save(&path);

        assert!(result.is_ok());
        assert!(path.exists());
    }

    #[test]
    fn test_save_html_pdf() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("html.pdf");

        let mut pdf = Pdf::from_html("<h1>Hello</h1>").unwrap();
        let result = pdf.save(&path);

        assert!(result.is_ok());
        assert!(path.exists());
    }

    #[test]
    fn test_save_with_metadata() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("metadata.pdf");

        let mut pdf = PdfBuilder::new()
            .title("Test Document")
            .author("Test Author")
            .subject("Testing")
            .from_text("Content")
            .unwrap();

        let result = pdf.save(&path);
        assert!(result.is_ok());
        assert!(path.exists());
    }
}

mod builder_to_pdf_tests {
    use super::*;

    #[test]
    fn test_builder_from_text() {
        let pdf = PdfBuilder::new()
            .title("Plain Text")
            .from_text("Plain text content")
            .unwrap();

        assert!(!pdf.as_bytes().is_empty());
    }

    #[test]
    fn test_builder_from_markdown() {
        let pdf = PdfBuilder::new()
            .title("Markdown Doc")
            .author("Author")
            .page_size(PageSize::A4)
            .from_markdown("# Title\n\nContent")
            .unwrap();

        assert!(!pdf.as_bytes().is_empty());
    }

    #[test]
    fn test_builder_from_html() {
        let pdf = PdfBuilder::new()
            .title("HTML Doc")
            .margin(50.0)
            .from_html("<h1>Title</h1><p>Content</p>")
            .unwrap();

        assert!(!pdf.as_bytes().is_empty());
    }

    #[test]
    fn test_builder_custom_fonts() {
        let pdf = PdfBuilder::new()
            .font_size(14.0)
            .line_height(1.6)
            .from_text("Custom font settings")
            .unwrap();

        assert!(!pdf.as_bytes().is_empty());
    }

    #[test]
    fn test_builder_custom_margins() {
        let pdf = PdfBuilder::new()
            .margins(36.0, 36.0, 72.0, 72.0)
            .from_text("Custom margins")
            .unwrap();

        assert!(!pdf.as_bytes().is_empty());
    }
}

mod integration_tests {
    use super::*;
    use pdf_oxide::editor::{DocumentEditor, EditableDocument};

    #[test]
    fn test_create_and_open() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("roundtrip.pdf");

        // Create PDF
        let mut pdf = Pdf::from_markdown("# Test Document\n\nContent here.").unwrap();
        pdf.save(&path).unwrap();

        // Open with editor
        let editor = DocumentEditor::open(&path);
        assert!(editor.is_ok());

        let mut editor = editor.unwrap();
        assert!(editor.page_count().unwrap() >= 1);
    }

    #[test]
    fn test_full_workflow() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("workflow.pdf");

        // Build with full options
        let mut pdf = PdfBuilder::new()
            .title("Complete Document")
            .author("Integration Test")
            .subject("Testing all features")
            .keywords("test, integration, pdf")
            .page_size(PageSize::Letter)
            .margin(72.0)
            .font_size(12.0)
            .line_height(1.5)
            .from_markdown(
                r#"
# Introduction

This is a complete test document.

## Features

- Headings
- Lists
- Text formatting

## Code

```
let x = 42;
```

> And a quote for good measure.
"#,
            )
            .unwrap();

        // Save
        pdf.save(&path).unwrap();

        // Verify
        assert!(path.exists());
        let bytes = std::fs::read(&path).unwrap();
        assert!(bytes.starts_with(b"%PDF"));
        assert!(bytes.len() > 100);
    }
}