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,345 @@
1
+ /**
2
+ * Manager for PDF document security and encryption
3
+ *
4
+ * Provides methods to check document encryption status, access permissions,
5
+ * and security properties.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { SecurityManager } from 'pdf_oxide';
10
+ *
11
+ * const doc = PdfDocument.open('document.pdf');
12
+ * const securityManager = new SecurityManager(doc);
13
+ *
14
+ * if (securityManager.isEncrypted()) {
15
+ * console.log('Document is encrypted');
16
+ * console.log(`Can print: ${securityManager.canPrint()}`);
17
+ * console.log(`Can modify: ${securityManager.canModify()}`);
18
+ * }
19
+ * ```
20
+ */
21
+
22
+ export interface PermissionsSummary {
23
+ canPrint: boolean;
24
+ canCopy: boolean;
25
+ canModify: boolean;
26
+ canAnnotate: boolean;
27
+ canFillForms: boolean;
28
+ isViewOnly: boolean;
29
+ isEncrypted: boolean;
30
+ requiresPassword: boolean;
31
+ encryptionAlgorithm: string | null;
32
+ }
33
+
34
+ export interface SecurityLevel {
35
+ level: 'high' | 'medium' | 'low' | 'none';
36
+ description: string;
37
+ isEncrypted: boolean;
38
+ algorithm: string | null;
39
+ restrictedAccess: boolean;
40
+ }
41
+
42
+ export interface AccessibilityValidation {
43
+ canExtractText: boolean;
44
+ canExtractImages: boolean;
45
+ canAnalyzeContent: boolean;
46
+ canSearch: boolean;
47
+ canViewContent: boolean;
48
+ isAccessible: boolean;
49
+ issues: string[];
50
+ }
51
+
52
+ export class SecurityManager {
53
+ private _document: any;
54
+
55
+ /**
56
+ * Creates a new SecurityManager for the given document
57
+ * @param document - The PDF document
58
+ * @throws Error if document is null or undefined
59
+ */
60
+ constructor(document: any) {
61
+ if (!document) {
62
+ throw new Error('Document is required');
63
+ }
64
+ this._document = document;
65
+ }
66
+
67
+ /**
68
+ * Checks if the document is encrypted
69
+ * @returns True if document is encrypted
70
+ */
71
+ isEncrypted(): boolean {
72
+ try {
73
+ return this._document.isEncrypted();
74
+ } catch (error) {
75
+ return false;
76
+ }
77
+ }
78
+
79
+ /**
80
+ * Checks if document requires a password
81
+ * @returns True if password is required to open
82
+ */
83
+ requiresPassword(): boolean {
84
+ return this.isEncrypted();
85
+ }
86
+
87
+ /**
88
+ * Gets encryption algorithm used
89
+ * @returns Encryption algorithm (e.g., 'RC4', 'AES-128', 'AES-256') or null
90
+ */
91
+ getEncryptionAlgorithm(): string | null {
92
+ try {
93
+ if (!this.isEncrypted()) return null;
94
+ return this._document.getEncryptionAlgorithm() || null;
95
+ } catch (error) {
96
+ return null;
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Checks if user can print the document
102
+ * @returns True if printing is allowed
103
+ */
104
+ canPrint(): boolean {
105
+ try {
106
+ return this._document.canPrint();
107
+ } catch (error) {
108
+ return true;
109
+ }
110
+ }
111
+
112
+ /**
113
+ * Checks if user can copy text from document
114
+ * @returns True if copying is allowed
115
+ */
116
+ canCopy(): boolean {
117
+ try {
118
+ return this._document.canCopy();
119
+ } catch (error) {
120
+ return true;
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Checks if user can modify the document
126
+ * @returns True if modification is allowed
127
+ */
128
+ canModify(): boolean {
129
+ try {
130
+ return this._document.canModify();
131
+ } catch (error) {
132
+ return true;
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Checks if user can add or modify annotations
138
+ * @returns True if annotation modification is allowed
139
+ */
140
+ canAnnotate(): boolean {
141
+ try {
142
+ return this._document.canAnnotate();
143
+ } catch (error) {
144
+ return true;
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Checks if user can fill form fields
150
+ * @returns True if form filling is allowed
151
+ */
152
+ canFillForms(): boolean {
153
+ try {
154
+ return this._document.canFillForms();
155
+ } catch (error) {
156
+ return true;
157
+ }
158
+ }
159
+
160
+ /**
161
+ * Checks if document is view-only
162
+ * @returns True if no modifications are allowed
163
+ */
164
+ isViewOnly(): boolean {
165
+ return !this.canModify() && !this.canCopy() && !this.canAnnotate();
166
+ }
167
+
168
+ /**
169
+ * Gets all permissions as a summary object
170
+ * @returns Permissions summary
171
+ *
172
+ * @example
173
+ * ```typescript
174
+ * const perms = manager.getPermissionsSummary();
175
+ * console.log(JSON.stringify(perms, null, 2));
176
+ * // {
177
+ * // canPrint: true,
178
+ * // canCopy: true,
179
+ * // canModify: false,
180
+ * // canAnnotate: true,
181
+ * // canFillForms: true,
182
+ * // isViewOnly: false,
183
+ * // isEncrypted: true
184
+ * // }
185
+ * ```
186
+ */
187
+ getPermissionsSummary(): PermissionsSummary {
188
+ return {
189
+ canPrint: this.canPrint(),
190
+ canCopy: this.canCopy(),
191
+ canModify: this.canModify(),
192
+ canAnnotate: this.canAnnotate(),
193
+ canFillForms: this.canFillForms(),
194
+ isViewOnly: this.isViewOnly(),
195
+ isEncrypted: this.isEncrypted(),
196
+ requiresPassword: this.requiresPassword(),
197
+ encryptionAlgorithm: this.getEncryptionAlgorithm(),
198
+ };
199
+ }
200
+
201
+ /**
202
+ * Gets security level information
203
+ * @returns Security level details
204
+ *
205
+ * @example
206
+ * ```typescript
207
+ * const secLevel = manager.getSecurityLevel();
208
+ * console.log(`Security level: ${secLevel.level}`); // 'high', 'medium', 'low', 'none'
209
+ * ```
210
+ */
211
+ getSecurityLevel(): SecurityLevel {
212
+ const isEncrypted = this.isEncrypted();
213
+ const algorithm = this.getEncryptionAlgorithm();
214
+
215
+ let level: 'high' | 'medium' | 'low' | 'none' = 'none';
216
+ let description = 'No encryption';
217
+
218
+ if (isEncrypted) {
219
+ if (algorithm === 'AES-256') {
220
+ level = 'high';
221
+ description = 'AES-256 encryption';
222
+ } else if (algorithm === 'AES-128') {
223
+ level = 'medium';
224
+ description = 'AES-128 encryption';
225
+ } else if (algorithm === 'RC4') {
226
+ level = 'low';
227
+ description = 'RC4 encryption (deprecated)';
228
+ } else {
229
+ level = 'medium';
230
+ description = 'Encrypted with unknown algorithm';
231
+ }
232
+
233
+ // Reduce level if permissions are very restrictive
234
+ if (this.isViewOnly()) {
235
+ description += ' (read-only)';
236
+ }
237
+ }
238
+
239
+ return {
240
+ level,
241
+ description,
242
+ isEncrypted,
243
+ algorithm,
244
+ restrictedAccess: !this.canModify() && !this.canCopy(),
245
+ };
246
+ }
247
+
248
+ /**
249
+ * Validates document accessibility based on security settings
250
+ * @returns Accessibility validation result
251
+ *
252
+ * @example
253
+ * ```typescript
254
+ * const validation = manager.validateAccessibility();
255
+ * if (!validation.canExtractText) {
256
+ * console.log('Warning: Document does not allow text extraction');
257
+ * }
258
+ * ```
259
+ */
260
+ validateAccessibility(): AccessibilityValidation {
261
+ return {
262
+ canExtractText: this.canCopy(),
263
+ canExtractImages: this.canCopy(),
264
+ canAnalyzeContent: this.canCopy(),
265
+ canSearch: true, // Always allowed
266
+ canViewContent: true, // Always allowed
267
+ isAccessible: this.canCopy(),
268
+ issues: this.getAccessibilityIssues(),
269
+ };
270
+ }
271
+
272
+ /**
273
+ * Gets list of accessibility issues
274
+ * @returns Array of accessibility issues
275
+ * @private
276
+ */
277
+ private getAccessibilityIssues(): string[] {
278
+ const issues: string[] = [];
279
+
280
+ if (this.isEncrypted()) {
281
+ issues.push('Document is encrypted');
282
+ }
283
+
284
+ if (this.requiresPassword()) {
285
+ issues.push('Document requires password');
286
+ }
287
+
288
+ if (!this.canCopy()) {
289
+ issues.push('Text extraction is restricted');
290
+ }
291
+
292
+ if (!this.canModify()) {
293
+ issues.push('Document modification is restricted');
294
+ }
295
+
296
+ if (!this.canPrint()) {
297
+ issues.push('Printing is restricted');
298
+ }
299
+
300
+ if (!this.canAnnotate()) {
301
+ issues.push('Annotation is restricted');
302
+ }
303
+
304
+ return issues;
305
+ }
306
+
307
+ /**
308
+ * Generates security report
309
+ * @returns Human-readable security report
310
+ *
311
+ * @example
312
+ * ```typescript
313
+ * console.log(manager.generateSecurityReport());
314
+ * ```
315
+ */
316
+ generateSecurityReport(): string {
317
+ const lines: string[] = [];
318
+ const perms = this.getPermissionsSummary();
319
+ const secLevel = this.getSecurityLevel();
320
+
321
+ lines.push('=== PDF Security Report ===\n');
322
+
323
+ lines.push(`Encryption Status: ${perms.isEncrypted ? 'Encrypted' : 'Not encrypted'}`);
324
+ if (perms.isEncrypted) {
325
+ lines.push(` Algorithm: ${perms.encryptionAlgorithm || 'Unknown'}`);
326
+ lines.push(` Requires Password: ${perms.requiresPassword}`);
327
+ }
328
+
329
+ lines.push(`\nSecurity Level: ${secLevel.level.toUpperCase()}`);
330
+ lines.push(` Description: ${secLevel.description}`);
331
+
332
+ lines.push('\nAccess Permissions:');
333
+ lines.push(` Print: ${perms.canPrint ? 'Allowed' : 'Restricted'}`);
334
+ lines.push(` Copy: ${perms.canCopy ? 'Allowed' : 'Restricted'}`);
335
+ lines.push(` Modify: ${perms.canModify ? 'Allowed' : 'Restricted'}`);
336
+ lines.push(` Annotate: ${perms.canAnnotate ? 'Allowed' : 'Restricted'}`);
337
+ lines.push(` Fill Forms: ${perms.canFillForms ? 'Allowed' : 'Restricted'}`);
338
+
339
+ if (perms.isViewOnly) {
340
+ lines.push('\n⚠️ WARNING: Document is VIEW-ONLY');
341
+ }
342
+
343
+ return lines.join('\n');
344
+ }
345
+ }