modern-pdf-lib 0.15.1 → 0.22.9

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 (95) hide show
  1. package/README.md +119 -9
  2. package/dist/batchOptimize-Ba_pWw71.cjs +427 -0
  3. package/dist/batchOptimize-CxyY4fZe.mjs +392 -0
  4. package/dist/{bridge-DpzMOnHd.mjs → bridge-DTH5LMAK.mjs} +3 -3
  5. package/dist/{bridge-DN7BOHRW.cjs → bridge-DYCQzxF7.cjs} +2 -2
  6. package/dist/browser.cjs +621 -0
  7. package/dist/browser.d.cts +190 -0
  8. package/dist/browser.d.cts.map +1 -0
  9. package/dist/browser.d.mts +190 -0
  10. package/dist/browser.d.mts.map +1 -0
  11. package/dist/browser.mjs +212 -0
  12. package/dist/cli/index.cjs +40 -18
  13. package/dist/cli/index.mjs +40 -18
  14. package/dist/compressionAnalysis-B84FPXaQ.cjs +1525 -0
  15. package/dist/compressionAnalysis-BBv4BkQP.d.mts +261 -0
  16. package/dist/compressionAnalysis-BBv4BkQP.d.mts.map +1 -0
  17. package/dist/compressionAnalysis-ChkscEa1.mjs +1490 -0
  18. package/dist/compressionAnalysis-CtJ2X9l2.d.cts +261 -0
  19. package/dist/compressionAnalysis-CtJ2X9l2.d.cts.map +1 -0
  20. package/dist/create.cjs +35 -0
  21. package/dist/create.d.cts +3 -0
  22. package/dist/create.d.mts +3 -0
  23. package/dist/create.mjs +5 -0
  24. package/dist/{deduplicateImages-BfpjHY9b.mjs → deduplicateImages-CmTeo6Tx.mjs} +3 -3
  25. package/dist/{deduplicateImages-BtJ5tlrr.cjs → deduplicateImages-cKsnD6Ep.cjs} +2 -2
  26. package/dist/{fflateAdapter-D2mv_ttM.mjs → fflateAdapter-CBQpGTlx.mjs} +2 -2
  27. package/dist/{fflateAdapter-cT4YeY_h.cjs → fflateAdapter-LTAeAhaD.cjs} +1 -1
  28. package/dist/fieldAppearance-C8PoLFSc.d.mts +136 -0
  29. package/dist/fieldAppearance-C8PoLFSc.d.mts.map +1 -0
  30. package/dist/fieldAppearance-CdiGFG5e.d.cts +136 -0
  31. package/dist/fieldAppearance-CdiGFG5e.d.cts.map +1 -0
  32. package/dist/fontEmbed-Dsu9fo4U.d.mts +636 -0
  33. package/dist/fontEmbed-Dsu9fo4U.d.mts.map +1 -0
  34. package/dist/fontEmbed-LID6yG6g.d.cts +636 -0
  35. package/dist/fontEmbed-LID6yG6g.d.cts.map +1 -0
  36. package/dist/{fontSubset-BxsF9Tu5.cjs → fontSubset-5SLWMmEw.cjs} +1 -1
  37. package/dist/{fontSubset-ClyTXlhY.mjs → fontSubset-DWpduoY2.mjs} +2 -2
  38. package/dist/forms.cjs +13 -0
  39. package/dist/forms.d.cts +3 -0
  40. package/dist/forms.d.mts +3 -0
  41. package/dist/forms.mjs +3 -0
  42. package/dist/grayscaleDetect-C2m-eEXR.cjs +96 -0
  43. package/dist/grayscaleDetect-C6kFF3dk.mjs +84 -0
  44. package/dist/imageExtract-Dnk_Ssv7.mjs +155 -0
  45. package/dist/imageExtract-zEb1gnkb.cjs +166 -0
  46. package/dist/index-BtYOx5wh.d.mts +4904 -0
  47. package/dist/index-BtYOx5wh.d.mts.map +1 -0
  48. package/dist/index-bpktKzCA.d.cts +4904 -0
  49. package/dist/index-bpktKzCA.d.cts.map +1 -0
  50. package/dist/index.cjs +274 -20704
  51. package/dist/index.d.cts +7 -9151
  52. package/dist/index.d.mts +7 -9151
  53. package/dist/index.mjs +17 -20532
  54. package/dist/layout-CuAVk_Or.cjs +563 -0
  55. package/dist/layout-DgX_0jfK.mjs +438 -0
  56. package/dist/{libdeflateWasm-Cg7cWHOq.cjs → libdeflateWasm-BdiDEJOj.cjs} +2 -2
  57. package/dist/{libdeflateWasm-Cmxa-yiS.mjs → libdeflateWasm-rLppXytE.mjs} +3 -3
  58. package/dist/loader-3u6Tw5T-.mjs +328 -0
  59. package/dist/loader-I4zdkoWc.cjs +393 -0
  60. package/dist/parse.cjs +24 -0
  61. package/dist/parse.d.cts +4 -0
  62. package/dist/parse.d.mts +4 -0
  63. package/dist/parse.mjs +7 -0
  64. package/dist/{pdfCatalog-BcOL6QF-.cjs → pdfCatalog-CYy4NXEY.cjs} +2 -2
  65. package/dist/{pdfCatalog-CnJRovvm.mjs → pdfCatalog-IImGcMbR.mjs} +3 -3
  66. package/dist/pdfDocument-BFxHD_2u.mjs +13755 -0
  67. package/dist/pdfDocument-BSiQdNZq.d.cts +4640 -0
  68. package/dist/pdfDocument-BSiQdNZq.d.cts.map +1 -0
  69. package/dist/pdfDocument-i6U5fQ91.d.mts +4640 -0
  70. package/dist/pdfDocument-i6U5fQ91.d.mts.map +1 -0
  71. package/dist/pdfDocument-pmRXryVI.cjs +14180 -0
  72. package/dist/pdfForm-9gd40uz9.cjs +1796 -0
  73. package/dist/pdfForm-BiyNtYem.d.mts +905 -0
  74. package/dist/pdfForm-BiyNtYem.d.mts.map +1 -0
  75. package/dist/pdfForm-Cn-cVicP.mjs +1695 -0
  76. package/dist/pdfForm-SOXJ72LW.d.cts +905 -0
  77. package/dist/pdfForm-SOXJ72LW.d.cts.map +1 -0
  78. package/dist/{pdfObjects-BrU4Xd0V.cjs → pdfObjects-1veop1_d.cjs} +2 -2
  79. package/dist/{pdfObjects-DZZ2GPRW.mjs → pdfObjects-uEsWlfzU.mjs} +2 -2
  80. package/dist/{pdfPage-Dm5XC_g_.mjs → pdfPage-Cd8e7flb.mjs} +3024 -5
  81. package/dist/{pdfPage-Dz_SVKUS.cjs → pdfPage-Cd8jOJp6.cjs} +3046 -3
  82. package/dist/{pngEmbed-I1hU3Y6m.mjs → pngEmbed-BLj2zi-5.mjs} +3 -3
  83. package/dist/{pngEmbed-C6M1eX6b.cjs → pngEmbed-D4X4ZN-3.cjs} +2 -2
  84. package/dist/src-6L07EQsi.cjs +11852 -0
  85. package/dist/src-Dm4aaZ8q.mjs +11103 -0
  86. package/dist/{imageExtract-vjyQyFcT.mjs → streamDecode-Bj568Nc9.mjs} +1646 -188
  87. package/dist/{imageExtract-BC7TMY98.cjs → streamDecode-CvgErkFu.cjs} +1645 -199
  88. package/package.json +33 -1
  89. package/dist/batchOptimize-ClXizv19.mjs +0 -306
  90. package/dist/batchOptimize-DYQOX1-7.cjs +0 -329
  91. package/dist/index.d.cts.map +0 -1
  92. package/dist/index.d.mts.map +0 -1
  93. package/dist/loader-B6VIrZOJ.mjs +0 -164
  94. package/dist/loader-DdB5Xo5D.cjs +0 -166
  95. package/dist/rolldown-runtime-95iHPtFO.mjs +0 -18
@@ -0,0 +1,261 @@
1
+ import { n as PdfDocument } from "./pdfDocument-i6U5fQ91.mjs";
2
+ import { C as PdfRef, _ as PdfDict, v as PdfName, w as PdfStream } from "./pdfForm-BiyNtYem.mjs";
3
+
4
+ //#region src/parser/contentStreamParser.d.ts
5
+ /**
6
+ * A single operand value in a content stream.
7
+ *
8
+ * - `number` — integer or real
9
+ * - `string` — literal `(…)` or hex `<…>` string (decoded to a JS string)
10
+ * - `boolean` — `true` / `false`
11
+ * - `null` — the PDF `null` keyword
12
+ * - `PdfName` — a `/Name`
13
+ * - `Operand[]` — a PDF array `[…]`
14
+ */
15
+ type Operand = number | string | boolean | null | PdfName | Operand[];
16
+ /**
17
+ * A parsed content-stream operator with its preceding operands.
18
+ */
19
+ interface ContentStreamOperator {
20
+ /** The operator keyword, e.g. `"BT"`, `"Tf"`, `"Tj"`, `"re"`, `"cm"`. */
21
+ operator: string;
22
+ /** The operand values that preceded this operator. */
23
+ operands: Operand[];
24
+ }
25
+ /**
26
+ * Parse a PDF content stream into an ordered list of operators.
27
+ *
28
+ * @param data - The raw content-stream bytes (already decompressed).
29
+ * @returns An array of operators in document order.
30
+ */
31
+ declare function parseContentStream(data: Uint8Array): ContentStreamOperator[];
32
+ //#endregion
33
+ //#region src/parser/textExtractor.d.ts
34
+ /**
35
+ * A single extracted text item with position and font information.
36
+ */
37
+ interface TextItem {
38
+ /** The extracted text string. */
39
+ text: string;
40
+ /** Horizontal position in user-space units. */
41
+ x: number;
42
+ /** Vertical position in user-space units. */
43
+ y: number;
44
+ /** Approximate width of the text in user-space units. */
45
+ width: number;
46
+ /** Approximate height of the text in user-space units (based on font size). */
47
+ height: number;
48
+ /** Font size in user-space units. */
49
+ fontSize: number;
50
+ /** Font resource name (e.g. `"/F1"`). */
51
+ fontName: string;
52
+ }
53
+ /**
54
+ * Options for text extraction.
55
+ */
56
+ interface TextExtractionOptions {
57
+ /**
58
+ * Include position information.
59
+ * Default: `false` for performance.
60
+ */
61
+ withPositions?: boolean | undefined;
62
+ }
63
+ /**
64
+ * Extract plain text from a sequence of parsed content-stream operators.
65
+ *
66
+ * This function concatenates all text-showing operator strings, inserting
67
+ * spaces between text objects (BT/ET blocks) and newlines at line breaks
68
+ * (`T*`, `Td`, `TD`).
69
+ *
70
+ * @param operators - Parsed content-stream operators.
71
+ * @param resources - Optional page `/Resources` dictionary (used to look
72
+ * up font encodings and ToUnicode CMaps).
73
+ * @param options - Extraction options.
74
+ * @returns The extracted text as a single string.
75
+ */
76
+ declare function extractText(operators: ContentStreamOperator[], resources?: PdfDict, options?: TextExtractionOptions): string;
77
+ /**
78
+ * Extract text with position information from a parsed content stream.
79
+ *
80
+ * Each returned {@link TextItem} includes the text string, its position
81
+ * (x, y), dimensions (width, height), font size, and font name.
82
+ *
83
+ * @param operators - Parsed content-stream operators.
84
+ * @param resources - Optional page `/Resources` dictionary.
85
+ * @returns An array of positioned text items.
86
+ */
87
+ declare function extractTextWithPositions(operators: ContentStreamOperator[], resources?: PdfDict): TextItem[];
88
+ //#endregion
89
+ //#region src/parser/streamDecode.d.ts
90
+ /**
91
+ * Decode (decompress) PDF stream data that may have one or more filters
92
+ * applied. Filters are applied in the order they appear in the array
93
+ * (first entry is the outermost encoding, decoded first).
94
+ *
95
+ * @param data - The raw (encoded) stream bytes.
96
+ * @param filters - A single filter name or an ordered array of filter
97
+ * names (e.g. `"FlateDecode"` or
98
+ * `["ASCIIHexDecode", "FlateDecode"]`).
99
+ * @param decodeParms - Optional decode parameters — a single `PdfDict` or
100
+ * an array of `PdfDict | null` parallel to `filters`.
101
+ * @returns The fully decoded bytes.
102
+ */
103
+ declare function decodeStream(data: Uint8Array, filters: string | string[], decodeParms?: PdfDict | PdfDict[] | null): Uint8Array;
104
+ //#endregion
105
+ //#region src/assets/image/imageExtract.d.ts
106
+ /**
107
+ * Information about a single image XObject in a PDF document.
108
+ */
109
+ interface ImageInfo {
110
+ /** The PdfStream object for this image (can be mutated for in-place optimization). */
111
+ readonly stream: PdfStream;
112
+ /** The indirect reference to this stream in the registry. */
113
+ readonly ref: PdfRef;
114
+ /** Resource name on the page (e.g. '/Im1'). */
115
+ readonly name: string;
116
+ /** Zero-based page index where this image appears. */
117
+ readonly pageIndex: number;
118
+ /** Image width in pixels. */
119
+ readonly width: number;
120
+ /** Image height in pixels. */
121
+ readonly height: number;
122
+ /** Bits per component (typically 8). */
123
+ readonly bitsPerComponent: number;
124
+ /** PDF color space name (e.g. 'DeviceRGB', 'DeviceGray', 'DeviceCMYK'). */
125
+ readonly colorSpace: string;
126
+ /** Number of color channels (1, 3, or 4). */
127
+ readonly channels: number;
128
+ /** PDF filter name(s) applied to this stream. */
129
+ readonly filters: readonly string[];
130
+ /** Size of the compressed stream data in bytes. */
131
+ readonly compressedSize: number;
132
+ }
133
+ /**
134
+ * Extract all image XObjects from a PDF document.
135
+ *
136
+ * Walks every page's `/Resources /XObject` dictionary and collects
137
+ * metadata for each image XObject found.
138
+ *
139
+ * @param doc - A parsed `PdfDocument`.
140
+ * @returns An array of `ImageInfo` objects, one per image XObject.
141
+ *
142
+ * @example
143
+ * ```ts
144
+ * import { loadPdf, extractImages } from 'modern-pdf-lib';
145
+ *
146
+ * const doc = await loadPdf(pdfBytes);
147
+ * const images = extractImages(doc);
148
+ *
149
+ * for (const img of images) {
150
+ * console.log(`${img.name}: ${img.width}x${img.height} ${img.colorSpace} (${img.compressedSize} bytes)`);
151
+ * }
152
+ * ```
153
+ */
154
+ declare function extractImages(doc: PdfDocument): ImageInfo[];
155
+ /**
156
+ * Decode image stream data into raw pixels.
157
+ *
158
+ * For DCTDecode (JPEG) streams, returns the raw JPEG bytes (not decoded
159
+ * to pixels) since JPEG decoding requires the WASM module.
160
+ *
161
+ * For FlateDecode and other filters, fully decodes the stream.
162
+ *
163
+ * @param imageInfo - An `ImageInfo` from `extractImages()`.
164
+ * @returns The decoded stream data.
165
+ */
166
+ declare function decodeImageStream(imageInfo: ImageInfo): Uint8Array;
167
+ //#endregion
168
+ //#region src/assets/image/compressionAnalysis.d.ts
169
+ /**
170
+ * Per-image analysis result.
171
+ */
172
+ interface ImageAnalysis {
173
+ /** Resource name on the page (e.g. '/Im1'). */
174
+ readonly name: string;
175
+ /** Zero-based page index where this image appears. */
176
+ readonly pageIndex: number;
177
+ /** Image width in pixels. */
178
+ readonly width: number;
179
+ /** Image height in pixels. */
180
+ readonly height: number;
181
+ /** Size of the current (compressed) stream data in bytes. */
182
+ readonly currentSize: number;
183
+ /** Description of the current encoding (e.g. 'FlateDecode', 'DCTDecode'). */
184
+ readonly currentFormat: string;
185
+ /** PDF color space name (e.g. 'DeviceRGB', 'DeviceGray'). */
186
+ readonly colorSpace: string;
187
+ /** Estimated JPEG-encoded size in bytes. */
188
+ readonly estimatedJpegSize: number;
189
+ /** Estimated savings in bytes (`currentSize - estimatedJpegSize`). */
190
+ readonly estimatedSavings: number;
191
+ /** Savings as a percentage of the current size. */
192
+ readonly savingsPercent: number;
193
+ /** Whether the image is effectively grayscale (even if stored as RGB). */
194
+ readonly isGrayscale: boolean;
195
+ /** Effective DPI of the image at its display size, or `undefined` if unknown. */
196
+ readonly effectiveDpi: number | undefined;
197
+ /** Recommended action for this image. */
198
+ readonly recommendation: 'recompress' | 'keep' | 'downscale' | 'grayscale';
199
+ }
200
+ /**
201
+ * Full document analysis report.
202
+ */
203
+ interface AnalysisReport {
204
+ /** Per-image analysis results. */
205
+ readonly images: readonly ImageAnalysis[];
206
+ /** Total size of all image streams in bytes. */
207
+ readonly totalCurrentSize: number;
208
+ /** Total estimated size after optimization. */
209
+ readonly totalEstimatedSize: number;
210
+ /** Total estimated savings in bytes. */
211
+ readonly totalSavings: number;
212
+ /** Total savings as a percentage of total current size. */
213
+ readonly totalSavingsPercent: number;
214
+ }
215
+ /**
216
+ * Options for `analyzeImages()`.
217
+ */
218
+ interface AnalyzeImagesOptions {
219
+ /**
220
+ * JPEG quality used for size estimation (1–100).
221
+ *
222
+ * Default: `80`.
223
+ */
224
+ readonly quality?: number;
225
+ /**
226
+ * Maximum allowed DPI. Images exceeding this at their display size
227
+ * receive a `'downscale'` recommendation.
228
+ *
229
+ * Default: `150`.
230
+ */
231
+ readonly maxDpi?: number;
232
+ }
233
+ /**
234
+ * Analyze all images in a PDF and report potential savings without
235
+ * modifying the document.
236
+ *
237
+ * For each image XObject with `bitsPerComponent === 8` and 1–4 channels,
238
+ * the function estimates the JPEG-encoded size — using the WASM encoder
239
+ * when available, or a heuristic fallback otherwise.
240
+ *
241
+ * @param doc - A parsed `PdfDocument`.
242
+ * @param options - Optional quality and maxDpi settings.
243
+ * @returns An `AnalysisReport` with per-image and aggregate statistics.
244
+ *
245
+ * @example
246
+ * ```ts
247
+ * import { loadPdf, analyzeImages } from 'modern-pdf-lib';
248
+ *
249
+ * const doc = await loadPdf(pdfBytes);
250
+ * const report = analyzeImages(doc, { quality: 75, maxDpi: 150 });
251
+ *
252
+ * console.log(`Total savings: ${report.totalSavingsPercent.toFixed(1)}%`);
253
+ * for (const img of report.images) {
254
+ * console.log(` ${img.name}: ${img.recommendation} (${img.savingsPercent.toFixed(1)}%)`);
255
+ * }
256
+ * ```
257
+ */
258
+ declare function analyzeImages(doc: PdfDocument, options?: AnalyzeImagesOptions): AnalysisReport;
259
+ //#endregion
260
+ export { decodeImageStream as a, TextExtractionOptions as c, extractTextWithPositions as d, ContentStreamOperator as f, ImageInfo as i, TextItem as l, parseContentStream as m, ImageAnalysis as n, extractImages as o, Operand as p, analyzeImages as r, decodeStream as s, AnalysisReport as t, extractText as u };
261
+ //# sourceMappingURL=compressionAnalysis-BBv4BkQP.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compressionAnalysis-BBv4BkQP.d.mts","names":[],"sources":["../src/parser/contentStreamParser.ts","../src/parser/textExtractor.ts","../src/parser/streamDecode.ts","../src/assets/image/imageExtract.ts","../src/assets/image/compressionAnalysis.ts"],"mappings":";;;;;AAmEA;;;;;;;;;KAlCY,OAAA,sCAA6C,OAAA,GAAU,OAAA;;;ACHnE;UDQiB,qBAAA;;EAEf,QAAA;ECRA;EDUA,QAAA,EAAU,OAAA;AAAA;ACiCZ;;;;;;AAAA,iBDRgB,kBAAA,CAAmB,IAAA,EAAM,UAAA,GAAa,qBAAA;;;;;;UCrCrC,QAAA;EDqC0D;ECnCzE,IAAA;;EAEA,CAAA;;EAEA,CAAA;EANuB;EAQvB,KAAA;EARuB;EAUvB,MAAA;EANA;EAQA,QAAA;EAJA;EAMA,QAAA;AAAA;;;;UAMe,qBAAA;EAAqB;;;;EAKpC,aAAA;AAAA;;;;;;;;;;;;;;iBAoBc,WAAA,CACd,SAAA,EAAW,qBAAA,IACX,SAAA,GAAY,OAAA,EACZ,OAAA,GAAU,qBAAA;AA6LZ;;;;;;;;;;AAAA,iBAAgB,wBAAA,CACd,SAAA,EAAW,qBAAA,IACX,SAAA,GAAY,OAAA,GACX,QAAA;;;;;;;;;AD3MH;;;;;;;iBE/BgB,YAAA,CACd,IAAA,EAAM,UAAA,EACN,OAAA,qBACA,WAAA,GAAc,OAAA,GAAU,OAAA,YACvB,UAAA;;;;;;UCRc,SAAA;EHUE;EAAA,SGRR,MAAA,EAAQ,SAAA;EHiCH;EAAA,SG/BL,GAAA,EAAK,MAAA;;WAEL,IAAA;EH6B8B;EAAA,SG3B9B,SAAA;EH2B2C;EAAA,SGzB3C,KAAA;EHyBgE;EAAA,SGvBhE,MAAA;;WAEA,gBAAA;EFhBM;EAAA,SEkBN,UAAA;;WAEA,QAAA;EFlBT;EAAA,SEoBS,OAAA;EFhBT;EAAA,SEkBS,cAAA;AAAA;;;;;AFJX;;;;;AAyBA;;;;;;;;;;;;iBE6FgB,aAAA,CAAc,GAAA,EAAK,WAAA,GAAc,SAAA;;;;AFmGjD;;;;;;;;iBEagB,iBAAA,CAAkB,SAAA,EAAW,SAAA,GAAY,UAAA;;;;;;UC/PxC,aAAA;EJiBE;EAAA,SIfR,IAAA;EJwCK;EAAA,SItCL,SAAA;;WAEA,KAAA;EJoC8B;EAAA,SIlC9B,MAAA;EJkC2C;EAAA,SIhC3C,WAAA;EJgCgE;EAAA,SI9BhE,aAAA;;WAEA,UAAA;EHTM;EAAA,SGWN,iBAAA;;WAEA,gBAAA;EHXT;EAAA,SGaS,cAAA;EHTT;EAAA,SGWS,WAAA;EHPT;EAAA,SGSS,YAAA;EHLT;EAAA,SGOS,cAAA;AAAA;AHDX;;;AAAA,UGOiB,cAAA;EHFF;EAAA,SGIJ,MAAA,WAAiB,aAAA;EHgBD;EAAA,SGdhB,gBAAA;EHeE;EAAA,SGbF,kBAAA;EHeC;EAAA,SGbD,YAAA;EHasB;EAAA,SGXtB,mBAAA;AAAA;;;;UAUM,oBAAA;EHCgB;;AA6LjC;;;EA7LiC,SGKtB,OAAA;EH0LG;;;;;;EAAA,SGnLH,MAAA;AAAA;;;;;;;AFtDX;;;;;;;;;;;;;;;;;;;iBEkKgB,aAAA,CACd,GAAA,EAAK,WAAA,EACL,OAAA,GAAU,oBAAA,GACT,cAAA"}