pdf_oxide 0.3.24

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
import { describe, it } from 'node:test';
import assert from 'node:assert';
import {
  addPdfDocumentProperties,
  addPdfProperties,
  addPdfPageProperties,
} from '../lib/properties.js';

describe('Property Getters - Phase 2.3', () => {
  describe('PdfDocument properties', () => {
    it('should add version property getter', () => {
      // Mock object with get_version method
      const MockDocument = class {
        get_version() {
          return [1, 7];
        }
      };

      addPdfDocumentProperties(MockDocument);
      const doc = new MockDocument();

      // Test property access
      assert.deepStrictEqual(doc.version, [1, 7]);

      // Test that property is on the prototype
      assert.ok(Object.getOwnPropertyDescriptor(MockDocument.prototype, 'version'));
    });

    it('should add pageCount property getter', () => {
      const MockDocument = class {
        get_page_count() {
          return 42;
        }
      };

      addPdfDocumentProperties(MockDocument);
      const doc = new MockDocument();

      assert.strictEqual(doc.pageCount, 42);
    });

    it('should add hasStructureTree property getter', () => {
      const MockDocument = class {
        has_structure_tree() {
          return true;
        }
      };

      addPdfDocumentProperties(MockDocument);
      const doc = new MockDocument();

      assert.strictEqual(doc.hasStructureTree, true);
    });

    it('should add documentInfo property getter with caching', () => {
      let callCount = 0;
      const MockDocument = class {
        get_document_info() {
          callCount++;
          return { title: 'Test' };
        }
      };

      addPdfDocumentProperties(MockDocument);
      const doc = new MockDocument();

      // First access
      const info1 = doc.documentInfo;
      assert.deepStrictEqual(info1, { title: 'Test' });
      assert.strictEqual(callCount, 1);

      // Second access should use cache
      const info2 = doc.documentInfo;
      assert.strictEqual(callCount, 1); // Not called again
      assert.strictEqual(info1, info2); // Same reference
    });

    it('should add metadata property getter with caching', () => {
      let callCount = 0;
      const MockDocument = class {
        get_metadata() {
          callCount++;
          return { format: 'xmp' };
        }
      };

      addPdfDocumentProperties(MockDocument);
      const doc = new MockDocument();

      const meta1 = doc.metadata;
      assert.deepStrictEqual(meta1, { format: 'xmp' });
      const meta2 = doc.metadata;
      assert.strictEqual(callCount, 1); // Cached
    });

    it('should add forms property getter', () => {
      const MockDocument = class {
        get_forms() {
          return { fields: [] };
        }
      };

      addPdfDocumentProperties(MockDocument);
      const doc = new MockDocument();

      assert.deepStrictEqual(doc.forms, { fields: [] });
    });

    it('should add pageLabels property getter', () => {
      const MockDocument = class {
        get_page_labels() {
          return ['i', 'ii', 'iii', '1', '2'];
        }
      };

      addPdfDocumentProperties(MockDocument);
      const doc = new MockDocument();

      assert.deepStrictEqual(doc.pageLabels, ['i', 'ii', 'iii', '1', '2']);
    });

    it('should add embeddedFiles property getter', () => {
      const MockDocument = class {
        get_embedded_files() {
          return [{ name: 'file.txt' }];
        }
      };

      addPdfDocumentProperties(MockDocument);
      const doc = new MockDocument();

      assert.deepStrictEqual(doc.embeddedFiles, [{ name: 'file.txt' }]);
    });

    it('should handle missing methods gracefully', () => {
      const MockDocument = class {};

      addPdfDocumentProperties(MockDocument);
      const doc = new MockDocument();

      // Should return defaults when methods don't exist
      // version returns [1, 4] as default
      assert.deepStrictEqual(doc.version, [1, 4]);
      assert.strictEqual(doc.pageCount, 0);
      assert.strictEqual(doc.hasStructureTree, false);
    });

    it('should be enumerable and configurable', () => {
      const MockDocument = class {
        get_version() {
          return [1, 4];
        }
      };

      addPdfDocumentProperties(MockDocument);

      const descriptor = Object.getOwnPropertyDescriptor(
        MockDocument.prototype,
        'version'
      );
      assert.strictEqual(descriptor.enumerable, true);
      assert.strictEqual(descriptor.configurable, true);
      assert.ok(!descriptor.writable); // Getter only
    });
  });

  describe('Pdf properties', () => {
    it('should add version property getter', () => {
      const MockPdf = class {
        get_version() {
          return [1, 5];
        }
      };

      addPdfProperties(MockPdf);
      const pdf = new MockPdf();

      assert.deepStrictEqual(pdf.version, [1, 5]);
    });

    it('should add pageCount property getter', () => {
      const MockPdf = class {
        get_page_count() {
          return 10;
        }
      };

      addPdfProperties(MockPdf);
      const pdf = new MockPdf();

      assert.strictEqual(pdf.pageCount, 10);
    });

    it('should add documentInfo property getter with caching', () => {
      let callCount = 0;
      const MockPdf = class {
        get_document_info() {
          callCount++;
          return { title: 'PDF Title' };
        }
      };

      addPdfProperties(MockPdf);
      const pdf = new MockPdf();

      const info1 = pdf.documentInfo;
      const info2 = pdf.documentInfo;
      assert.strictEqual(callCount, 1); // Cached
    });

    it('should add metadata property getter', () => {
      const MockPdf = class {
        get_metadata() {
          return { author: 'Test Author' };
        }
      };

      addPdfProperties(MockPdf);
      const pdf = new MockPdf();

      assert.deepStrictEqual(pdf.metadata, { author: 'Test Author' });
    });

    it('should add forms property getter', () => {
      const MockPdf = class {
        get_forms() {
          return null;
        }
      };

      addPdfProperties(MockPdf);
      const pdf = new MockPdf();

      assert.strictEqual(pdf.forms, null);
    });
  });

  describe('PdfPage properties', () => {
    it('should add pageIndex property getter', () => {
      const MockPage = class {
        get_page_index() {
          return 5;
        }
      };

      addPdfPageProperties(MockPage);
      const page = new MockPage();

      assert.strictEqual(page.pageIndex, 5);
    });

    it('should add width property getter', () => {
      const MockPage = class {
        get_width() {
          return 612.0;
        }
      };

      addPdfPageProperties(MockPage);
      const page = new MockPage();

      assert.strictEqual(page.width, 612.0);
    });

    it('should add height property getter', () => {
      const MockPage = class {
        get_height() {
          return 792.0;
        }
      };

      addPdfPageProperties(MockPage);
      const page = new MockPage();

      assert.strictEqual(page.height, 792.0);
    });

    it('should add bounds property getter', () => {
      const MockPage = class {
        get_width() {
          return 612.0;
        }
        get_height() {
          return 792.0;
        }
        get_bounds() {
          return { x: 0, y: 0, width: 612.0, height: 792.0 };
        }
      };

      addPdfPageProperties(MockPage);
      const page = new MockPage();

      const bounds = page.bounds;
      assert.strictEqual(bounds.x, 0);
      assert.strictEqual(bounds.y, 0);
      assert.strictEqual(bounds.width, 612.0);
      assert.strictEqual(bounds.height, 792.0);
    });

    it('should compute orientation from dimensions', () => {
      const PortraitPage = class {
        get_width() {
          return 612.0;
        }
        get_height() {
          return 792.0;
        }
      };

      const LandscapePage = class {
        get_width() {
          return 792.0;
        }
        get_height() {
          return 612.0;
        }
      };

      addPdfPageProperties(PortraitPage);
      addPdfPageProperties(LandscapePage);

      const portrait = new PortraitPage();
      const landscape = new LandscapePage();

      assert.strictEqual(portrait.orientation, 'portrait');
      assert.strictEqual(landscape.orientation, 'landscape');
    });

    it('should compute aspect ratio', () => {
      const MockPage = class {
        get_width() {
          return 800.0;
        }
        get_height() {
          return 600.0;
        }
      };

      addPdfPageProperties(MockPage);
      const page = new MockPage();

      // Aspect ratio should be width / height = 800 / 600 = 1.333...
      assert.strictEqual(page.aspectRatio, 800.0 / 600.0);
    });

    it('should handle missing methods gracefully', () => {
      const MockPage = class {};

      addPdfPageProperties(MockPage);
      const page = new MockPage();

      // Should return defaults
      assert.strictEqual(page.pageIndex, 0);
      assert.strictEqual(page.width, 612.0);
      assert.strictEqual(page.height, 792.0);
      assert.strictEqual(page.orientation, 'portrait'); // 612 < 792
    });

    it('should be enumerable properties', () => {
      const MockPage = class {
        get_width() {
          return 612.0;
        }
      };

      addPdfPageProperties(MockPage);

      const descriptor = Object.getOwnPropertyDescriptor(MockPage.prototype, 'width');
      assert.strictEqual(descriptor.enumerable, true);
      assert.strictEqual(descriptor.configurable, true);
    });
  });

  describe('Property getter integration', () => {
    it('should work with multiple instances independently', () => {
      const MockDocument = class {
        constructor(pages) {
          this._pages = pages;
        }

        get_page_count() {
          return this._pages;
        }
      };

      addPdfDocumentProperties(MockDocument);

      const doc1 = new MockDocument(10);
      const doc2 = new MockDocument(20);

      assert.strictEqual(doc1.pageCount, 10);
      assert.strictEqual(doc2.pageCount, 20);
    });

    it('should not interfere with other properties', () => {
      const MockDocument = class {
        constructor(title) {
          this.title = title;
        }

        get_version() {
          return [1, 4];
        }
      };

      addPdfDocumentProperties(MockDocument);

      const doc = new MockDocument('My PDF');

      // Both own and getter properties should work
      assert.strictEqual(doc.title, 'My PDF');
      assert.deepStrictEqual(doc.version, [1, 4]);
    });
  });

  describe('Edge cases', () => {
    it('should handle null/undefined gracefully', () => {
      const MockDocument = class {
        get_version() {
          return null;
        }
      };

      addPdfDocumentProperties(MockDocument);
      const doc = new MockDocument();

      assert.strictEqual(doc.version, null);
    });

    it('should handle error cases in getters', () => {
      const MockDocument = class {
        get_page_count() {
          throw new Error('Test error');
        }
      };

      addPdfDocumentProperties(MockDocument);
      const doc = new MockDocument();

      // Accessing the property should throw the error
      assert.throws(
        () => {
          const _ = doc.pageCount;
        },
        (err) => err.message === 'Test error'
      );
    });

    it('should work with inherited classes', () => {
      class BaseMockDocument {
        get_version() {
          return [1, 7];
        }
      }

      class DerivedDocument extends BaseMockDocument {}

      addPdfDocumentProperties(DerivedDocument);
      const doc = new DerivedDocument();

      // Should work through inheritance
      assert.deepStrictEqual(doc.version, [1, 7]);
    });
  });
});