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.
Files changed (93) hide show
  1. package/EXAMPLES.md +51 -70
  2. package/README.md +1 -1
  3. package/dist/acroform/appearance/pdf-button-appearance-stream.d.ts +1 -1
  4. package/dist/acroform/appearance/pdf-button-appearance-stream.js +6 -2
  5. package/dist/acroform/fields/pdf-button-form-field.d.ts +0 -9
  6. package/dist/acroform/fields/pdf-button-form-field.js +7 -39
  7. package/dist/acroform/fields/pdf-choice-form-field.d.ts +2 -1
  8. package/dist/acroform/fields/pdf-choice-form-field.js +19 -38
  9. package/dist/acroform/fields/pdf-default-appearance.js +1 -1
  10. package/dist/acroform/fields/pdf-form-field.d.ts +27 -32
  11. package/dist/acroform/fields/pdf-form-field.js +180 -94
  12. package/dist/acroform/fields/pdf-text-form-field.js +6 -33
  13. package/dist/acroform/fields/types.d.ts +1 -1
  14. package/dist/acroform/index.d.ts +0 -2
  15. package/dist/acroform/index.js +0 -2
  16. package/dist/acroform/pdf-acro-form.d.ts +12 -36
  17. package/dist/acroform/pdf-acro-form.js +111 -201
  18. package/dist/acroform/xfa/pdf-xfa-data.d.ts +4 -3
  19. package/dist/acroform/xfa/pdf-xfa-data.js +16 -12
  20. package/dist/acroform/xfa/pdf-xfa-form.d.ts +9 -4
  21. package/dist/acroform/xfa/pdf-xfa-form.js +17 -39
  22. package/dist/annotations/index.d.ts +0 -1
  23. package/dist/annotations/index.js +0 -1
  24. package/dist/annotations/pdf-annotation.d.ts +7 -2
  25. package/dist/annotations/pdf-annotation.js +30 -19
  26. package/dist/annotations/pdf-default-resources.d.ts +11 -0
  27. package/dist/annotations/pdf-default-resources.js +3 -0
  28. package/dist/core/decoder.js +1 -1
  29. package/dist/core/objects/pdf-array.d.ts +8 -1
  30. package/dist/core/objects/pdf-array.js +31 -0
  31. package/dist/core/objects/pdf-dictionary.d.ts +2 -0
  32. package/dist/core/objects/pdf-dictionary.js +14 -7
  33. package/dist/core/objects/pdf-hexadecimal.d.ts +1 -0
  34. package/dist/core/objects/pdf-hexadecimal.js +3 -3
  35. package/dist/core/objects/pdf-indirect-object.d.ts +18 -9
  36. package/dist/core/objects/pdf-indirect-object.js +75 -16
  37. package/dist/core/objects/pdf-number.d.ts +1 -0
  38. package/dist/core/objects/pdf-number.js +5 -4
  39. package/dist/core/objects/pdf-object-reference.d.ts +8 -1
  40. package/dist/core/objects/pdf-object-reference.js +14 -0
  41. package/dist/core/objects/pdf-object.d.ts +14 -0
  42. package/dist/core/objects/pdf-object.js +36 -0
  43. package/dist/core/objects/pdf-start-xref.d.ts +1 -0
  44. package/dist/core/objects/pdf-start-xref.js +4 -0
  45. package/dist/core/objects/pdf-stream.d.ts +47 -7
  46. package/dist/core/objects/pdf-stream.js +301 -32
  47. package/dist/core/objects/pdf-string.d.ts +1 -0
  48. package/dist/core/objects/pdf-string.js +3 -6
  49. package/dist/core/objects/pdf-trailer.d.ts +1 -0
  50. package/dist/core/objects/pdf-trailer.js +6 -3
  51. package/dist/core/objects/pdf-xref-table.js +1 -1
  52. package/dist/core/ref.d.ts +3 -1
  53. package/dist/core/ref.js +8 -5
  54. package/dist/core/tokens/token.d.ts +2 -1
  55. package/dist/core/tokens/token.js +3 -0
  56. package/dist/fonts/index.d.ts +0 -1
  57. package/dist/fonts/index.js +0 -1
  58. package/dist/fonts/pdf-font.d.ts +32 -27
  59. package/dist/fonts/pdf-font.js +115 -77
  60. package/dist/pdf/index.d.ts +2 -0
  61. package/dist/pdf/index.js +2 -0
  62. package/dist/pdf/pdf-document.d.ts +63 -37
  63. package/dist/pdf/pdf-document.js +351 -135
  64. package/dist/pdf/pdf-page.d.ts +50 -0
  65. package/dist/pdf/pdf-page.js +144 -0
  66. package/dist/pdf/pdf-pages.d.ts +28 -0
  67. package/dist/pdf/pdf-pages.js +94 -0
  68. package/dist/pdf/pdf-reader.d.ts +5 -1
  69. package/dist/pdf/pdf-reader.js +36 -2
  70. package/dist/pdf/pdf-revision.d.ts +3 -3
  71. package/dist/pdf/pdf-revision.js +7 -7
  72. package/dist/pdf/pdf-xref-lookup.js +34 -14
  73. package/dist/signing/document-security-store.d.ts +14 -17
  74. package/dist/signing/document-security-store.js +19 -34
  75. package/dist/signing/signer.d.ts +23 -8
  76. package/dist/signing/signer.js +51 -17
  77. package/dist/utils/index.d.ts +0 -1
  78. package/dist/utils/index.js +0 -1
  79. package/dist/utils/needsCentralWhitespace.d.ts +10 -0
  80. package/dist/utils/needsCentralWhitespace.js +34 -0
  81. package/package.json +3 -3
  82. package/dist/acroform/acroform.d.ts +0 -9
  83. package/dist/acroform/acroform.js +0 -7
  84. package/dist/acroform/manager.d.ts +0 -37
  85. package/dist/acroform/manager.js +0 -57
  86. package/dist/acroform/pdf-font-encoding-cache.d.ts +0 -27
  87. package/dist/acroform/pdf-font-encoding-cache.js +0 -188
  88. package/dist/annotations/pdf-annotation-writer.d.ts +0 -20
  89. package/dist/annotations/pdf-annotation-writer.js +0 -76
  90. package/dist/fonts/manager.d.ts +0 -127
  91. package/dist/fonts/manager.js +0 -378
  92. package/dist/utils/predictors.d.ts +0 -113
  93. package/dist/utils/predictors.js +0 -279
@@ -1,188 +0,0 @@
1
- import { PdfDictionary } from '../core/objects/pdf-dictionary.js';
2
- import { PdfArray } from '../core/objects/pdf-array.js';
3
- import { PdfName } from '../core/objects/pdf-name.js';
4
- import { PdfString } from '../core/objects/pdf-string.js';
5
- import { PdfObjectReference } from '../core/objects/pdf-object-reference.js';
6
- import { buildEncodingMap } from '../utils/decodeWithFontEncoding.js';
7
- /**
8
- * Resolves and caches font encoding maps from the form's default resources.
9
- */
10
- export class PdfFontEncodingCache {
11
- fontEncodingMaps = new Map();
12
- fontTypes = new Map();
13
- /** Object references for all resolved fonts, keyed by resource name. */
14
- fontRefs = new Map();
15
- document;
16
- defaultResources;
17
- constructor(document, defaultResources) {
18
- this.document = document;
19
- this.defaultResources = defaultResources;
20
- }
21
- async getFontEncodingMap(fontName) {
22
- if (this.fontEncodingMaps.has(fontName)) {
23
- return this.fontEncodingMaps.get(fontName);
24
- }
25
- const dr = this.defaultResources;
26
- if (!dr)
27
- return null;
28
- const fontDictEntry = dr.get('Font');
29
- const fonts = fontDictEntry instanceof PdfDictionary ? fontDictEntry : null;
30
- if (!fonts)
31
- return null;
32
- const fontEntry = fonts.get(fontName);
33
- const fontRef = fontEntry instanceof PdfObjectReference ? fontEntry : null;
34
- if (!fontRef)
35
- return null;
36
- this.fontRefs.set(fontName, fontRef);
37
- const fontObj = await this.document?.readObject(fontRef);
38
- if (!fontObj)
39
- return null;
40
- const fontDict = fontObj.content instanceof PdfDictionary ? fontObj.content : null;
41
- if (!fontDict)
42
- return null;
43
- // Cache font subtype (Type0, TrueType, Type1, etc.)
44
- const subtypeEntry = fontDict.get('Subtype');
45
- const subtype = subtypeEntry instanceof PdfName ? subtypeEntry.value : undefined;
46
- if (subtype) {
47
- this.fontTypes.set(fontName, subtype);
48
- }
49
- const encoding = fontDict.get('Encoding');
50
- let encodingDict = null;
51
- if (encoding instanceof PdfObjectReference) {
52
- const encodingObj = await this.document?.readObject(encoding);
53
- encodingDict =
54
- encodingObj?.content instanceof PdfDictionary
55
- ? encodingObj.content
56
- : null;
57
- }
58
- else if (encoding instanceof PdfDictionary) {
59
- encodingDict = encoding;
60
- }
61
- if (!encodingDict)
62
- return null;
63
- const differences = encodingDict.get('Differences')?.as(PdfArray);
64
- if (!differences)
65
- return null;
66
- const encodingMap = buildEncodingMap(differences);
67
- if (!encodingMap)
68
- return null;
69
- this.fontEncodingMaps.set(fontName, encodingMap);
70
- return encodingMap;
71
- }
72
- async cacheAllFontEncodings(fields) {
73
- // Collect font names with an associated page ref for the first field
74
- // that uses them (needed to fall back to page resources).
75
- const fontPageRefs = new Map();
76
- for (const field of fields) {
77
- const daEntry = field.content.get('DA');
78
- const da = daEntry instanceof PdfString ? daEntry.value : undefined;
79
- if (da) {
80
- const fontMatch = da.match(/\/(\w+)\s+[\d.]+\s+Tf/);
81
- if (fontMatch) {
82
- const fontName = fontMatch[1];
83
- if (!fontPageRefs.has(fontName)) {
84
- const pageEntry = field.content.get('P');
85
- const pageRef = pageEntry instanceof PdfObjectReference
86
- ? pageEntry
87
- : null;
88
- fontPageRefs.set(fontName, pageRef);
89
- }
90
- }
91
- }
92
- }
93
- for (const [fontName, pageRef] of fontPageRefs) {
94
- await this.getFontEncodingMap(fontName);
95
- // If the font was not in the AcroForm DR, try the field's page resources
96
- if (!this.fontRefs.has(fontName) && pageRef) {
97
- await this.resolveFontFromPage(fontName, pageRef);
98
- }
99
- }
100
- }
101
- /**
102
- * Walks the page-tree (page → parent → … → Pages root), looking for the
103
- * font in each node's /Resources/Font dict. Stops as soon as it finds the
104
- * font. PDF resource inheritance means any ancestor can supply the font.
105
- */
106
- async resolveFontFromPage(fontName, pageRef) {
107
- if (!this.document)
108
- return;
109
- // Walk up the parent chain (max 16 levels to avoid infinite loops)
110
- let currentRef = pageRef;
111
- for (let depth = 0; depth < 16 && currentRef; depth++) {
112
- const nodeObj = await this.document.readObject({
113
- objectNumber: currentRef.objectNumber,
114
- generationNumber: currentRef.generationNumber,
115
- });
116
- const nodeDict = nodeObj?.content instanceof PdfDictionary
117
- ? nodeObj.content
118
- : null;
119
- if (!nodeDict)
120
- break;
121
- // Try Resources.Font in this node
122
- const resourcesEntry = nodeDict.get('Resources');
123
- const resources = resourcesEntry instanceof PdfDictionary ? resourcesEntry : null;
124
- if (resources) {
125
- const fontsEntry = resources.get('Font');
126
- const fonts = fontsEntry instanceof PdfDictionary ? fontsEntry : null;
127
- if (fonts) {
128
- const fontEntry = fonts.get(fontName);
129
- const fontRef = fontEntry instanceof PdfObjectReference
130
- ? fontEntry
131
- : null;
132
- if (fontRef) {
133
- // Found — cache and finish
134
- currentRef = fontRef;
135
- break;
136
- }
137
- }
138
- }
139
- // Walk to parent
140
- const parentEntry = nodeDict.get('Parent');
141
- currentRef =
142
- parentEntry instanceof PdfObjectReference ? parentEntry : null;
143
- }
144
- const fontRef = currentRef;
145
- if (!fontRef)
146
- return;
147
- this.fontRefs.set(fontName, fontRef);
148
- const fontObj = await this.document.readObject({
149
- objectNumber: fontRef.objectNumber,
150
- generationNumber: fontRef.generationNumber,
151
- });
152
- const fontDict = fontObj?.content instanceof PdfDictionary ? fontObj.content : null;
153
- if (!fontDict)
154
- return;
155
- const subtypeEntry = fontDict.get('Subtype');
156
- const subtype = subtypeEntry instanceof PdfName ? subtypeEntry.value : undefined;
157
- if (subtype)
158
- this.fontTypes.set(fontName, subtype);
159
- // Also resolve encoding map if present
160
- const encoding = fontDict.get('Encoding');
161
- let encodingDict = null;
162
- if (encoding instanceof PdfObjectReference) {
163
- const encodingObj = await this.document.readObject({
164
- objectNumber: encoding.objectNumber,
165
- generationNumber: encoding.generationNumber,
166
- });
167
- encodingDict =
168
- encodingObj?.content instanceof PdfDictionary
169
- ? encodingObj.content
170
- : null;
171
- }
172
- else if (encoding instanceof PdfDictionary) {
173
- encodingDict = encoding;
174
- }
175
- if (!encodingDict)
176
- return;
177
- const differencesEntry = encodingDict.get('Differences');
178
- if (!(differencesEntry instanceof PdfArray))
179
- return;
180
- const encodingMap = buildEncodingMap(differencesEntry);
181
- if (encodingMap) {
182
- this.fontEncodingMaps.set(fontName, encodingMap);
183
- }
184
- }
185
- isFontUnicode(fontName) {
186
- return this.fontTypes.get(fontName) === 'Type0';
187
- }
188
- }
@@ -1,20 +0,0 @@
1
- import { PdfDocument } from '../pdf/pdf-document.js';
2
- import { PdfDictionary } from '../core/objects/pdf-dictionary.js';
3
- import { PdfArray } from '../core/objects/pdf-array.js';
4
- import { PdfObjectReference } from '../core/objects/pdf-object-reference.js';
5
- /**
6
- * Manages page Annots arrays during AcroForm write operations.
7
- */
8
- export declare class PdfAnnotationWriter {
9
- static getPageAnnotsArray(document: PdfDocument, pageDict: PdfDictionary): Promise<{
10
- annotsArray: PdfArray<PdfObjectReference>;
11
- isIndirect: boolean;
12
- objectNumber?: number;
13
- generationNumber?: number;
14
- }>;
15
- static addFieldsToAnnots(annotsArray: PdfArray<PdfObjectReference>, fieldRefs: PdfObjectReference[]): void;
16
- static updatePageAnnotations(document: PdfDocument, fieldsByPage: Map<string, {
17
- pageRef: PdfObjectReference;
18
- fieldRefs: PdfObjectReference[];
19
- }>): Promise<void>;
20
- }
@@ -1,76 +0,0 @@
1
- import { PdfDictionary } from '../core/objects/pdf-dictionary.js';
2
- import { PdfArray } from '../core/objects/pdf-array.js';
3
- import { PdfObjectReference } from '../core/objects/pdf-object-reference.js';
4
- import { PdfIndirectObject } from '../core/objects/pdf-indirect-object.js';
5
- /**
6
- * Manages page Annots arrays during AcroForm write operations.
7
- */
8
- export class PdfAnnotationWriter {
9
- static async getPageAnnotsArray(document, pageDict) {
10
- const annotsRef = pageDict.get('Annots');
11
- if (annotsRef instanceof PdfObjectReference) {
12
- const annotsObj = await document.readObject({
13
- objectNumber: annotsRef.objectNumber,
14
- generationNumber: annotsRef.generationNumber,
15
- });
16
- return {
17
- annotsArray: annotsObj.content
18
- .as((PdfArray))
19
- .clone(),
20
- isIndirect: true,
21
- objectNumber: annotsRef.objectNumber,
22
- generationNumber: annotsRef.generationNumber,
23
- };
24
- }
25
- else if (annotsRef instanceof PdfArray) {
26
- return {
27
- annotsArray: annotsRef.as((PdfArray)).clone(),
28
- isIndirect: false,
29
- };
30
- }
31
- else {
32
- const newArray = new PdfArray();
33
- pageDict.set('Annots', newArray);
34
- return {
35
- annotsArray: newArray,
36
- isIndirect: false,
37
- };
38
- }
39
- }
40
- static addFieldsToAnnots(annotsArray, fieldRefs) {
41
- for (const fieldRef of fieldRefs) {
42
- const exists = annotsArray.items.some((ref) => ref.equals(fieldRef));
43
- if (!exists) {
44
- annotsArray.push(fieldRef);
45
- }
46
- }
47
- }
48
- static async updatePageAnnotations(document, fieldsByPage) {
49
- for (const { pageRef, fieldRefs } of fieldsByPage.values()) {
50
- const pageObj = await document.readObject({
51
- objectNumber: pageRef.objectNumber,
52
- generationNumber: pageRef.generationNumber,
53
- });
54
- if (!pageObj)
55
- continue;
56
- const pageDict = pageObj.content.as(PdfDictionary).clone();
57
- const annotsInfo = await PdfAnnotationWriter.getPageAnnotsArray(document, pageDict);
58
- PdfAnnotationWriter.addFieldsToAnnots(annotsInfo.annotsArray, fieldRefs);
59
- if (annotsInfo.isIndirect &&
60
- annotsInfo.objectNumber !== undefined) {
61
- const annotsIndirect = new PdfIndirectObject({
62
- objectNumber: annotsInfo.objectNumber,
63
- generationNumber: annotsInfo.generationNumber,
64
- content: annotsInfo.annotsArray,
65
- });
66
- document.add(annotsIndirect);
67
- }
68
- const pageIndirect = new PdfIndirectObject({
69
- objectNumber: pageRef.objectNumber,
70
- generationNumber: pageRef.generationNumber,
71
- content: pageDict,
72
- });
73
- document.add(pageIndirect);
74
- }
75
- }
76
- }
@@ -1,127 +0,0 @@
1
- import { PdfDocument } from '../pdf/pdf-document.js';
2
- import { ByteArray } from '../types.js';
3
- import { FontDescriptor, UnicodeFontDescriptor } from './types.js';
4
- import { PdfFont } from './pdf-font.js';
5
- /**
6
- * Manages font embedding in PDF documents.
7
- * Provides methods to embed TrueType and other font formats.
8
- */
9
- export declare class PdfFontManager {
10
- private document;
11
- private fontResourceCounter;
12
- constructor(document: PdfDocument);
13
- findFontByName(fontName: string): Promise<PdfFont | undefined>;
14
- /**
15
- * Embeds a TrueType font into the PDF document.
16
- *
17
- * @param fontData - The font file bytes
18
- * @param fontName - The name to use for this font in the PDF
19
- * @param descriptor - Font metrics and properties
20
- * @returns A PdfFont object representing the embedded font
21
- */
22
- embedTrueTypeFont(fontData: ByteArray, fontName: string, descriptor: FontDescriptor): Promise<PdfFont>;
23
- /**
24
- * Embeds a standard PDF font (Type1).
25
- * These fonts don't require font data as they're built into PDF viewers.
26
- *
27
- * @param fontName - One of the 14 standard PDF fonts
28
- * @returns A PdfFont object representing the embedded font
29
- */
30
- embedStandardFont(fontName: 'Helvetica' | 'Helvetica-Bold' | 'Helvetica-Oblique' | 'Helvetica-BoldOblique' | 'Times-Roman' | 'Times-Bold' | 'Times-Italic' | 'Times-BoldItalic' | 'Courier' | 'Courier-Bold' | 'Courier-Oblique' | 'Courier-BoldOblique' | 'Symbol' | 'ZapfDingbats'): Promise<PdfFont>;
31
- /**
32
- * Embeds a font from file data with automatic parsing and configuration.
33
- * This is the recommended high-level API for embedding fonts.
34
- *
35
- * @param fontData - The font file bytes (TTF, OTF, or WOFF format)
36
- * @param options - Optional configuration
37
- * @param options.fontName - Custom font name (defaults to PostScript name from font)
38
- * @param options.unicode - Use Unicode/Type0 encoding for non-ASCII characters
39
- * @param options.unicodeMappings - Custom CID to Unicode mappings for Type0 fonts
40
- * @returns A PdfFont object representing the embedded font
41
- *
42
- * @example
43
- * ```typescript
44
- * // Simple embedding with auto-generated name
45
- * const font = await document.fonts.embedFromFile(fontData)
46
- * field.font = font
47
- *
48
- * // With custom name and Unicode support
49
- * const font = await document.fonts.embedFromFile(fontData, {
50
- * fontName: 'MyCustomFont',
51
- * unicode: true
52
- * })
53
- * ```
54
- */
55
- embedFromFile(fontData: ByteArray, options?: {
56
- fontName?: string;
57
- unicode?: boolean;
58
- unicodeMappings?: Map<number, number>;
59
- }): Promise<PdfFont>;
60
- /**
61
- * Gets the font reference by font name or resource name.
62
- */
63
- getFont(fontName: string): Promise<PdfFont | undefined>;
64
- /**
65
- * Writes a font to the PDF document.
66
- * Assigns resource name, creates container object, commits all objects,
67
- * and registers it in page resources.
68
- *
69
- * @param font - The PdfFont instance to write
70
- * @param cacheKey - Unused, kept for API compatibility
71
- * @returns The font with its resourceName and container set
72
- * @internal
73
- */
74
- write(font: PdfFont): Promise<PdfFont>;
75
- /**
76
- * Gets all embedded fonts.
77
- * Searches the PDF structure to find all fonts.
78
- */
79
- getAllFonts(): Promise<Map<string, PdfFont>>;
80
- /**
81
- * Embeds a TrueType font with Unicode/Type0 support.
82
- * Use this for fonts that need to display non-ASCII characters.
83
- *
84
- * @param fontData - The font file bytes
85
- * @param fontName - The name to use for this font in the PDF
86
- * @param descriptor - Unicode font descriptor with CID metrics
87
- * @param unicodeMappings - Map of CID to Unicode code point for ToUnicode CMap
88
- * @returns A PdfFont object representing the embedded font
89
- */
90
- embedTrueTypeFontUnicode(fontData: ByteArray, fontName: string, descriptor: UnicodeFontDescriptor, unicodeMappings?: Map<number, number>): Promise<PdfFont>;
91
- /**
92
- * Loads existing fonts from the PDF document.
93
- * Traverses the page tree and extracts font information from page resources.
94
- *
95
- * @returns Map of font names to their PdfFont objects
96
- */
97
- loadExistingFonts(): Promise<Map<string, PdfFont>>;
98
- /**
99
- * Traverses the page tree to find and load existing fonts.
100
- */
101
- private traversePageTreeForFonts;
102
- /**
103
- * Extracts font information from a page's resources.
104
- */
105
- private extractFontsFromPage;
106
- /**
107
- * Searches the PDF structure for a font by name.
108
- * @internal
109
- */
110
- private searchFontInPdf;
111
- /**
112
- * Collects all fonts from the PDF structure.
113
- * @internal
114
- */
115
- private collectAllFontsFromPdf;
116
- /**
117
- * Adds a font to the AcroForm default resources (DR) dictionary.
118
- * This ensures fonts are available to form fields.
119
- */
120
- private addFontToAcroFormResources;
121
- /**
122
- * Adds a font to the global /Pages node Resources dictionary.
123
- * All child pages will inherit these fonts automatically.
124
- * This is more efficient than adding to each individual page.
125
- */
126
- private addFontToPageResources;
127
- }