pdf-lite 1.3.0 → 1.3.1
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.
- package/dist/acroform/acroform.d.ts +258 -23
- package/dist/acroform/acroform.js +986 -174
- package/dist/core/decoder.d.ts +1 -1
- package/dist/core/decoder.js +1 -1
- package/dist/core/index.d.ts +2 -2
- package/dist/core/index.js +2 -2
- package/dist/core/objects/pdf-array.d.ts +1 -0
- package/dist/core/objects/pdf-array.js +4 -0
- package/dist/core/objects/pdf-dictionary.d.ts +1 -0
- package/dist/core/objects/pdf-dictionary.js +12 -0
- package/dist/core/objects/pdf-hexadecimal.d.ts +3 -1
- package/dist/core/objects/pdf-hexadecimal.js +14 -2
- package/dist/core/objects/pdf-indirect-object.d.ts +5 -3
- package/dist/core/objects/pdf-indirect-object.js +23 -5
- package/dist/core/objects/pdf-number.js +3 -0
- package/dist/core/objects/pdf-object.d.ts +6 -0
- package/dist/core/objects/pdf-object.js +10 -0
- package/dist/core/objects/pdf-stream.js +3 -0
- package/dist/core/objects/pdf-string.js +3 -0
- package/dist/core/ref.d.ts +5 -0
- package/dist/core/ref.js +14 -0
- package/dist/core/serializer.d.ts +1 -1
- package/dist/core/serializer.js +1 -1
- package/dist/core/tokeniser.d.ts +2 -2
- package/dist/core/tokeniser.js +2 -2
- package/dist/fonts/font-manager.js +6 -8
- package/dist/pdf/pdf-document.d.ts +6 -5
- package/dist/pdf/pdf-document.js +29 -21
- package/dist/pdf/pdf-revision.d.ts +33 -4
- package/dist/pdf/pdf-revision.js +100 -26
- package/dist/pdf/pdf-xref-lookup.js +3 -2
- package/dist/xfa/manager.js +2 -4
- package/package.json +1 -1
- /package/dist/core/{incremental-parser.d.ts → parser/incremental-parser.d.ts} +0 -0
- /package/dist/core/{incremental-parser.js → parser/incremental-parser.js} +0 -0
- /package/dist/core/{parser.d.ts → parser/parser.d.ts} +0 -0
- /package/dist/core/{parser.js → parser/parser.js} +0 -0
|
@@ -2,6 +2,8 @@ import { PdfDictionary } from '../core/objects/pdf-dictionary.js';
|
|
|
2
2
|
import { PdfObject } from '../core/objects/pdf-object.js';
|
|
3
3
|
import { PdfTrailerEntries } from '../core/objects/pdf-trailer.js';
|
|
4
4
|
import { PdfToken } from '../core/tokens/token.js';
|
|
5
|
+
import { PdfComment } from '../index.js';
|
|
6
|
+
import { ByteArray } from '../types.js';
|
|
5
7
|
import { PdfXrefLookup } from './pdf-xref-lookup.js';
|
|
6
8
|
/**
|
|
7
9
|
* Represents a single revision of a PDF document.
|
|
@@ -9,12 +11,13 @@ import { PdfXrefLookup } from './pdf-xref-lookup.js';
|
|
|
9
11
|
* where each revision contains its own set of objects and cross-reference table.
|
|
10
12
|
*/
|
|
11
13
|
export declare class PdfRevision extends PdfObject {
|
|
12
|
-
/** Objects contained in this revision */
|
|
13
|
-
|
|
14
|
+
/** Objects contained in this revision (private backing field) */
|
|
15
|
+
private _objects;
|
|
16
|
+
/** Whether this revision is locked (private backing field) */
|
|
17
|
+
private _locked;
|
|
14
18
|
/** Cross-reference lookup table for this revision */
|
|
15
19
|
xref: PdfXrefLookup;
|
|
16
|
-
|
|
17
|
-
locked: boolean;
|
|
20
|
+
private cachedBytes?;
|
|
18
21
|
/**
|
|
19
22
|
* Creates a new PDF revision.
|
|
20
23
|
*
|
|
@@ -28,6 +31,29 @@ export declare class PdfRevision extends PdfObject {
|
|
|
28
31
|
prev?: PdfXrefLookup | PdfRevision;
|
|
29
32
|
locked?: boolean;
|
|
30
33
|
});
|
|
34
|
+
get header(): PdfComment | undefined;
|
|
35
|
+
set header(comment: PdfComment);
|
|
36
|
+
/**
|
|
37
|
+
* Gets whether this revision is locked (cannot be modified).
|
|
38
|
+
*/
|
|
39
|
+
get locked(): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Sets whether this revision is locked.
|
|
42
|
+
* When locking, creates a cached clone of all objects to freeze their state.
|
|
43
|
+
* When unlocking, clears the cache.
|
|
44
|
+
*/
|
|
45
|
+
set locked(value: boolean);
|
|
46
|
+
/**
|
|
47
|
+
* Gets the objects in this revision.
|
|
48
|
+
* Returns fresh clones of cached objects if the revision is locked, otherwise returns live objects.
|
|
49
|
+
* Each access to a locked revision's objects returns new clones to prevent mutations.
|
|
50
|
+
*/
|
|
51
|
+
get objects(): ReadonlyArray<PdfObject>;
|
|
52
|
+
/**
|
|
53
|
+
* Sets the objects array.
|
|
54
|
+
* @throws Error if the revision is locked
|
|
55
|
+
*/
|
|
56
|
+
set objects(value: PdfObject[]);
|
|
31
57
|
/**
|
|
32
58
|
* Links this revision to a previous revision's cross-reference table.
|
|
33
59
|
*
|
|
@@ -52,12 +78,14 @@ export declare class PdfRevision extends PdfObject {
|
|
|
52
78
|
* Adds objects to the beginning of the revision's object list.
|
|
53
79
|
*
|
|
54
80
|
* @param objects - Objects to add at the beginning
|
|
81
|
+
* @throws Error if the revision is locked
|
|
55
82
|
*/
|
|
56
83
|
unshift(...objects: PdfObject[]): void;
|
|
57
84
|
/**
|
|
58
85
|
* Adds objects to the revision.
|
|
59
86
|
*
|
|
60
87
|
* @param objects - Objects to add to the revision
|
|
88
|
+
* @throws Error if the revision is locked
|
|
61
89
|
*/
|
|
62
90
|
addObject(...objects: PdfObject[]): void;
|
|
63
91
|
/**
|
|
@@ -98,4 +126,5 @@ export declare class PdfRevision extends PdfObject {
|
|
|
98
126
|
*/
|
|
99
127
|
clone(): this;
|
|
100
128
|
protected tokenize(): PdfToken[];
|
|
129
|
+
toBytes(): ByteArray;
|
|
101
130
|
}
|
package/dist/pdf/pdf-revision.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PdfIndirectObject } from '../core/objects/pdf-indirect-object.js';
|
|
2
2
|
import { PdfObject } from '../core/objects/pdf-object.js';
|
|
3
3
|
import { PdfWhitespaceToken } from '../core/tokens/whitespace-token.js';
|
|
4
|
+
import { PdfComment } from '../index.js';
|
|
4
5
|
import { PdfXrefLookup } from './pdf-xref-lookup.js';
|
|
5
6
|
/**
|
|
6
7
|
* Represents a single revision of a PDF document.
|
|
@@ -8,12 +9,13 @@ import { PdfXrefLookup } from './pdf-xref-lookup.js';
|
|
|
8
9
|
* where each revision contains its own set of objects and cross-reference table.
|
|
9
10
|
*/
|
|
10
11
|
export class PdfRevision extends PdfObject {
|
|
11
|
-
/** Objects contained in this revision */
|
|
12
|
-
|
|
12
|
+
/** Objects contained in this revision (private backing field) */
|
|
13
|
+
_objects = [];
|
|
14
|
+
/** Whether this revision is locked (private backing field) */
|
|
15
|
+
_locked = false;
|
|
13
16
|
/** Cross-reference lookup table for this revision */
|
|
14
17
|
xref;
|
|
15
|
-
|
|
16
|
-
locked = false;
|
|
18
|
+
cachedBytes;
|
|
17
19
|
/**
|
|
18
20
|
* Creates a new PDF revision.
|
|
19
21
|
*
|
|
@@ -25,13 +27,67 @@ export class PdfRevision extends PdfObject {
|
|
|
25
27
|
constructor(options) {
|
|
26
28
|
super();
|
|
27
29
|
this.modified = false;
|
|
28
|
-
this.
|
|
29
|
-
this.xref = PdfXrefLookup.fromObjects(this.
|
|
30
|
+
this._objects = options?.objects ?? [];
|
|
31
|
+
this.xref = PdfXrefLookup.fromObjects(this._objects);
|
|
30
32
|
if (options?.prev)
|
|
31
33
|
this.setPrev(options.prev);
|
|
32
34
|
if (!this.contains(this.xref.object))
|
|
33
35
|
this.addObject(...this.xref.toTrailerSection());
|
|
34
|
-
this.
|
|
36
|
+
this._locked = options?.locked ?? false;
|
|
37
|
+
}
|
|
38
|
+
get header() {
|
|
39
|
+
const firstObj = this._objects[0];
|
|
40
|
+
if (firstObj instanceof PdfComment && firstObj.isVersionComment()) {
|
|
41
|
+
return firstObj;
|
|
42
|
+
}
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
set header(comment) {
|
|
46
|
+
if (this._locked) {
|
|
47
|
+
throw new Error('Cannot modify header in locked PDF revision');
|
|
48
|
+
}
|
|
49
|
+
const currentHeader = this.header;
|
|
50
|
+
if (currentHeader) {
|
|
51
|
+
this._objects[0] = comment;
|
|
52
|
+
}
|
|
53
|
+
else
|
|
54
|
+
this._objects.unshift(comment);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Gets whether this revision is locked (cannot be modified).
|
|
58
|
+
*/
|
|
59
|
+
get locked() {
|
|
60
|
+
return this._locked;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Sets whether this revision is locked.
|
|
64
|
+
* When locking, creates a cached clone of all objects to freeze their state.
|
|
65
|
+
* When unlocking, clears the cache.
|
|
66
|
+
*/
|
|
67
|
+
set locked(value) {
|
|
68
|
+
this._locked = value;
|
|
69
|
+
this.cachedBytes = value ? this.toBytes() : undefined;
|
|
70
|
+
for (const obj of this._objects) {
|
|
71
|
+
obj.setImmutable(value);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Gets the objects in this revision.
|
|
76
|
+
* Returns fresh clones of cached objects if the revision is locked, otherwise returns live objects.
|
|
77
|
+
* Each access to a locked revision's objects returns new clones to prevent mutations.
|
|
78
|
+
*/
|
|
79
|
+
get objects() {
|
|
80
|
+
return this._objects;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Sets the objects array.
|
|
84
|
+
* @throws Error if the revision is locked
|
|
85
|
+
*/
|
|
86
|
+
set objects(value) {
|
|
87
|
+
if (this._locked) {
|
|
88
|
+
throw new Error('Cannot modify objects array in locked revision');
|
|
89
|
+
}
|
|
90
|
+
this._objects = value;
|
|
35
91
|
}
|
|
36
92
|
/**
|
|
37
93
|
* Links this revision to a previous revision's cross-reference table.
|
|
@@ -49,7 +105,7 @@ export class PdfRevision extends PdfObject {
|
|
|
49
105
|
* @returns True if the exact object instance exists in this revision
|
|
50
106
|
*/
|
|
51
107
|
contains(object) {
|
|
52
|
-
return this.
|
|
108
|
+
return this._objects.includes(object);
|
|
53
109
|
}
|
|
54
110
|
/**
|
|
55
111
|
* Checks if an equivalent object exists in this revision (by value equality).
|
|
@@ -58,7 +114,7 @@ export class PdfRevision extends PdfObject {
|
|
|
58
114
|
* @returns True if an equal object exists in this revision
|
|
59
115
|
*/
|
|
60
116
|
exists(object) {
|
|
61
|
-
for (const obj of this.
|
|
117
|
+
for (const obj of this._objects) {
|
|
62
118
|
if (obj.equals(object)) {
|
|
63
119
|
return true;
|
|
64
120
|
}
|
|
@@ -69,10 +125,14 @@ export class PdfRevision extends PdfObject {
|
|
|
69
125
|
* Adds objects to the beginning of the revision's object list.
|
|
70
126
|
*
|
|
71
127
|
* @param objects - Objects to add at the beginning
|
|
128
|
+
* @throws Error if the revision is locked
|
|
72
129
|
*/
|
|
73
130
|
unshift(...objects) {
|
|
131
|
+
if (this._locked) {
|
|
132
|
+
throw new Error('Cannot add object to locked PDF revision');
|
|
133
|
+
}
|
|
74
134
|
for (const obj of objects.reverse()) {
|
|
75
|
-
this.
|
|
135
|
+
this._objects.unshift(obj);
|
|
76
136
|
if (obj instanceof PdfIndirectObject)
|
|
77
137
|
this.xref.addObject(obj);
|
|
78
138
|
}
|
|
@@ -81,8 +141,12 @@ export class PdfRevision extends PdfObject {
|
|
|
81
141
|
* Adds objects to the revision.
|
|
82
142
|
*
|
|
83
143
|
* @param objects - Objects to add to the revision
|
|
144
|
+
* @throws Error if the revision is locked
|
|
84
145
|
*/
|
|
85
146
|
addObject(...objects) {
|
|
147
|
+
if (this._locked) {
|
|
148
|
+
throw new Error('Cannot add object to locked PDF revision');
|
|
149
|
+
}
|
|
86
150
|
for (const obj of objects) {
|
|
87
151
|
this.addObjectAt(obj);
|
|
88
152
|
}
|
|
@@ -95,25 +159,25 @@ export class PdfRevision extends PdfObject {
|
|
|
95
159
|
* @throws Error if the revision is locked or index is out of bounds
|
|
96
160
|
*/
|
|
97
161
|
addObjectAt(object, index) {
|
|
98
|
-
if (this.
|
|
162
|
+
if (this._locked) {
|
|
99
163
|
throw new Error('Cannot add object to locked PDF revision');
|
|
100
164
|
}
|
|
101
165
|
if (index === undefined) {
|
|
102
166
|
index =
|
|
103
167
|
object instanceof PdfIndirectObject
|
|
104
168
|
? this.xref.object
|
|
105
|
-
: this.
|
|
169
|
+
: this._objects.length;
|
|
106
170
|
}
|
|
107
171
|
if (typeof index !== 'number') {
|
|
108
|
-
index = this.
|
|
172
|
+
index = this._objects.indexOf(index);
|
|
109
173
|
if (index === -1) {
|
|
110
|
-
index = this.
|
|
174
|
+
index = this._objects.length;
|
|
111
175
|
}
|
|
112
176
|
}
|
|
113
|
-
if (index < 0 || index > this.
|
|
177
|
+
if (index < 0 || index > this._objects.length) {
|
|
114
178
|
throw new Error('Index out of bounds');
|
|
115
179
|
}
|
|
116
|
-
this.
|
|
180
|
+
this._objects.splice(index, 0, object);
|
|
117
181
|
this.sortObjects();
|
|
118
182
|
if (object instanceof PdfIndirectObject) {
|
|
119
183
|
this.xref.addObject(object);
|
|
@@ -127,16 +191,16 @@ export class PdfRevision extends PdfObject {
|
|
|
127
191
|
* @throws Error if the revision is locked
|
|
128
192
|
*/
|
|
129
193
|
deleteObject(...objects) {
|
|
130
|
-
if (this.
|
|
194
|
+
if (this._locked) {
|
|
131
195
|
throw new Error('Cannot delete object from locked PDF revision');
|
|
132
196
|
}
|
|
133
197
|
for (const object of objects) {
|
|
134
|
-
const index = this.
|
|
198
|
+
const index = this._objects.indexOf(object);
|
|
135
199
|
if (index === -1) {
|
|
136
200
|
return;
|
|
137
201
|
}
|
|
138
202
|
this.modified = true;
|
|
139
|
-
this.
|
|
203
|
+
this._objects.splice(index, 1);
|
|
140
204
|
if (object instanceof PdfIndirectObject) {
|
|
141
205
|
this.xref.removeObject(object);
|
|
142
206
|
}
|
|
@@ -145,13 +209,13 @@ export class PdfRevision extends PdfObject {
|
|
|
145
209
|
isModified() {
|
|
146
210
|
return (super.isModified() ||
|
|
147
211
|
this.xref.trailerDict.isModified() ||
|
|
148
|
-
this.
|
|
212
|
+
this._objects.some((obj) => obj.isModified()));
|
|
149
213
|
}
|
|
150
214
|
/**
|
|
151
215
|
* Updates the revision by sorting objects and updating the xref table.
|
|
152
216
|
*/
|
|
153
217
|
update() {
|
|
154
|
-
if (this.
|
|
218
|
+
if (this._locked) {
|
|
155
219
|
return;
|
|
156
220
|
}
|
|
157
221
|
this.sortObjects();
|
|
@@ -163,7 +227,7 @@ export class PdfRevision extends PdfObject {
|
|
|
163
227
|
* Indirect objects are placed before other objects.
|
|
164
228
|
*/
|
|
165
229
|
sortObjects() {
|
|
166
|
-
this.
|
|
230
|
+
this._objects.sort((a, b) => {
|
|
167
231
|
if (a instanceof PdfIndirectObject &&
|
|
168
232
|
b instanceof PdfIndirectObject) {
|
|
169
233
|
return a.order() - b.order();
|
|
@@ -191,9 +255,19 @@ export class PdfRevision extends PdfObject {
|
|
|
191
255
|
return new PdfRevision({ objects: clonedObjects });
|
|
192
256
|
}
|
|
193
257
|
tokenize() {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
PdfWhitespaceToken
|
|
197
|
-
|
|
258
|
+
const output = this.objects.flatMap((obj) => {
|
|
259
|
+
const objTokens = obj.toTokens();
|
|
260
|
+
if (!(objTokens[objTokens.length - 1] instanceof PdfWhitespaceToken)) {
|
|
261
|
+
objTokens.push(new PdfWhitespaceToken('\n'));
|
|
262
|
+
}
|
|
263
|
+
return objTokens;
|
|
264
|
+
});
|
|
265
|
+
return output;
|
|
266
|
+
}
|
|
267
|
+
toBytes() {
|
|
268
|
+
if (this.cachedBytes) {
|
|
269
|
+
return this.cachedBytes;
|
|
270
|
+
}
|
|
271
|
+
return super.toBytes();
|
|
198
272
|
}
|
|
199
273
|
}
|
|
@@ -375,10 +375,11 @@ export class PdfXrefLookup {
|
|
|
375
375
|
* @returns The xref entry or undefined if not found
|
|
376
376
|
*/
|
|
377
377
|
getObject(objectNumber) {
|
|
378
|
-
|
|
378
|
+
const entry = this.entries.get(objectNumber);
|
|
379
|
+
if (this.prev && !entry) {
|
|
379
380
|
return this.prev.getObject(objectNumber);
|
|
380
381
|
}
|
|
381
|
-
return
|
|
382
|
+
return entry;
|
|
382
383
|
}
|
|
383
384
|
/**
|
|
384
385
|
* Generates the trailer section objects for this xref.
|
package/dist/xfa/manager.js
CHANGED
|
@@ -68,10 +68,8 @@ export class PdfXfaManager {
|
|
|
68
68
|
* @returns The AcroForm dictionary or null if not found
|
|
69
69
|
*/
|
|
70
70
|
async getAcroForm() {
|
|
71
|
-
const catalog = this.document.
|
|
72
|
-
|
|
73
|
-
return null;
|
|
74
|
-
const acroFormRef = catalog.get('AcroForm');
|
|
71
|
+
const catalog = this.document.root;
|
|
72
|
+
const acroFormRef = catalog.content.get('AcroForm');
|
|
75
73
|
if (!acroFormRef)
|
|
76
74
|
return null;
|
|
77
75
|
if (acroFormRef instanceof PdfObjectReference) {
|
package/package.json
CHANGED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|