pdf-lite 1.4.0 → 1.6.0

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 (144) hide show
  1. package/EXAMPLES.md +51 -70
  2. package/README.md +1 -1
  3. package/dist/acroform/appearance/index.d.ts +4 -4
  4. package/dist/acroform/appearance/index.js +4 -4
  5. package/dist/acroform/appearance/{PdfAppearanceStream.d.ts → pdf-appearance-stream.d.ts} +9 -3
  6. package/dist/acroform/appearance/{PdfAppearanceStream.js → pdf-appearance-stream.js} +14 -5
  7. package/dist/acroform/appearance/{PdfButtonAppearanceStream.d.ts → pdf-button-appearance-stream.d.ts} +3 -2
  8. package/dist/acroform/appearance/pdf-button-appearance-stream.js +58 -0
  9. package/dist/acroform/appearance/pdf-choice-appearance-stream.d.ts +22 -0
  10. package/dist/acroform/appearance/pdf-choice-appearance-stream.js +75 -0
  11. package/dist/acroform/appearance/pdf-graphics.d.ts +51 -0
  12. package/dist/acroform/appearance/pdf-graphics.js +239 -0
  13. package/dist/acroform/appearance/{PdfTextAppearanceStream.d.ts → pdf-text-appearance-stream.d.ts} +7 -2
  14. package/dist/acroform/appearance/pdf-text-appearance-stream.js +104 -0
  15. package/dist/acroform/fields/index.d.ts +7 -7
  16. package/dist/acroform/fields/index.js +7 -7
  17. package/dist/acroform/fields/pdf-button-form-field.d.ts +14 -0
  18. package/dist/acroform/fields/pdf-button-form-field.js +70 -0
  19. package/dist/acroform/fields/pdf-choice-form-field.d.ts +19 -0
  20. package/dist/acroform/fields/pdf-choice-form-field.js +112 -0
  21. package/dist/acroform/fields/{PdfFormFieldFlags.d.ts → pdf-form-field-flags.d.ts} +5 -6
  22. package/dist/acroform/fields/{PdfFormFieldFlags.js → pdf-form-field-flags.js} +12 -18
  23. package/dist/acroform/fields/{PdfFormField.d.ts → pdf-form-field.d.ts} +37 -38
  24. package/dist/acroform/fields/pdf-form-field.js +519 -0
  25. package/dist/acroform/fields/{PdfSignatureFormField.d.ts → pdf-signature-form-field.d.ts} +1 -1
  26. package/dist/acroform/fields/{PdfSignatureFormField.js → pdf-signature-form-field.js} +1 -1
  27. package/dist/acroform/fields/{PdfTextFormField.d.ts → pdf-text-form-field.d.ts} +1 -1
  28. package/dist/acroform/fields/{PdfTextFormField.js → pdf-text-form-field.js} +11 -13
  29. package/dist/acroform/fields/types.d.ts +6 -1
  30. package/dist/acroform/index.d.ts +1 -3
  31. package/dist/acroform/index.js +1 -3
  32. package/dist/acroform/pdf-acro-form.d.ts +45 -0
  33. package/dist/acroform/pdf-acro-form.js +203 -0
  34. package/dist/acroform/xfa/index.d.ts +3 -3
  35. package/dist/acroform/xfa/index.js +2 -2
  36. package/dist/acroform/xfa/{PdfXfaData.d.ts → pdf-xfa-data.d.ts} +4 -3
  37. package/dist/acroform/xfa/{PdfXfaData.js → pdf-xfa-data.js} +16 -12
  38. package/dist/acroform/xfa/pdf-xfa-form.d.ts +16 -0
  39. package/dist/acroform/xfa/pdf-xfa-form.js +34 -0
  40. package/dist/annotations/index.d.ts +3 -4
  41. package/dist/annotations/index.js +3 -4
  42. package/dist/annotations/{PdfAnnotationFlags.d.ts → pdf-annotation-flags.d.ts} +3 -4
  43. package/dist/annotations/{PdfAnnotationFlags.js → pdf-annotation-flags.js} +5 -6
  44. package/dist/annotations/{PdfAnnotation.d.ts → pdf-annotation.d.ts} +31 -5
  45. package/dist/annotations/{PdfAnnotation.js → pdf-annotation.js} +31 -19
  46. package/dist/annotations/pdf-default-resources.d.ts +11 -0
  47. package/dist/annotations/pdf-default-resources.js +3 -0
  48. package/dist/annotations/{PdfWidgetAnnotation.d.ts → pdf-widget-annotation.d.ts} +1 -1
  49. package/dist/annotations/{PdfWidgetAnnotation.js → pdf-widget-annotation.js} +1 -1
  50. package/dist/core/decoder.js +1 -1
  51. package/dist/core/objects/pdf-array.d.ts +8 -1
  52. package/dist/core/objects/pdf-array.js +31 -0
  53. package/dist/core/objects/pdf-dictionary.d.ts +2 -0
  54. package/dist/core/objects/pdf-dictionary.js +14 -7
  55. package/dist/core/objects/pdf-hexadecimal.d.ts +1 -0
  56. package/dist/core/objects/pdf-hexadecimal.js +3 -3
  57. package/dist/core/objects/pdf-indirect-object.d.ts +18 -9
  58. package/dist/core/objects/pdf-indirect-object.js +75 -16
  59. package/dist/core/objects/pdf-number.d.ts +1 -0
  60. package/dist/core/objects/pdf-number.js +5 -4
  61. package/dist/core/objects/pdf-object-reference.d.ts +8 -1
  62. package/dist/core/objects/pdf-object-reference.js +14 -0
  63. package/dist/core/objects/pdf-object.d.ts +14 -0
  64. package/dist/core/objects/pdf-object.js +36 -0
  65. package/dist/core/objects/pdf-start-xref.d.ts +1 -0
  66. package/dist/core/objects/pdf-start-xref.js +4 -0
  67. package/dist/core/objects/pdf-stream.d.ts +44 -7
  68. package/dist/core/objects/pdf-stream.js +284 -26
  69. package/dist/core/objects/pdf-string.d.ts +1 -0
  70. package/dist/core/objects/pdf-string.js +3 -6
  71. package/dist/core/objects/pdf-trailer.d.ts +1 -0
  72. package/dist/core/objects/pdf-trailer.js +6 -3
  73. package/dist/core/objects/pdf-xref-table.js +1 -1
  74. package/dist/core/parser/incremental-parser.d.ts +0 -13
  75. package/dist/core/parser/incremental-parser.js +1 -18
  76. package/dist/core/ref.d.ts +3 -1
  77. package/dist/core/ref.js +8 -5
  78. package/dist/core/streams/object-stream.d.ts +1 -1
  79. package/dist/core/streams/object-stream.js +1 -1
  80. package/dist/core/tokens/token.d.ts +2 -1
  81. package/dist/core/tokens/token.js +3 -0
  82. package/dist/errors.d.ts +22 -0
  83. package/dist/errors.js +24 -0
  84. package/dist/fonts/index.d.ts +0 -1
  85. package/dist/fonts/index.js +0 -1
  86. package/dist/fonts/pdf-font.d.ts +94 -32
  87. package/dist/fonts/pdf-font.js +301 -83
  88. package/dist/index.d.ts +1 -0
  89. package/dist/index.js +1 -0
  90. package/dist/pdf/index.d.ts +2 -1
  91. package/dist/pdf/index.js +2 -1
  92. package/dist/pdf/pdf-document.d.ts +61 -36
  93. package/dist/pdf/pdf-document.js +315 -117
  94. package/dist/pdf/pdf-page.d.ts +50 -0
  95. package/dist/pdf/pdf-page.js +144 -0
  96. package/dist/pdf/pdf-pages.d.ts +28 -0
  97. package/dist/pdf/pdf-pages.js +94 -0
  98. package/dist/pdf/pdf-reader.d.ts +5 -1
  99. package/dist/pdf/pdf-reader.js +36 -2
  100. package/dist/pdf/pdf-revision.d.ts +3 -3
  101. package/dist/pdf/pdf-revision.js +7 -7
  102. package/dist/pdf/pdf-xref-lookup.js +34 -14
  103. package/dist/signing/document-security-store.d.ts +14 -17
  104. package/dist/signing/document-security-store.js +19 -34
  105. package/dist/signing/signer.d.ts +23 -8
  106. package/dist/signing/signer.js +51 -17
  107. package/dist/utils/encodePdfText.d.ts +17 -0
  108. package/dist/utils/encodePdfText.js +61 -0
  109. package/dist/utils/index.d.ts +1 -2
  110. package/dist/utils/index.js +1 -2
  111. package/dist/utils/needsCentralWhitespace.d.ts +10 -0
  112. package/dist/utils/needsCentralWhitespace.js +34 -0
  113. package/package.json +3 -3
  114. package/dist/acroform/PdfAcroForm.d.ts +0 -63
  115. package/dist/acroform/PdfAcroForm.js +0 -279
  116. package/dist/acroform/PdfFontEncodingCache.d.ts +0 -16
  117. package/dist/acroform/PdfFontEncodingCache.js +0 -75
  118. package/dist/acroform/acroform.d.ts +0 -9
  119. package/dist/acroform/acroform.js +0 -7
  120. package/dist/acroform/appearance/PdfButtonAppearanceStream.js +0 -54
  121. package/dist/acroform/appearance/PdfChoiceAppearanceStream.d.ts +0 -15
  122. package/dist/acroform/appearance/PdfChoiceAppearanceStream.js +0 -48
  123. package/dist/acroform/appearance/PdfTextAppearanceStream.js +0 -75
  124. package/dist/acroform/fields/PdfButtonFormField.d.ts +0 -9
  125. package/dist/acroform/fields/PdfButtonFormField.js +0 -35
  126. package/dist/acroform/fields/PdfChoiceFormField.d.ts +0 -9
  127. package/dist/acroform/fields/PdfChoiceFormField.js +0 -46
  128. package/dist/acroform/fields/PdfFormField.js +0 -499
  129. package/dist/acroform/manager.d.ts +0 -33
  130. package/dist/acroform/manager.js +0 -51
  131. package/dist/acroform/xfa/PdfXfaForm.d.ts +0 -12
  132. package/dist/acroform/xfa/PdfXfaForm.js +0 -64
  133. package/dist/annotations/PdfAnnotationWriter.d.ts +0 -20
  134. package/dist/annotations/PdfAnnotationWriter.js +0 -76
  135. package/dist/fonts/font-manager.d.ts +0 -127
  136. package/dist/fonts/font-manager.js +0 -378
  137. package/dist/pdf/errors.d.ts +0 -6
  138. package/dist/pdf/errors.js +0 -6
  139. package/dist/utils/predictors.d.ts +0 -113
  140. package/dist/utils/predictors.js +0 -279
  141. /package/dist/acroform/fields/{PdfDefaultAppearance.d.ts → pdf-default-appearance.d.ts} +0 -0
  142. /package/dist/acroform/fields/{PdfDefaultAppearance.js → pdf-default-appearance.js} +0 -0
  143. /package/dist/utils/{IterableReadableStream.d.ts → iterable-readable-stream.d.ts} +0 -0
  144. /package/dist/utils/{IterableReadableStream.js → iterable-readable-stream.js} +0 -0
@@ -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
- }
@@ -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,6 +0,0 @@
1
- /**
2
- * Error thrown when attempting to access a compressed object
3
- * that requires decompression from an object stream.
4
- */
5
- export declare class FoundCompressedObjectError extends Error {
6
- }
@@ -1,6 +0,0 @@
1
- /**
2
- * Error thrown when attempting to access a compressed object
3
- * that requires decompression from an object stream.
4
- */
5
- export class FoundCompressedObjectError extends Error {
6
- }