pdf-oxide 0.3.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/README.md +218 -0
  2. package/binding.gyp +35 -0
  3. package/package.json +78 -0
  4. package/src/builders/annotation-builder.ts +367 -0
  5. package/src/builders/conversion-options-builder.ts +257 -0
  6. package/src/builders/index.ts +12 -0
  7. package/src/builders/metadata-builder.ts +317 -0
  8. package/src/builders/pdf-builder.ts +386 -0
  9. package/src/builders/search-options-builder.ts +151 -0
  10. package/src/document-editor-manager.ts +318 -0
  11. package/src/errors.ts +1629 -0
  12. package/src/form-field-manager.ts +666 -0
  13. package/src/hybrid-ml-manager.ts +283 -0
  14. package/src/index.ts +453 -0
  15. package/src/managers/accessibility-manager.ts +338 -0
  16. package/src/managers/annotation-manager.ts +439 -0
  17. package/src/managers/barcode-manager.ts +235 -0
  18. package/src/managers/batch-manager.ts +533 -0
  19. package/src/managers/cache-manager.ts +486 -0
  20. package/src/managers/compliance-manager.ts +375 -0
  21. package/src/managers/content-manager.ts +339 -0
  22. package/src/managers/document-utility-manager.ts +922 -0
  23. package/src/managers/dom-pdf-creator.ts +365 -0
  24. package/src/managers/editing-manager.ts +514 -0
  25. package/src/managers/enterprise-manager.ts +478 -0
  26. package/src/managers/extended-managers.ts +437 -0
  27. package/src/managers/extraction-manager.ts +583 -0
  28. package/src/managers/final-utilities.ts +429 -0
  29. package/src/managers/hybrid-ml-advanced.ts +479 -0
  30. package/src/managers/index.ts +239 -0
  31. package/src/managers/layer-manager.ts +500 -0
  32. package/src/managers/metadata-manager.ts +303 -0
  33. package/src/managers/ocr-manager.ts +756 -0
  34. package/src/managers/optimization-manager.ts +262 -0
  35. package/src/managers/outline-manager.ts +196 -0
  36. package/src/managers/page-manager.ts +289 -0
  37. package/src/managers/pattern-detection.ts +440 -0
  38. package/src/managers/rendering-manager.ts +863 -0
  39. package/src/managers/search-manager.ts +385 -0
  40. package/src/managers/security-manager.ts +345 -0
  41. package/src/managers/signature-manager.ts +1664 -0
  42. package/src/managers/streams.ts +618 -0
  43. package/src/managers/xfa-manager.ts +500 -0
  44. package/src/pdf-creator-manager.ts +494 -0
  45. package/src/properties.ts +522 -0
  46. package/src/result-accessors-manager.ts +867 -0
  47. package/src/tests/advanced-features.test.ts +414 -0
  48. package/src/tests/advanced.test.ts +266 -0
  49. package/src/tests/extended-managers.test.ts +316 -0
  50. package/src/tests/final-utilities.test.ts +455 -0
  51. package/src/tests/foundation.test.ts +315 -0
  52. package/src/tests/high-demand.test.ts +257 -0
  53. package/src/tests/specialized.test.ts +97 -0
  54. package/src/thumbnail-manager.ts +272 -0
  55. package/src/types/common.ts +142 -0
  56. package/src/types/document-types.ts +457 -0
  57. package/src/types/index.ts +6 -0
  58. package/src/types/manager-types.ts +284 -0
  59. package/src/types/native-bindings.ts +517 -0
  60. package/src/workers/index.ts +7 -0
  61. package/src/workers/pool.ts +274 -0
  62. package/src/workers/worker.ts +131 -0
@@ -0,0 +1,522 @@
1
+ /**
2
+ * PDF Oxide Property Getters for Node.js
3
+ *
4
+ * Adds JavaScript property getters to native classes for idiomatic API.
5
+ * Converts Java-style getters (getPageCount()) to JavaScript properties (pageCount).
6
+ * Caches results where appropriate for performance.
7
+ */
8
+
9
+ import type { DocumentInfo, Metadata, Rect } from './types/common';
10
+
11
+ /**
12
+ * Cache structure for PdfDocument properties
13
+ */
14
+ interface DocumentPropertyCache {
15
+ version?: [number, number];
16
+ pageCount?: number;
17
+ documentInfo?: DocumentInfo;
18
+ metadata?: Metadata;
19
+ forms?: any;
20
+ pageLabels?: string[];
21
+ embeddedFiles?: any[];
22
+ }
23
+
24
+ /**
25
+ * Cache structure for Pdf properties
26
+ */
27
+ interface PdfPropertyCache {
28
+ version?: [number, number];
29
+ pageCount?: number;
30
+ documentInfo?: DocumentInfo;
31
+ metadata?: Metadata;
32
+ forms?: any;
33
+ }
34
+
35
+ /**
36
+ * Cache structure for PdfPage properties
37
+ */
38
+ interface PagePropertyCache {
39
+ bounds?: Rect;
40
+ }
41
+
42
+ /**
43
+ * Adds property getters to PdfDocument class
44
+ * Converts methods to idiomatic JavaScript properties
45
+ *
46
+ * @param PdfDocument - The native PdfDocument class
47
+ * @returns Enhanced PdfDocument class with property getters
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * const { PdfDocument } = require('pdf_oxide');
52
+ * const doc = PdfDocument.open('file.pdf');
53
+ * console.log(doc.pageCount); // Instead of doc.get_page_count()
54
+ * console.log(doc.version); // Instead of doc.get_version()
55
+ * ```
56
+ */
57
+ export function addPdfDocumentProperties<T extends { prototype: any }>(
58
+ PdfDocument: T
59
+ ): T {
60
+ if (!PdfDocument || !PdfDocument.prototype) {
61
+ return PdfDocument;
62
+ }
63
+
64
+ const prototype = PdfDocument.prototype;
65
+
66
+ // Cache for frequently accessed properties
67
+ const cache = new WeakMap<any, DocumentPropertyCache>();
68
+
69
+ /**
70
+ * Gets the PDF version as [major, minor]
71
+ * Caches result for performance
72
+ */
73
+ Object.defineProperty(prototype, 'version', {
74
+ get(this: any) {
75
+ if (!cache.has(this)) {
76
+ cache.set(this, {});
77
+ }
78
+ const obj = cache.get(this)!;
79
+
80
+ if (!obj.version) {
81
+ obj.version = this.get_version ? this.get_version() : [1, 4];
82
+ }
83
+ return obj.version;
84
+ },
85
+ configurable: true,
86
+ enumerable: true,
87
+ });
88
+
89
+ /**
90
+ * Gets the number of pages
91
+ * Caches result for performance
92
+ */
93
+ Object.defineProperty(prototype, 'pageCount', {
94
+ get(this: any) {
95
+ if (!cache.has(this)) {
96
+ cache.set(this, {});
97
+ }
98
+ const obj = cache.get(this)!;
99
+
100
+ if (obj.pageCount === undefined) {
101
+ try {
102
+ obj.pageCount = this.get_page_count ? this.get_page_count() : 0;
103
+ } catch (err) {
104
+ throw err;
105
+ }
106
+ }
107
+ return obj.pageCount;
108
+ },
109
+ configurable: true,
110
+ enumerable: true,
111
+ });
112
+
113
+ /**
114
+ * Checks if document has structure tree (Tagged PDF)
115
+ */
116
+ Object.defineProperty(prototype, 'hasStructureTree', {
117
+ get(this: any) {
118
+ return this.has_structure_tree ? this.has_structure_tree() : false;
119
+ },
120
+ configurable: true,
121
+ enumerable: true,
122
+ });
123
+
124
+ /**
125
+ * Gets document information metadata
126
+ */
127
+ Object.defineProperty(prototype, 'documentInfo', {
128
+ get(this: any) {
129
+ if (!cache.has(this)) {
130
+ cache.set(this, {});
131
+ }
132
+ const obj = cache.get(this)!;
133
+
134
+ if (!obj.documentInfo) {
135
+ try {
136
+ obj.documentInfo = this.get_document_info ? this.get_document_info() : {};
137
+ } catch (err) {
138
+ throw err;
139
+ }
140
+ }
141
+ return obj.documentInfo;
142
+ },
143
+ configurable: true,
144
+ enumerable: true,
145
+ });
146
+
147
+ /**
148
+ * Gets document XMP metadata
149
+ */
150
+ Object.defineProperty(prototype, 'metadata', {
151
+ get(this: any) {
152
+ if (!cache.has(this)) {
153
+ cache.set(this, {});
154
+ }
155
+ const obj = cache.get(this)!;
156
+
157
+ if (!obj.metadata) {
158
+ try {
159
+ obj.metadata = this.get_metadata ? this.get_metadata() : {};
160
+ } catch (err) {
161
+ throw err;
162
+ }
163
+ }
164
+ return obj.metadata;
165
+ },
166
+ configurable: true,
167
+ enumerable: true,
168
+ });
169
+
170
+ /**
171
+ * Gets document forms (AcroForm)
172
+ */
173
+ Object.defineProperty(prototype, 'forms', {
174
+ get(this: any) {
175
+ if (!cache.has(this)) {
176
+ cache.set(this, {});
177
+ }
178
+ const obj = cache.get(this)!;
179
+
180
+ if (!obj.forms) {
181
+ try {
182
+ obj.forms = this.get_forms ? this.get_forms() : null;
183
+ } catch (err) {
184
+ throw err;
185
+ }
186
+ }
187
+ return obj.forms;
188
+ },
189
+ configurable: true,
190
+ enumerable: true,
191
+ });
192
+
193
+ /**
194
+ * Gets page labels
195
+ */
196
+ Object.defineProperty(prototype, 'pageLabels', {
197
+ get(this: any) {
198
+ if (!cache.has(this)) {
199
+ cache.set(this, {});
200
+ }
201
+ const obj = cache.get(this)!;
202
+
203
+ if (!obj.pageLabels) {
204
+ try {
205
+ obj.pageLabels = this.get_page_labels ? this.get_page_labels() : [];
206
+ } catch (err) {
207
+ throw err;
208
+ }
209
+ }
210
+ return obj.pageLabels;
211
+ },
212
+ configurable: true,
213
+ enumerable: true,
214
+ });
215
+
216
+ /**
217
+ * Gets embedded files
218
+ */
219
+ Object.defineProperty(prototype, 'embeddedFiles', {
220
+ get(this: any) {
221
+ if (!cache.has(this)) {
222
+ cache.set(this, {});
223
+ }
224
+ const obj = cache.get(this)!;
225
+
226
+ if (!obj.embeddedFiles) {
227
+ try {
228
+ obj.embeddedFiles = this.get_embedded_files ? this.get_embedded_files() : [];
229
+ } catch (err) {
230
+ throw err;
231
+ }
232
+ }
233
+ return obj.embeddedFiles;
234
+ },
235
+ configurable: true,
236
+ enumerable: true,
237
+ });
238
+
239
+ return PdfDocument;
240
+ }
241
+
242
+ /**
243
+ * Adds property getters to Pdf class
244
+ *
245
+ * @param Pdf - The native Pdf class
246
+ * @returns Enhanced Pdf class with property getters
247
+ *
248
+ * @example
249
+ * ```typescript
250
+ * const { Pdf } = require('pdf_oxide');
251
+ * const doc = Pdf.fromMarkdown('# Hello');
252
+ * console.log(doc.pageCount); // Instead of doc.get_page_count()
253
+ * console.log(doc.version); // Instead of doc.get_version()
254
+ * ```
255
+ */
256
+ export function addPdfProperties<T extends { prototype: any }>(Pdf: T): T {
257
+ if (!Pdf || !Pdf.prototype) {
258
+ return Pdf;
259
+ }
260
+
261
+ const prototype = Pdf.prototype;
262
+ const cache = new WeakMap<any, PdfPropertyCache>();
263
+
264
+ /**
265
+ * Gets the PDF version as [major, minor]
266
+ */
267
+ Object.defineProperty(prototype, 'version', {
268
+ get(this: any) {
269
+ if (!cache.has(this)) {
270
+ cache.set(this, {});
271
+ }
272
+ const obj = cache.get(this)!;
273
+
274
+ if (!obj.version) {
275
+ obj.version = this.get_version ? this.get_version() : [1, 4];
276
+ }
277
+ return obj.version;
278
+ },
279
+ configurable: true,
280
+ enumerable: true,
281
+ });
282
+
283
+ /**
284
+ * Gets the number of pages
285
+ */
286
+ Object.defineProperty(prototype, 'pageCount', {
287
+ get(this: any) {
288
+ if (!cache.has(this)) {
289
+ cache.set(this, {});
290
+ }
291
+ const obj = cache.get(this)!;
292
+
293
+ if (obj.pageCount === undefined) {
294
+ try {
295
+ obj.pageCount = this.get_page_count ? this.get_page_count() : 0;
296
+ } catch (err) {
297
+ throw err;
298
+ }
299
+ }
300
+ return obj.pageCount;
301
+ },
302
+ configurable: true,
303
+ enumerable: true,
304
+ });
305
+
306
+ /**
307
+ * Gets document information metadata
308
+ */
309
+ Object.defineProperty(prototype, 'documentInfo', {
310
+ get(this: any) {
311
+ if (!cache.has(this)) {
312
+ cache.set(this, {});
313
+ }
314
+ const obj = cache.get(this)!;
315
+
316
+ if (!obj.documentInfo) {
317
+ try {
318
+ obj.documentInfo = this.get_document_info ? this.get_document_info() : {};
319
+ } catch (err) {
320
+ throw err;
321
+ }
322
+ }
323
+ return obj.documentInfo;
324
+ },
325
+ configurable: true,
326
+ enumerable: true,
327
+ });
328
+
329
+ /**
330
+ * Gets document XMP metadata
331
+ */
332
+ Object.defineProperty(prototype, 'metadata', {
333
+ get(this: any) {
334
+ if (!cache.has(this)) {
335
+ cache.set(this, {});
336
+ }
337
+ const obj = cache.get(this)!;
338
+
339
+ if (!obj.metadata) {
340
+ try {
341
+ obj.metadata = this.get_metadata ? this.get_metadata() : {};
342
+ } catch (err) {
343
+ throw err;
344
+ }
345
+ }
346
+ return obj.metadata;
347
+ },
348
+ configurable: true,
349
+ enumerable: true,
350
+ });
351
+
352
+ /**
353
+ * Gets document forms
354
+ */
355
+ Object.defineProperty(prototype, 'forms', {
356
+ get(this: any) {
357
+ if (!cache.has(this)) {
358
+ cache.set(this, {});
359
+ }
360
+ const obj = cache.get(this)!;
361
+
362
+ if (!obj.forms) {
363
+ try {
364
+ obj.forms = this.get_forms ? this.get_forms() : null;
365
+ } catch (err) {
366
+ throw err;
367
+ }
368
+ }
369
+ return obj.forms;
370
+ },
371
+ configurable: true,
372
+ enumerable: true,
373
+ });
374
+
375
+ return Pdf;
376
+ }
377
+
378
+ /**
379
+ * Adds property getters to PdfPage class
380
+ *
381
+ * @param PdfPage - The native PdfPage class
382
+ * @returns Enhanced PdfPage class with property getters
383
+ *
384
+ * @example
385
+ * ```typescript
386
+ * const page = doc.page(0);
387
+ * console.log(page.width); // Instead of page.get_width()
388
+ * console.log(page.height); // Instead of page.get_height()
389
+ * console.log(page.pageIndex); // Instead of page.get_page_index()
390
+ * ```
391
+ */
392
+ export function addPdfPageProperties<T extends { prototype: any }>(PdfPage: T): T {
393
+ if (!PdfPage || !PdfPage.prototype) {
394
+ return PdfPage;
395
+ }
396
+
397
+ const prototype = PdfPage.prototype;
398
+ const cache = new WeakMap<any, PagePropertyCache>();
399
+
400
+ /**
401
+ * Gets the page index (zero-based)
402
+ */
403
+ Object.defineProperty(prototype, 'pageIndex', {
404
+ get(this: any) {
405
+ return this.get_page_index ? this.get_page_index() : 0;
406
+ },
407
+ configurable: true,
408
+ enumerable: true,
409
+ });
410
+
411
+ /**
412
+ * Gets the page width in points
413
+ */
414
+ Object.defineProperty(prototype, 'width', {
415
+ get(this: any) {
416
+ return this.get_width ? this.get_width() : 612.0;
417
+ },
418
+ configurable: true,
419
+ enumerable: true,
420
+ });
421
+
422
+ /**
423
+ * Gets the page height in points
424
+ */
425
+ Object.defineProperty(prototype, 'height', {
426
+ get(this: any) {
427
+ return this.get_height ? this.get_height() : 792.0;
428
+ },
429
+ configurable: true,
430
+ enumerable: true,
431
+ });
432
+
433
+ /**
434
+ * Gets the page bounds as a Rect
435
+ */
436
+ Object.defineProperty(prototype, 'bounds', {
437
+ get(this: any) {
438
+ if (!cache.has(this)) {
439
+ cache.set(this, {});
440
+ }
441
+ const obj = cache.get(this)!;
442
+
443
+ if (!obj.bounds) {
444
+ if (this.get_bounds) {
445
+ obj.bounds = this.get_bounds();
446
+ } else {
447
+ // Return default bounds [0, 0, width, height]
448
+ obj.bounds = {
449
+ x: 0,
450
+ y: 0,
451
+ width: this.width || 612.0,
452
+ height: this.height || 792.0,
453
+ };
454
+ }
455
+ }
456
+ return obj.bounds;
457
+ },
458
+ configurable: true,
459
+ enumerable: true,
460
+ });
461
+
462
+ /**
463
+ * Gets the page orientation (portrait or landscape)
464
+ */
465
+ Object.defineProperty(prototype, 'orientation', {
466
+ get(this: any) {
467
+ const width = this.width || 612.0;
468
+ const height = this.height || 792.0;
469
+ return height >= width ? 'portrait' : 'landscape';
470
+ },
471
+ configurable: true,
472
+ enumerable: true,
473
+ });
474
+
475
+ /**
476
+ * Gets the page aspect ratio (width / height)
477
+ */
478
+ Object.defineProperty(prototype, 'aspectRatio', {
479
+ get(this: any) {
480
+ const width = this.width || 612.0;
481
+ const height = this.height || 792.0;
482
+ return width / height;
483
+ },
484
+ configurable: true,
485
+ enumerable: true,
486
+ });
487
+
488
+ return PdfPage;
489
+ }
490
+
491
+ /**
492
+ * Adds property getters to all supported classes
493
+ *
494
+ * @param classes - Object with class references
495
+ * @returns Object with enhanced classes
496
+ *
497
+ * @example
498
+ * ```typescript
499
+ * const classes = {
500
+ * PdfDocument,
501
+ * Pdf,
502
+ * PdfPage,
503
+ * PdfElement,
504
+ * PdfText,
505
+ * PdfImage,
506
+ * };
507
+ * const enhanced = addPropertiesToAll(classes);
508
+ * ```
509
+ */
510
+ export function addPropertiesToAll(classes: Record<string, any>): Record<string, any> {
511
+ if (classes.PdfDocument) {
512
+ classes.PdfDocument = addPdfDocumentProperties(classes.PdfDocument);
513
+ }
514
+ if (classes.Pdf) {
515
+ classes.Pdf = addPdfProperties(classes.Pdf);
516
+ }
517
+ if (classes.PdfPage) {
518
+ classes.PdfPage = addPdfPageProperties(classes.PdfPage);
519
+ }
520
+
521
+ return classes;
522
+ }