pdf-lite 1.5.0 → 1.6.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/EXAMPLES.md +51 -70
- package/README.md +1 -1
- package/dist/acroform/appearance/pdf-button-appearance-stream.d.ts +1 -1
- package/dist/acroform/appearance/pdf-button-appearance-stream.js +6 -2
- package/dist/acroform/fields/pdf-button-form-field.d.ts +0 -9
- package/dist/acroform/fields/pdf-button-form-field.js +7 -39
- package/dist/acroform/fields/pdf-choice-form-field.d.ts +2 -1
- package/dist/acroform/fields/pdf-choice-form-field.js +19 -38
- package/dist/acroform/fields/pdf-default-appearance.js +1 -1
- package/dist/acroform/fields/pdf-form-field.d.ts +27 -32
- package/dist/acroform/fields/pdf-form-field.js +180 -94
- package/dist/acroform/fields/pdf-text-form-field.js +6 -33
- package/dist/acroform/fields/types.d.ts +1 -1
- package/dist/acroform/index.d.ts +0 -2
- package/dist/acroform/index.js +0 -2
- package/dist/acroform/pdf-acro-form.d.ts +12 -36
- package/dist/acroform/pdf-acro-form.js +111 -201
- package/dist/acroform/xfa/pdf-xfa-data.d.ts +4 -3
- package/dist/acroform/xfa/pdf-xfa-data.js +16 -12
- package/dist/acroform/xfa/pdf-xfa-form.d.ts +9 -4
- package/dist/acroform/xfa/pdf-xfa-form.js +17 -39
- package/dist/annotations/index.d.ts +0 -1
- package/dist/annotations/index.js +0 -1
- package/dist/annotations/pdf-annotation.d.ts +7 -2
- package/dist/annotations/pdf-annotation.js +30 -19
- package/dist/annotations/pdf-default-resources.d.ts +11 -0
- package/dist/annotations/pdf-default-resources.js +3 -0
- package/dist/core/decoder.js +1 -1
- package/dist/core/objects/pdf-array.d.ts +8 -1
- package/dist/core/objects/pdf-array.js +31 -0
- package/dist/core/objects/pdf-dictionary.d.ts +2 -0
- package/dist/core/objects/pdf-dictionary.js +14 -7
- package/dist/core/objects/pdf-hexadecimal.d.ts +1 -0
- package/dist/core/objects/pdf-hexadecimal.js +3 -3
- package/dist/core/objects/pdf-indirect-object.d.ts +18 -9
- package/dist/core/objects/pdf-indirect-object.js +75 -16
- package/dist/core/objects/pdf-number.d.ts +1 -0
- package/dist/core/objects/pdf-number.js +5 -4
- package/dist/core/objects/pdf-object-reference.d.ts +8 -1
- package/dist/core/objects/pdf-object-reference.js +14 -0
- package/dist/core/objects/pdf-object.d.ts +14 -0
- package/dist/core/objects/pdf-object.js +36 -0
- package/dist/core/objects/pdf-start-xref.d.ts +1 -0
- package/dist/core/objects/pdf-start-xref.js +4 -0
- package/dist/core/objects/pdf-stream.d.ts +47 -7
- package/dist/core/objects/pdf-stream.js +301 -32
- package/dist/core/objects/pdf-string.d.ts +1 -0
- package/dist/core/objects/pdf-string.js +3 -6
- package/dist/core/objects/pdf-trailer.d.ts +1 -0
- package/dist/core/objects/pdf-trailer.js +6 -3
- package/dist/core/objects/pdf-xref-table.js +1 -1
- package/dist/core/ref.d.ts +3 -1
- package/dist/core/ref.js +8 -5
- package/dist/core/tokens/token.d.ts +2 -1
- package/dist/core/tokens/token.js +3 -0
- package/dist/fonts/index.d.ts +0 -1
- package/dist/fonts/index.js +0 -1
- package/dist/fonts/pdf-font.d.ts +32 -27
- package/dist/fonts/pdf-font.js +115 -77
- package/dist/pdf/index.d.ts +2 -0
- package/dist/pdf/index.js +2 -0
- package/dist/pdf/pdf-document.d.ts +63 -37
- package/dist/pdf/pdf-document.js +351 -135
- package/dist/pdf/pdf-page.d.ts +50 -0
- package/dist/pdf/pdf-page.js +144 -0
- package/dist/pdf/pdf-pages.d.ts +28 -0
- package/dist/pdf/pdf-pages.js +94 -0
- package/dist/pdf/pdf-reader.d.ts +5 -1
- package/dist/pdf/pdf-reader.js +36 -2
- package/dist/pdf/pdf-revision.d.ts +3 -3
- package/dist/pdf/pdf-revision.js +7 -7
- package/dist/pdf/pdf-xref-lookup.js +34 -14
- package/dist/signing/document-security-store.d.ts +14 -17
- package/dist/signing/document-security-store.js +19 -34
- package/dist/signing/signer.d.ts +23 -8
- package/dist/signing/signer.js +51 -17
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +0 -1
- package/dist/utils/needsCentralWhitespace.d.ts +10 -0
- package/dist/utils/needsCentralWhitespace.js +34 -0
- package/package.json +3 -3
- package/dist/acroform/acroform.d.ts +0 -9
- package/dist/acroform/acroform.js +0 -7
- package/dist/acroform/manager.d.ts +0 -37
- package/dist/acroform/manager.js +0 -57
- package/dist/acroform/pdf-font-encoding-cache.d.ts +0 -27
- package/dist/acroform/pdf-font-encoding-cache.js +0 -188
- package/dist/annotations/pdf-annotation-writer.d.ts +0 -20
- package/dist/annotations/pdf-annotation-writer.js +0 -76
- package/dist/fonts/manager.d.ts +0 -127
- package/dist/fonts/manager.js +0 -378
- package/dist/utils/predictors.d.ts +0 -113
- package/dist/utils/predictors.js +0 -279
package/dist/utils/predictors.js
DELETED
|
@@ -1,279 +0,0 @@
|
|
|
1
|
-
import { PdfNumber } from '../core/objects/pdf-number.js';
|
|
2
|
-
/**
|
|
3
|
-
* Handles PNG and TIFF predictor encoding and decoding for PDF streams.
|
|
4
|
-
* Predictors are used to improve compression efficiency by transforming
|
|
5
|
-
* image data before or after compression.
|
|
6
|
-
*/
|
|
7
|
-
export class Predictor {
|
|
8
|
-
/**
|
|
9
|
-
* Decodes data that was encoded with a predictor.
|
|
10
|
-
*
|
|
11
|
-
* @param data - The encoded data to decode.
|
|
12
|
-
* @param params - Optional decode parameters including Predictor, Columns, Colors, and BitsPerComponent.
|
|
13
|
-
* @returns The decoded byte array.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* const decoded = Predictor.decode(encodedData, { Predictor: 12, Columns: 100 })
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
static decode(data, params = {}) {
|
|
21
|
-
const predictor = params.Predictor ?? 1;
|
|
22
|
-
const columns = params.Columns ?? 1;
|
|
23
|
-
const colors = params.Colors ?? 1;
|
|
24
|
-
const bpc = params.BitsPerComponent ?? 8;
|
|
25
|
-
const bpp = Math.ceil((colors * bpc) / 8);
|
|
26
|
-
if (predictor === 2) {
|
|
27
|
-
return this.tiffDecode(data, columns, bpp);
|
|
28
|
-
}
|
|
29
|
-
else if (predictor >= 10 && predictor <= 15) {
|
|
30
|
-
return this.pngDecode(data, columns, bpp);
|
|
31
|
-
}
|
|
32
|
-
return data;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Encodes data using a predictor algorithm.
|
|
36
|
-
*
|
|
37
|
-
* @param data - The data to encode.
|
|
38
|
-
* @param params - Optional encode parameters including Predictor, Columns, Colors, and BitsPerComponent.
|
|
39
|
-
* @param PdfStreamFilterType - The PNG filter type to use for encoding.
|
|
40
|
-
* @returns The encoded byte array.
|
|
41
|
-
*
|
|
42
|
-
* @example
|
|
43
|
-
* ```typescript
|
|
44
|
-
* const encoded = Predictor.encode(rawData, { Predictor: 12, Columns: 100 }, 1)
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
static encode(data, params = {}, PdfStreamFilterType = 0) {
|
|
48
|
-
const predictor = params.Predictor ?? 1;
|
|
49
|
-
const columns = params.Columns ?? 1;
|
|
50
|
-
const colors = params.Colors ?? 1;
|
|
51
|
-
const bpc = params.BitsPerComponent ?? 8;
|
|
52
|
-
const bpp = Math.ceil((colors * bpc) / 8);
|
|
53
|
-
if (predictor === 2) {
|
|
54
|
-
return this.tiffEncode(data, columns, bpp);
|
|
55
|
-
}
|
|
56
|
-
else if (predictor >= 10 && predictor <= 15) {
|
|
57
|
-
return this.pngEncode(data, columns, bpp, PdfStreamFilterType);
|
|
58
|
-
}
|
|
59
|
-
return data;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Decodes TIFF predictor encoded data.
|
|
63
|
-
*
|
|
64
|
-
* @param data - The TIFF encoded data.
|
|
65
|
-
* @param columns - The number of columns in the image.
|
|
66
|
-
* @param bpp - Bytes per pixel.
|
|
67
|
-
* @returns The decoded byte array.
|
|
68
|
-
*/
|
|
69
|
-
static tiffDecode(data, columns, bpp) {
|
|
70
|
-
const rowLength = columns * bpp;
|
|
71
|
-
const output = new Uint8Array(data.length);
|
|
72
|
-
for (let i = 0; i < data.length; i += rowLength) {
|
|
73
|
-
for (let j = 0; j < rowLength; j++) {
|
|
74
|
-
output[i + j] =
|
|
75
|
-
j < bpp
|
|
76
|
-
? data[i + j]
|
|
77
|
-
: (data[i + j] + output[i + j - bpp]) & 0xff;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return output;
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Encodes data using TIFF predictor.
|
|
84
|
-
*
|
|
85
|
-
* @param data - The data to encode.
|
|
86
|
-
* @param columns - The number of columns in the image.
|
|
87
|
-
* @param bpp - Bytes per pixel.
|
|
88
|
-
* @returns The TIFF encoded byte array.
|
|
89
|
-
*/
|
|
90
|
-
static tiffEncode(data, columns, bpp) {
|
|
91
|
-
const rowLength = columns * bpp;
|
|
92
|
-
const output = new Uint8Array(data.length);
|
|
93
|
-
for (let i = 0; i < data.length; i += rowLength) {
|
|
94
|
-
for (let j = 0; j < rowLength; j++) {
|
|
95
|
-
output[i + j] =
|
|
96
|
-
j < bpp
|
|
97
|
-
? data[i + j]
|
|
98
|
-
: (data[i + j] - data[i + j - bpp] + 256) & 0xff;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return output;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Decodes PNG predictor encoded data.
|
|
105
|
-
*
|
|
106
|
-
* @param data - The PNG encoded data.
|
|
107
|
-
* @param columns - The number of columns in the image.
|
|
108
|
-
* @param bpp - Bytes per pixel.
|
|
109
|
-
* @returns The decoded byte array.
|
|
110
|
-
* @throws Error if an unsupported PNG filter type is encountered.
|
|
111
|
-
*/
|
|
112
|
-
static pngDecode(data, columns, bpp) {
|
|
113
|
-
const rowLength = columns * bpp;
|
|
114
|
-
const output = [];
|
|
115
|
-
for (let i = 0; i < data.length;) {
|
|
116
|
-
const filter = data[i];
|
|
117
|
-
const row = data.slice(i + 1, i + 1 + rowLength);
|
|
118
|
-
const prior = output.slice(output.length - rowLength, output.length);
|
|
119
|
-
const decodedRow = new Uint8Array(rowLength);
|
|
120
|
-
for (let j = 0; j < rowLength; j++) {
|
|
121
|
-
const left = j >= bpp ? decodedRow[j - bpp] : 0;
|
|
122
|
-
const up = prior[j] ?? 0;
|
|
123
|
-
const upperLeft = j >= bpp ? (prior[j - bpp] ?? 0) : 0;
|
|
124
|
-
switch (filter) {
|
|
125
|
-
case 0:
|
|
126
|
-
decodedRow[j] = row[j];
|
|
127
|
-
break;
|
|
128
|
-
case 1:
|
|
129
|
-
decodedRow[j] = (row[j] + left) & 0xff;
|
|
130
|
-
break;
|
|
131
|
-
case 2:
|
|
132
|
-
decodedRow[j] = (row[j] + up) & 0xff;
|
|
133
|
-
break;
|
|
134
|
-
case 3:
|
|
135
|
-
decodedRow[j] =
|
|
136
|
-
(row[j] + Math.floor((left + up) / 2)) & 0xff;
|
|
137
|
-
break;
|
|
138
|
-
case 4:
|
|
139
|
-
decodedRow[j] =
|
|
140
|
-
(row[j] +
|
|
141
|
-
this.paethPredictor(left, up, upperLeft)) &
|
|
142
|
-
0xff;
|
|
143
|
-
break;
|
|
144
|
-
default:
|
|
145
|
-
throw new Error(`Unsupported PNG filter: ${filter}`);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
output.push(...decodedRow);
|
|
149
|
-
i += 1 + rowLength;
|
|
150
|
-
}
|
|
151
|
-
return new Uint8Array(output);
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Encodes data using PNG predictor.
|
|
155
|
-
*
|
|
156
|
-
* @param data - The data to encode.
|
|
157
|
-
* @param columns - The number of columns in the image.
|
|
158
|
-
* @param bpp - Bytes per pixel.
|
|
159
|
-
* @param PdfStreamFilterType - The PNG filter type (0-4) to use.
|
|
160
|
-
* @returns The PNG encoded byte array.
|
|
161
|
-
* @throws Error if an unsupported PNG filter type is specified.
|
|
162
|
-
*/
|
|
163
|
-
static pngEncode(data, columns, bpp, PdfStreamFilterType) {
|
|
164
|
-
const rowLength = columns * bpp;
|
|
165
|
-
const output = [];
|
|
166
|
-
let prior = new Uint8Array(rowLength);
|
|
167
|
-
for (let i = 0; i < data.length; i += rowLength) {
|
|
168
|
-
const row = data.slice(i, i + rowLength);
|
|
169
|
-
const encodedRow = new Uint8Array(rowLength);
|
|
170
|
-
for (let j = 0; j < rowLength; j++) {
|
|
171
|
-
const left = j >= bpp ? row[j - bpp] : 0;
|
|
172
|
-
const up = prior[j] ?? 0;
|
|
173
|
-
const upperLeft = j >= bpp ? (prior[j - bpp] ?? 0) : 0;
|
|
174
|
-
switch (PdfStreamFilterType) {
|
|
175
|
-
case 0:
|
|
176
|
-
encodedRow[j] = row[j];
|
|
177
|
-
break;
|
|
178
|
-
case 1:
|
|
179
|
-
encodedRow[j] = (row[j] - left + 256) & 0xff;
|
|
180
|
-
break;
|
|
181
|
-
case 2:
|
|
182
|
-
encodedRow[j] = (row[j] - up + 256) & 0xff;
|
|
183
|
-
break;
|
|
184
|
-
case 3:
|
|
185
|
-
encodedRow[j] =
|
|
186
|
-
(row[j] - Math.floor((left + up) / 2) + 256) & 0xff;
|
|
187
|
-
break;
|
|
188
|
-
case 4:
|
|
189
|
-
encodedRow[j] =
|
|
190
|
-
(row[j] -
|
|
191
|
-
this.paethPredictor(left, up, upperLeft) +
|
|
192
|
-
256) &
|
|
193
|
-
0xff;
|
|
194
|
-
break;
|
|
195
|
-
default:
|
|
196
|
-
throw new Error(`Unsupported PNG filter: ${PdfStreamFilterType}`);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
output.push(PdfStreamFilterType, ...encodedRow);
|
|
200
|
-
prior = row;
|
|
201
|
-
}
|
|
202
|
-
return new Uint8Array(output);
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Implements the Paeth predictor algorithm used in PNG filtering.
|
|
206
|
-
*
|
|
207
|
-
* @param a - The left pixel value.
|
|
208
|
-
* @param b - The above pixel value.
|
|
209
|
-
* @param c - The upper-left pixel value.
|
|
210
|
-
* @returns The predicted pixel value.
|
|
211
|
-
*/
|
|
212
|
-
static paethPredictor(a, b, c) {
|
|
213
|
-
const p = a + b - c;
|
|
214
|
-
const pa = Math.abs(p - a);
|
|
215
|
-
const pb = Math.abs(p - b);
|
|
216
|
-
const pc = Math.abs(p - c);
|
|
217
|
-
if (pa <= pb && pa <= pc)
|
|
218
|
-
return a;
|
|
219
|
-
if (pb <= pc)
|
|
220
|
-
return b;
|
|
221
|
-
return c;
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Extracts decode parameters from a PDF dictionary.
|
|
225
|
-
*
|
|
226
|
-
* @param decodeParms - Optional PDF dictionary containing decode parameters.
|
|
227
|
-
* @returns The decode parameters object or null if not applicable.
|
|
228
|
-
*
|
|
229
|
-
* @example
|
|
230
|
-
* ```typescript
|
|
231
|
-
* const params = Predictor.getDecodeParms(dictionary)
|
|
232
|
-
* if (params) {
|
|
233
|
-
* console.log(params.Predictor)
|
|
234
|
-
* }
|
|
235
|
-
* ```
|
|
236
|
-
*/
|
|
237
|
-
static getDecodeParms(decodeParms) {
|
|
238
|
-
if (!decodeParms) {
|
|
239
|
-
return null;
|
|
240
|
-
}
|
|
241
|
-
const predictor = decodeParms.get('Predictor')?.as(PdfNumber)?.value;
|
|
242
|
-
if (predictor === undefined) {
|
|
243
|
-
return null;
|
|
244
|
-
}
|
|
245
|
-
if (predictor <= 1 || predictor > 15) {
|
|
246
|
-
return null;
|
|
247
|
-
}
|
|
248
|
-
const BitsPerComponent = decodeParms
|
|
249
|
-
.get('BitsPerComponent')
|
|
250
|
-
?.as(PdfNumber);
|
|
251
|
-
const Columns = decodeParms.get('Columns')?.as(PdfNumber);
|
|
252
|
-
const Colors = decodeParms.get('Colors')?.as(PdfNumber);
|
|
253
|
-
return {
|
|
254
|
-
BitsPerComponent: BitsPerComponent?.value,
|
|
255
|
-
Columns: Columns?.value,
|
|
256
|
-
Predictor: predictor,
|
|
257
|
-
Colors: Colors?.value,
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Checks if the decode parameters can be handled by this predictor.
|
|
262
|
-
*
|
|
263
|
-
* @param decodeParms - Optional PDF dictionary containing decode parameters.
|
|
264
|
-
* @returns True if the parameters can be handled, false otherwise.
|
|
265
|
-
*
|
|
266
|
-
* @example
|
|
267
|
-
* ```typescript
|
|
268
|
-
* if (Predictor.canHandleDecodeParms(dictionary)) {
|
|
269
|
-
* // Process with predictor
|
|
270
|
-
* }
|
|
271
|
-
* ```
|
|
272
|
-
*/
|
|
273
|
-
static canHandleDecodeParms(decodeParms) {
|
|
274
|
-
if (Predictor.getDecodeParms(decodeParms) === null) {
|
|
275
|
-
return false;
|
|
276
|
-
}
|
|
277
|
-
return true;
|
|
278
|
-
}
|
|
279
|
-
}
|