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/fonts/manager.js
DELETED
|
@@ -1,378 +0,0 @@
|
|
|
1
|
-
import { PdfDictionary } from '../core/objects/pdf-dictionary.js';
|
|
2
|
-
import { PdfIndirectObject } from '../core/objects/pdf-indirect-object.js';
|
|
3
|
-
import { PdfName } from '../core/objects/pdf-name.js';
|
|
4
|
-
import { PdfArray } from '../core/objects/pdf-array.js';
|
|
5
|
-
import { PdfObjectReference } from '../core/objects/pdf-object-reference.js';
|
|
6
|
-
import { PdfFont } from './pdf-font.js';
|
|
7
|
-
import { parseFont } from './parsers/font-parser.js';
|
|
8
|
-
/**
|
|
9
|
-
* Manages font embedding in PDF documents.
|
|
10
|
-
* Provides methods to embed TrueType and other font formats.
|
|
11
|
-
*/
|
|
12
|
-
export class PdfFontManager {
|
|
13
|
-
document;
|
|
14
|
-
fontResourceCounter = 0;
|
|
15
|
-
constructor(document) {
|
|
16
|
-
this.document = document;
|
|
17
|
-
}
|
|
18
|
-
async findFontByName(fontName) {
|
|
19
|
-
return await this.searchFontInPdf(fontName);
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Embeds a TrueType font into the PDF document.
|
|
23
|
-
*
|
|
24
|
-
* @param fontData - The font file bytes
|
|
25
|
-
* @param fontName - The name to use for this font in the PDF
|
|
26
|
-
* @param descriptor - Font metrics and properties
|
|
27
|
-
* @returns A PdfFont object representing the embedded font
|
|
28
|
-
*/
|
|
29
|
-
async embedTrueTypeFont(fontData, fontName, descriptor) {
|
|
30
|
-
// Create font using factory
|
|
31
|
-
const font = PdfFont.fromTrueTypeData(fontData, fontName, descriptor);
|
|
32
|
-
// Write to PDF
|
|
33
|
-
return await this.write(font);
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Embeds a standard PDF font (Type1).
|
|
37
|
-
* These fonts don't require font data as they're built into PDF viewers.
|
|
38
|
-
*
|
|
39
|
-
* @param fontName - One of the 14 standard PDF fonts
|
|
40
|
-
* @returns A PdfFont object representing the embedded font
|
|
41
|
-
*/
|
|
42
|
-
async embedStandardFont(fontName) {
|
|
43
|
-
// Create font using factory
|
|
44
|
-
const font = PdfFont.fromStandardFont(fontName);
|
|
45
|
-
// Write to PDF
|
|
46
|
-
return await this.write(font);
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Embeds a font from file data with automatic parsing and configuration.
|
|
50
|
-
* This is the recommended high-level API for embedding fonts.
|
|
51
|
-
*
|
|
52
|
-
* @param fontData - The font file bytes (TTF, OTF, or WOFF format)
|
|
53
|
-
* @param options - Optional configuration
|
|
54
|
-
* @param options.fontName - Custom font name (defaults to PostScript name from font)
|
|
55
|
-
* @param options.unicode - Use Unicode/Type0 encoding for non-ASCII characters
|
|
56
|
-
* @param options.unicodeMappings - Custom CID to Unicode mappings for Type0 fonts
|
|
57
|
-
* @returns A PdfFont object representing the embedded font
|
|
58
|
-
*
|
|
59
|
-
* @example
|
|
60
|
-
* ```typescript
|
|
61
|
-
* // Simple embedding with auto-generated name
|
|
62
|
-
* const font = await document.fonts.embedFromFile(fontData)
|
|
63
|
-
* field.font = font
|
|
64
|
-
*
|
|
65
|
-
* // With custom name and Unicode support
|
|
66
|
-
* const font = await document.fonts.embedFromFile(fontData, {
|
|
67
|
-
* fontName: 'MyCustomFont',
|
|
68
|
-
* unicode: true
|
|
69
|
-
* })
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
async embedFromFile(fontData, options) {
|
|
73
|
-
// Parse the font to extract metadata
|
|
74
|
-
const parser = parseFont(fontData);
|
|
75
|
-
const info = parser.getFontInfo();
|
|
76
|
-
// Auto-generate font name from metadata if not provided
|
|
77
|
-
const fontName = options?.fontName ?? info.postScriptName ?? info.fullName;
|
|
78
|
-
// Get the appropriate descriptor based on unicode option
|
|
79
|
-
const descriptor = parser.getFontDescriptor(fontName);
|
|
80
|
-
// Embed using the appropriate method and return PdfFont
|
|
81
|
-
if (options?.unicode) {
|
|
82
|
-
// For Unicode fonts, we need a UnicodeFontDescriptor
|
|
83
|
-
// Create one by extending the base descriptor
|
|
84
|
-
const unicodeDescriptor = {
|
|
85
|
-
...descriptor,
|
|
86
|
-
defaultWidth: 1000,
|
|
87
|
-
cidToGidMap: 'Identity',
|
|
88
|
-
};
|
|
89
|
-
return await this.embedTrueTypeFontUnicode(fontData, fontName, unicodeDescriptor, options.unicodeMappings);
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
// Use standard TrueType embedding
|
|
93
|
-
return await this.embedTrueTypeFont(fontData, fontName, descriptor);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Gets the font reference by font name or resource name.
|
|
98
|
-
*/
|
|
99
|
-
async getFont(fontName) {
|
|
100
|
-
return await this.searchFontInPdf(fontName);
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Writes a font to the PDF document.
|
|
104
|
-
* Assigns resource name, creates container object, commits all objects,
|
|
105
|
-
* and registers it in page resources.
|
|
106
|
-
*
|
|
107
|
-
* @param font - The PdfFont instance to write
|
|
108
|
-
* @param cacheKey - Unused, kept for API compatibility
|
|
109
|
-
* @returns The font with its resourceName and container set
|
|
110
|
-
* @internal
|
|
111
|
-
*/
|
|
112
|
-
async write(font) {
|
|
113
|
-
// Assign resource name
|
|
114
|
-
this.fontResourceCounter++;
|
|
115
|
-
const resourceName = `F${this.fontResourceCounter}`;
|
|
116
|
-
font.resourceName = resourceName;
|
|
117
|
-
// Create container that wraps the font dictionary
|
|
118
|
-
const fontObject = new PdfIndirectObject({
|
|
119
|
-
content: font,
|
|
120
|
-
});
|
|
121
|
-
font.container = fontObject;
|
|
122
|
-
// Get all objects to commit (auxiliary objects + container)
|
|
123
|
-
const objectsToCommit = font.getObjectsToCommit();
|
|
124
|
-
if (objectsToCommit.length > 0) {
|
|
125
|
-
await this.document.commit(...objectsToCommit);
|
|
126
|
-
}
|
|
127
|
-
// Register in page resources
|
|
128
|
-
await this.addFontToPageResources(resourceName, fontObject);
|
|
129
|
-
return font;
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Gets all embedded fonts.
|
|
133
|
-
* Searches the PDF structure to find all fonts.
|
|
134
|
-
*/
|
|
135
|
-
async getAllFonts() {
|
|
136
|
-
return await this.collectAllFontsFromPdf();
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Embeds a TrueType font with Unicode/Type0 support.
|
|
140
|
-
* Use this for fonts that need to display non-ASCII characters.
|
|
141
|
-
*
|
|
142
|
-
* @param fontData - The font file bytes
|
|
143
|
-
* @param fontName - The name to use for this font in the PDF
|
|
144
|
-
* @param descriptor - Unicode font descriptor with CID metrics
|
|
145
|
-
* @param unicodeMappings - Map of CID to Unicode code point for ToUnicode CMap
|
|
146
|
-
* @returns A PdfFont object representing the embedded font
|
|
147
|
-
*/
|
|
148
|
-
async embedTrueTypeFontUnicode(fontData, fontName, descriptor, unicodeMappings) {
|
|
149
|
-
// Create font using factory
|
|
150
|
-
const font = PdfFont.fromType0Data(fontData, fontName, descriptor, unicodeMappings);
|
|
151
|
-
// Write to PDF
|
|
152
|
-
return await this.write(font);
|
|
153
|
-
}
|
|
154
|
-
/**
|
|
155
|
-
* Loads existing fonts from the PDF document.
|
|
156
|
-
* Traverses the page tree and extracts font information from page resources.
|
|
157
|
-
*
|
|
158
|
-
* @returns Map of font names to their PdfFont objects
|
|
159
|
-
*/
|
|
160
|
-
async loadExistingFonts() {
|
|
161
|
-
return await this.collectAllFontsFromPdf();
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Traverses the page tree to find and load existing fonts.
|
|
165
|
-
*/
|
|
166
|
-
async traversePageTreeForFonts(nodeRef, fonts) {
|
|
167
|
-
const nodeObject = await this.document.readObject({
|
|
168
|
-
objectNumber: nodeRef.objectNumber,
|
|
169
|
-
generationNumber: nodeRef.generationNumber,
|
|
170
|
-
});
|
|
171
|
-
if (!nodeObject)
|
|
172
|
-
return;
|
|
173
|
-
const nodeDict = nodeObject.content.as(PdfDictionary);
|
|
174
|
-
const type = nodeDict.get('Type')?.as(PdfName);
|
|
175
|
-
if (type?.value === 'Page') {
|
|
176
|
-
await this.extractFontsFromPage(nodeObject, nodeDict, fonts);
|
|
177
|
-
}
|
|
178
|
-
else if (type?.value === 'Pages') {
|
|
179
|
-
const kids = nodeDict.get('Kids')?.as(PdfArray);
|
|
180
|
-
if (kids) {
|
|
181
|
-
for (const kidRef of kids.items) {
|
|
182
|
-
if (kidRef instanceof PdfObjectReference) {
|
|
183
|
-
await this.traversePageTreeForFonts(kidRef, fonts);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Extracts font information from a page's resources.
|
|
191
|
-
*/
|
|
192
|
-
async extractFontsFromPage(_pageObject, pageDict, fonts) {
|
|
193
|
-
// Get Resources - could be direct dict or reference
|
|
194
|
-
let resources = pageDict.get('Resources')?.as(PdfDictionary);
|
|
195
|
-
if (!resources) {
|
|
196
|
-
const resourcesRef = pageDict
|
|
197
|
-
.get('Resources')
|
|
198
|
-
?.as(PdfObjectReference);
|
|
199
|
-
if (resourcesRef) {
|
|
200
|
-
const resourcesObj = await this.document.readObject({
|
|
201
|
-
objectNumber: resourcesRef.objectNumber,
|
|
202
|
-
generationNumber: resourcesRef.generationNumber,
|
|
203
|
-
});
|
|
204
|
-
resources = resourcesObj?.content.as(PdfDictionary);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
if (!resources)
|
|
208
|
-
return;
|
|
209
|
-
// Get Font dictionary - could be direct dict or reference
|
|
210
|
-
let fontDict = resources.get('Font')?.as(PdfDictionary);
|
|
211
|
-
if (!fontDict) {
|
|
212
|
-
const fontRef = resources.get('Font')?.as(PdfObjectReference);
|
|
213
|
-
if (fontRef) {
|
|
214
|
-
const fontObj = await this.document.readObject({
|
|
215
|
-
objectNumber: fontRef.objectNumber,
|
|
216
|
-
generationNumber: fontRef.generationNumber,
|
|
217
|
-
});
|
|
218
|
-
fontDict = fontObj?.content.as(PdfDictionary);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
if (!fontDict)
|
|
222
|
-
return;
|
|
223
|
-
// Iterate through fonts in the Font dictionary
|
|
224
|
-
for (const [resourceName, fontValue] of fontDict.entries()) {
|
|
225
|
-
// Skip if already in this collection
|
|
226
|
-
if (fonts.has(resourceName))
|
|
227
|
-
continue;
|
|
228
|
-
let fontObjDict;
|
|
229
|
-
let fontIndirectObj;
|
|
230
|
-
const fontRef = fontValue.as(PdfObjectReference);
|
|
231
|
-
if (fontRef) {
|
|
232
|
-
const obj = await this.document.readObject({
|
|
233
|
-
objectNumber: fontRef.objectNumber,
|
|
234
|
-
generationNumber: fontRef.generationNumber,
|
|
235
|
-
});
|
|
236
|
-
fontObjDict = obj?.content.as(PdfDictionary);
|
|
237
|
-
fontIndirectObj = obj;
|
|
238
|
-
}
|
|
239
|
-
else {
|
|
240
|
-
fontObjDict = fontValue.as(PdfDictionary);
|
|
241
|
-
}
|
|
242
|
-
if (!fontObjDict || !fontIndirectObj)
|
|
243
|
-
continue;
|
|
244
|
-
const baseFont = fontObjDict.get('BaseFont')?.as(PdfName)?.value;
|
|
245
|
-
const encoding = fontObjDict.get('Encoding')?.as(PdfName)?.value;
|
|
246
|
-
if (baseFont) {
|
|
247
|
-
const pdfFont = new PdfFont({
|
|
248
|
-
fontName: baseFont,
|
|
249
|
-
resourceName: resourceName,
|
|
250
|
-
encoding: encoding,
|
|
251
|
-
manager: this,
|
|
252
|
-
container: fontIndirectObj,
|
|
253
|
-
});
|
|
254
|
-
fonts.set(resourceName, pdfFont);
|
|
255
|
-
// Also register by baseFont name for lookup convenience
|
|
256
|
-
if (!fonts.has(baseFont)) {
|
|
257
|
-
fonts.set(baseFont, pdfFont);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Searches the PDF structure for a font by name.
|
|
264
|
-
* @internal
|
|
265
|
-
*/
|
|
266
|
-
async searchFontInPdf(fontName) {
|
|
267
|
-
const fonts = await this.collectAllFontsFromPdf();
|
|
268
|
-
return fonts?.get(fontName);
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Collects all fonts from the PDF structure.
|
|
272
|
-
* @internal
|
|
273
|
-
*/
|
|
274
|
-
async collectAllFontsFromPdf() {
|
|
275
|
-
const fonts = new Map();
|
|
276
|
-
const catalog = this.document.root;
|
|
277
|
-
if (!catalog)
|
|
278
|
-
return fonts;
|
|
279
|
-
const pagesRef = catalog.content.get('Pages');
|
|
280
|
-
if (!pagesRef)
|
|
281
|
-
return fonts;
|
|
282
|
-
const pagesObjRef = pagesRef.as(PdfObjectReference);
|
|
283
|
-
if (!pagesObjRef)
|
|
284
|
-
return fonts;
|
|
285
|
-
await this.traversePageTreeForFonts(pagesObjRef, fonts);
|
|
286
|
-
return fonts;
|
|
287
|
-
}
|
|
288
|
-
/**
|
|
289
|
-
* Adds a font to the AcroForm default resources (DR) dictionary.
|
|
290
|
-
* This ensures fonts are available to form fields.
|
|
291
|
-
*/
|
|
292
|
-
async addFontToAcroFormResources(resourceName, fontObject) {
|
|
293
|
-
const catalog = this.document.root;
|
|
294
|
-
if (!catalog)
|
|
295
|
-
return;
|
|
296
|
-
// Get AcroForm dictionary
|
|
297
|
-
const acroFormRef = catalog.content.get('AcroForm');
|
|
298
|
-
if (!acroFormRef)
|
|
299
|
-
return;
|
|
300
|
-
let acroFormDict;
|
|
301
|
-
let acroFormContainer;
|
|
302
|
-
if (acroFormRef instanceof PdfObjectReference) {
|
|
303
|
-
const acroFormObject = await this.document.readObject({
|
|
304
|
-
objectNumber: acroFormRef.objectNumber,
|
|
305
|
-
generationNumber: acroFormRef.generationNumber,
|
|
306
|
-
});
|
|
307
|
-
if (acroFormObject) {
|
|
308
|
-
acroFormDict = acroFormObject.content.as(PdfDictionary);
|
|
309
|
-
acroFormContainer = acroFormObject;
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
else {
|
|
313
|
-
acroFormDict = acroFormRef.as(PdfDictionary);
|
|
314
|
-
}
|
|
315
|
-
if (!acroFormDict)
|
|
316
|
-
return;
|
|
317
|
-
// Get or create DR (Default Resources) dictionary
|
|
318
|
-
let dr = acroFormDict.get('DR')?.as(PdfDictionary);
|
|
319
|
-
if (!dr) {
|
|
320
|
-
dr = new PdfDictionary();
|
|
321
|
-
acroFormDict.set('DR', dr);
|
|
322
|
-
}
|
|
323
|
-
// Get or create Font dictionary within DR
|
|
324
|
-
let fontDict = dr.get('Font')?.as(PdfDictionary);
|
|
325
|
-
if (!fontDict) {
|
|
326
|
-
fontDict = new PdfDictionary();
|
|
327
|
-
dr.set('Font', fontDict);
|
|
328
|
-
}
|
|
329
|
-
// Add the font reference
|
|
330
|
-
fontDict.set(resourceName, fontObject.reference);
|
|
331
|
-
// Commit the modified AcroForm object if it's an indirect object
|
|
332
|
-
if (acroFormContainer) {
|
|
333
|
-
await this.document.commit(acroFormContainer);
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* Adds a font to the global /Pages node Resources dictionary.
|
|
338
|
-
* All child pages will inherit these fonts automatically.
|
|
339
|
-
* This is more efficient than adding to each individual page.
|
|
340
|
-
*/
|
|
341
|
-
async addFontToPageResources(resourceName, fontObject) {
|
|
342
|
-
const catalog = this.document.root;
|
|
343
|
-
const pagesRef = catalog.content.get('Pages');
|
|
344
|
-
if (!pagesRef)
|
|
345
|
-
return;
|
|
346
|
-
const pagesObjRef = pagesRef.as(PdfObjectReference);
|
|
347
|
-
if (!pagesObjRef)
|
|
348
|
-
return;
|
|
349
|
-
// Read the root /Pages object
|
|
350
|
-
const pagesObject = await this.document.readObject({
|
|
351
|
-
objectNumber: pagesObjRef.objectNumber,
|
|
352
|
-
generationNumber: pagesObjRef.generationNumber,
|
|
353
|
-
});
|
|
354
|
-
if (!pagesObject)
|
|
355
|
-
return;
|
|
356
|
-
const pagesDict = pagesObject.content.as(PdfDictionary);
|
|
357
|
-
if (!pagesDict)
|
|
358
|
-
return;
|
|
359
|
-
// Get or create Resources dictionary on the /Pages node
|
|
360
|
-
let resources = pagesDict.get('Resources')?.as(PdfDictionary);
|
|
361
|
-
if (!resources) {
|
|
362
|
-
resources = new PdfDictionary();
|
|
363
|
-
pagesDict.set('Resources', resources);
|
|
364
|
-
}
|
|
365
|
-
// Get or create Font dictionary within Resources
|
|
366
|
-
let fontDict = resources.get('Font')?.as(PdfDictionary);
|
|
367
|
-
if (!fontDict) {
|
|
368
|
-
fontDict = new PdfDictionary();
|
|
369
|
-
resources.set('Font', fontDict);
|
|
370
|
-
}
|
|
371
|
-
// Add the font reference to the global dictionary
|
|
372
|
-
fontDict.set(resourceName, fontObject.reference);
|
|
373
|
-
// Commit the modified /Pages object
|
|
374
|
-
await this.document.commit(pagesObject);
|
|
375
|
-
// Also add to AcroForm DR if AcroForm exists
|
|
376
|
-
await this.addFontToAcroFormResources(resourceName, fontObject);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { PdfDictionary } from '../core/objects/pdf-dictionary.js';
|
|
2
|
-
import { ByteArray, DecodeParms } from '../types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Handles PNG and TIFF predictor encoding and decoding for PDF streams.
|
|
5
|
-
* Predictors are used to improve compression efficiency by transforming
|
|
6
|
-
* image data before or after compression.
|
|
7
|
-
*/
|
|
8
|
-
export declare class Predictor {
|
|
9
|
-
/**
|
|
10
|
-
* Decodes data that was encoded with a predictor.
|
|
11
|
-
*
|
|
12
|
-
* @param data - The encoded data to decode.
|
|
13
|
-
* @param params - Optional decode parameters including Predictor, Columns, Colors, and BitsPerComponent.
|
|
14
|
-
* @returns The decoded byte array.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```typescript
|
|
18
|
-
* const decoded = Predictor.decode(encodedData, { Predictor: 12, Columns: 100 })
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
static decode(data: ByteArray, params?: DecodeParms): ByteArray;
|
|
22
|
-
/**
|
|
23
|
-
* Encodes data using a predictor algorithm.
|
|
24
|
-
*
|
|
25
|
-
* @param data - The data to encode.
|
|
26
|
-
* @param params - Optional encode parameters including Predictor, Columns, Colors, and BitsPerComponent.
|
|
27
|
-
* @param PdfStreamFilterType - The PNG filter type to use for encoding.
|
|
28
|
-
* @returns The encoded byte array.
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* ```typescript
|
|
32
|
-
* const encoded = Predictor.encode(rawData, { Predictor: 12, Columns: 100 }, 1)
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
static encode(data: ByteArray, params?: DecodeParms, PdfStreamFilterType?: number): ByteArray;
|
|
36
|
-
/**
|
|
37
|
-
* Decodes TIFF predictor encoded data.
|
|
38
|
-
*
|
|
39
|
-
* @param data - The TIFF encoded data.
|
|
40
|
-
* @param columns - The number of columns in the image.
|
|
41
|
-
* @param bpp - Bytes per pixel.
|
|
42
|
-
* @returns The decoded byte array.
|
|
43
|
-
*/
|
|
44
|
-
static tiffDecode(data: ByteArray, columns: number, bpp: number): ByteArray;
|
|
45
|
-
/**
|
|
46
|
-
* Encodes data using TIFF predictor.
|
|
47
|
-
*
|
|
48
|
-
* @param data - The data to encode.
|
|
49
|
-
* @param columns - The number of columns in the image.
|
|
50
|
-
* @param bpp - Bytes per pixel.
|
|
51
|
-
* @returns The TIFF encoded byte array.
|
|
52
|
-
*/
|
|
53
|
-
static tiffEncode(data: ByteArray, columns: number, bpp: number): ByteArray;
|
|
54
|
-
/**
|
|
55
|
-
* Decodes PNG predictor encoded data.
|
|
56
|
-
*
|
|
57
|
-
* @param data - The PNG encoded data.
|
|
58
|
-
* @param columns - The number of columns in the image.
|
|
59
|
-
* @param bpp - Bytes per pixel.
|
|
60
|
-
* @returns The decoded byte array.
|
|
61
|
-
* @throws Error if an unsupported PNG filter type is encountered.
|
|
62
|
-
*/
|
|
63
|
-
static pngDecode(data: ByteArray, columns: number, bpp: number): ByteArray;
|
|
64
|
-
/**
|
|
65
|
-
* Encodes data using PNG predictor.
|
|
66
|
-
*
|
|
67
|
-
* @param data - The data to encode.
|
|
68
|
-
* @param columns - The number of columns in the image.
|
|
69
|
-
* @param bpp - Bytes per pixel.
|
|
70
|
-
* @param PdfStreamFilterType - The PNG filter type (0-4) to use.
|
|
71
|
-
* @returns The PNG encoded byte array.
|
|
72
|
-
* @throws Error if an unsupported PNG filter type is specified.
|
|
73
|
-
*/
|
|
74
|
-
static pngEncode(data: ByteArray, columns: number, bpp: number, PdfStreamFilterType: number): ByteArray;
|
|
75
|
-
/**
|
|
76
|
-
* Implements the Paeth predictor algorithm used in PNG filtering.
|
|
77
|
-
*
|
|
78
|
-
* @param a - The left pixel value.
|
|
79
|
-
* @param b - The above pixel value.
|
|
80
|
-
* @param c - The upper-left pixel value.
|
|
81
|
-
* @returns The predicted pixel value.
|
|
82
|
-
*/
|
|
83
|
-
private static paethPredictor;
|
|
84
|
-
/**
|
|
85
|
-
* Extracts decode parameters from a PDF dictionary.
|
|
86
|
-
*
|
|
87
|
-
* @param decodeParms - Optional PDF dictionary containing decode parameters.
|
|
88
|
-
* @returns The decode parameters object or null if not applicable.
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```typescript
|
|
92
|
-
* const params = Predictor.getDecodeParms(dictionary)
|
|
93
|
-
* if (params) {
|
|
94
|
-
* console.log(params.Predictor)
|
|
95
|
-
* }
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
static getDecodeParms(decodeParms?: PdfDictionary): DecodeParms | null;
|
|
99
|
-
/**
|
|
100
|
-
* Checks if the decode parameters can be handled by this predictor.
|
|
101
|
-
*
|
|
102
|
-
* @param decodeParms - Optional PDF dictionary containing decode parameters.
|
|
103
|
-
* @returns True if the parameters can be handled, false otherwise.
|
|
104
|
-
*
|
|
105
|
-
* @example
|
|
106
|
-
* ```typescript
|
|
107
|
-
* if (Predictor.canHandleDecodeParms(dictionary)) {
|
|
108
|
-
* // Process with predictor
|
|
109
|
-
* }
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
static canHandleDecodeParms(decodeParms?: PdfDictionary): boolean;
|
|
113
|
-
}
|