@ironsoftware/ironpdf 2026.5.1 → 2026.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/package.json +2 -2
- package/src/index.d.ts +1 -0
- package/src/index.d.ts.map +1 -1
- package/src/index.js +1 -0
- package/src/index.js.map +1 -1
- package/src/index.ts +1 -0
- package/src/internal/IronPdfEngine.ProtoFiles/iron_pdf_service.proto +5 -0
- package/src/internal/IronPdfEngine.ProtoFiles/pdfium_compress.proto +1 -0
- package/src/internal/IronPdfEngine.ProtoFiles/pdfium_form.proto +32 -0
- package/src/internal/IronPdfEngine.ProtoFiles/qpdf.proto +35 -0
- package/src/internal/access.d.ts.map +1 -1
- package/src/internal/access.js +3 -0
- package/src/internal/access.js.map +1 -1
- package/src/internal/access.ts +4 -0
- package/src/internal/generated_proto/iron_pdf_service.d.ts +14 -0
- package/src/internal/generated_proto/iron_pdf_service.d.ts.map +1 -1
- package/src/internal/generated_proto/iron_pdf_service.ts +14 -0
- package/src/internal/generated_proto/ironpdfengineproto/IronPdfService.d.ts +38 -0
- package/src/internal/generated_proto/ironpdfengineproto/IronPdfService.d.ts.map +1 -1
- package/src/internal/generated_proto/ironpdfengineproto/IronPdfService.ts +46 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumCompressImagesRequestP.d.ts +2 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumCompressImagesRequestP.d.ts.map +1 -1
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumCompressImagesRequestP.ts +2 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumDisableFormFontFallbackRequestP.d.ts +8 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumDisableFormFontFallbackRequestP.d.ts.map +1 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumDisableFormFontFallbackRequestP.js +4 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumDisableFormFontFallbackRequestP.js.map +1 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumDisableFormFontFallbackRequestP.ts +11 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumDisableFormFontFallbackResultP.d.ts +12 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumDisableFormFontFallbackResultP.d.ts.map +1 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumDisableFormFontFallbackResultP.js +4 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumDisableFormFontFallbackResultP.js.map +1 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumDisableFormFontFallbackResultP.ts +15 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumSetFormFontRequestStreamP.d.ts +25 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumSetFormFontRequestStreamP.d.ts.map +1 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumSetFormFontRequestStreamP.js +4 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumSetFormFontRequestStreamP.js.map +1 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumSetFormFontRequestStreamP.ts +28 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumSetFormFontResultP.d.ts +12 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumSetFormFontResultP.d.ts.map +1 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumSetFormFontResultP.js +4 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumSetFormFontResultP.js.map +1 -0
- package/src/internal/generated_proto/ironpdfengineproto/PdfiumSetFormFontResultP.ts +15 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressAndSaveAsAdvancedFromBytesRequestStreamP.d.ts +23 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressAndSaveAsAdvancedFromBytesRequestStreamP.d.ts.map +1 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressAndSaveAsAdvancedFromBytesRequestStreamP.js +4 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressAndSaveAsAdvancedFromBytesRequestStreamP.js.map +1 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressAndSaveAsAdvancedFromBytesRequestStreamP.ts +26 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressAndSaveAsAdvancedRequestP.d.ts +15 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressAndSaveAsAdvancedRequestP.d.ts.map +1 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressAndSaveAsAdvancedRequestP.js +4 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressAndSaveAsAdvancedRequestP.js.map +1 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressAndSaveAsAdvancedRequestP.ts +18 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressionFlagsP.d.ts +30 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressionFlagsP.d.ts.map +1 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressionFlagsP.js +4 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressionFlagsP.js.map +1 -0
- package/src/internal/generated_proto/ironpdfengineproto/QPdfCompressionFlagsP.ts +33 -0
- package/src/internal/grpc_layer/pdfium/compress.d.ts +28 -0
- package/src/internal/grpc_layer/pdfium/compress.d.ts.map +1 -1
- package/src/internal/grpc_layer/pdfium/compress.js +154 -3
- package/src/internal/grpc_layer/pdfium/compress.js.map +1 -1
- package/src/internal/grpc_layer/pdfium/compress.ts +191 -1
- package/src/internal/grpc_layer/pdfium/form.d.ts +15 -0
- package/src/internal/grpc_layer/pdfium/form.d.ts.map +1 -0
- package/src/internal/grpc_layer/pdfium/form.js +99 -0
- package/src/internal/grpc_layer/pdfium/form.js.map +1 -0
- package/src/internal/grpc_layer/pdfium/form.ts +119 -0
- package/src/internal/grpc_layer/pdfium/formFontValidator.d.ts +26 -0
- package/src/internal/grpc_layer/pdfium/formFontValidator.d.ts.map +1 -0
- package/src/internal/grpc_layer/pdfium/formFontValidator.js +81 -0
- package/src/internal/grpc_layer/pdfium/formFontValidator.js.map +1 -0
- package/src/internal/grpc_layer/pdfium/formFontValidator.ts +86 -0
- package/src/public/compression.d.ts +92 -0
- package/src/public/compression.d.ts.map +1 -0
- package/src/public/compression.js +26 -0
- package/src/public/compression.js.map +1 -0
- package/src/public/compression.ts +107 -0
- package/src/public/ironpdfglobalconfig.d.ts +12 -0
- package/src/public/ironpdfglobalconfig.d.ts.map +1 -1
- package/src/public/ironpdfglobalconfig.js +7 -0
- package/src/public/ironpdfglobalconfig.js.map +1 -1
- package/src/public/ironpdfglobalconfig.ts +19 -0
- package/src/public/pdfDocument.d.ts +66 -0
- package/src/public/pdfDocument.d.ts.map +1 -1
- package/src/public/pdfDocument.js +148 -0
- package/src/public/pdfDocument.js.map +1 -1
- package/src/public/pdfDocument.ts +154 -1
|
@@ -1,10 +1,38 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
/// <reference types="node" />
|
|
4
|
+
import { QPdfCompressionFlagsP } from "../../generated_proto/ironpdfengineproto/QPdfCompressionFlagsP";
|
|
4
5
|
import { Readable } from "stream";
|
|
6
|
+
import { Buffer } from "buffer";
|
|
7
|
+
import { AdvancedCompressionOptions } from "../../../public/compression";
|
|
5
8
|
export declare function compressImage(id: string, imageQuality: number, scaleToVisibleSize?: boolean): Promise<void>;
|
|
6
9
|
export declare function compressStructTree(id: string): Promise<void>;
|
|
7
10
|
export declare function compressAndSaveAs(id: string, outputPath: string, imageQuality?: number): Promise<void>;
|
|
8
11
|
export declare function compressInMemory(id: string, imageQuality?: number): Promise<Buffer>;
|
|
12
|
+
/**
|
|
13
|
+
* Map a user-facing {@link AdvancedCompressionOptions} onto the wire
|
|
14
|
+
* {@link QPdfCompressionFlagsP}.
|
|
15
|
+
*/
|
|
16
|
+
export declare function toFlagsProto(options: AdvancedCompressionOptions): QPdfCompressionFlagsP;
|
|
17
|
+
/**
|
|
18
|
+
* Apply the advanced compression pipeline to an open engine document and
|
|
19
|
+
* save the result to disk. Mirrors .NET
|
|
20
|
+
* {@code PdfDocument.CompressAndSaveAs(string, AdvancedCompressionOptions)}.
|
|
21
|
+
*
|
|
22
|
+
* Step 1 (optional) — pdfium image re-encoding when {@code targetImageDpi > 0}
|
|
23
|
+
* and {@code jpegQuality != null}.
|
|
24
|
+
* Step 2 (optional) — struct-tree removal when {@code removeStructureTree}.
|
|
25
|
+
* Step 3 — qpdf {@code QPdf_Compression_CompressAndSaveAsAdvanced}.
|
|
26
|
+
*/
|
|
27
|
+
export declare function compressAndSaveAsAdvanced(id: string, outputPath: string, options: AdvancedCompressionOptions, password?: string): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Apply the advanced compression pipeline to byte[] input and stream the
|
|
30
|
+
* result back. Mirrors .NET
|
|
31
|
+
* {@code PdfDocument.CompressAndSaveAs(byte[], string, AdvancedCompressionOptions, ...)}.
|
|
32
|
+
*
|
|
33
|
+
* Returns the compressed bytes; when {@code outputPath} is non-empty also
|
|
34
|
+
* writes them to disk.
|
|
35
|
+
*/
|
|
36
|
+
export declare function compressAndSaveAsAdvancedFromBytes(pdfBytes: Buffer, outputPath: string, options: AdvancedCompressionOptions, password?: string): Promise<Buffer>;
|
|
9
37
|
export declare function compressInMemoryStream(id: string, imageQuality?: number): Promise<Readable>;
|
|
10
38
|
//# sourceMappingURL=compress.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compress.d.ts","sourceRoot":"","sources":["compress.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"compress.d.ts","sourceRoot":"","sources":["compress.ts"],"names":[],"mappings":";;;AAMA,OAAO,EAAC,qBAAqB,EAAC,MAAM,gEAAgE,CAAC;AAKrG,OAAO,EAAc,QAAQ,EAAC,MAAM,QAAQ,CAAC;AAE7C,OAAO,EAAC,MAAM,EAAC,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAC,0BAA0B,EAAmB,MAAM,6BAA6B,CAAC;AAEzF,wBAAsB,aAAa,CAClC,EAAE,EAAE,MAAM,EACV,YAAY,EAAE,MAAM,EACpB,kBAAkB,UAAQ,GACxB,OAAO,CAAC,IAAI,CAAC,CAwBf;AAED,wBAAsB,kBAAkB,CACvC,EAAE,EAAE,MAAM,GACR,OAAO,CAAC,IAAI,CAAC,CAsBf;AAED,wBAAsB,iBAAiB,CACtC,EAAE,EAAE,MAAM,EACV,UAAU,EAAE,MAAM,EAClB,YAAY,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC,IAAI,CAAC,CAmCf;AAED,wBAAsB,gBAAgB,CACrC,EAAE,EAAE,MAAM,EACV,YAAY,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC,MAAM,CAAC,CAkCjB;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,0BAA0B,GAAG,qBAAqB,CA0BvF;AAED;;;;;;;;;GASG;AACH,wBAAsB,yBAAyB,CAC9C,EAAE,EAAE,MAAM,EACV,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,0BAA0B,EACnC,QAAQ,SAAK,GACX,OAAO,CAAC,IAAI,CAAC,CA8Df;AAED;;;;;;;GAOG;AACH,wBAAsB,kCAAkC,CACvD,QAAQ,EAAE,MAAM,EAChB,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,0BAA0B,EACnC,QAAQ,SAAK,GACX,OAAO,CAAC,MAAM,CAAC,CA0DjB;AAED,wBAAsB,sBAAsB,CAC3C,EAAE,EAAE,MAAM,EACV,YAAY,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC,QAAQ,CAAC,CAiCnB"}
|
|
@@ -12,11 +12,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.compressInMemoryStream = exports.compressInMemory = exports.compressAndSaveAs = exports.compressStructTree = exports.compressImage = void 0;
|
|
15
|
+
exports.compressInMemoryStream = exports.compressAndSaveAsAdvancedFromBytes = exports.compressAndSaveAsAdvanced = exports.toFlagsProto = exports.compressInMemory = exports.compressAndSaveAs = exports.compressStructTree = exports.compressImage = void 0;
|
|
16
16
|
const access_1 = require("../../access");
|
|
17
17
|
const util_1 = require("../util");
|
|
18
18
|
const stream_1 = require("stream");
|
|
19
19
|
const fs_1 = __importDefault(require("fs"));
|
|
20
|
+
const buffer_1 = require("buffer");
|
|
21
|
+
const compression_1 = require("../../../public/compression");
|
|
20
22
|
function compressImage(id, imageQuality, scaleToVisibleSize = false) {
|
|
21
23
|
return __awaiter(this, void 0, void 0, function* () {
|
|
22
24
|
const client = yield access_1.Access.ensureConnection();
|
|
@@ -76,7 +78,7 @@ function compressAndSaveAs(id, outputPath, imageQuality) {
|
|
|
76
78
|
reject(`${err.name}/n${err.message}`);
|
|
77
79
|
});
|
|
78
80
|
stream.on("end", () => {
|
|
79
|
-
fs_1.default.writeFileSync(outputPath, Buffer.concat(buffers));
|
|
81
|
+
fs_1.default.writeFileSync(outputPath, buffer_1.Buffer.concat(buffers));
|
|
80
82
|
resolve();
|
|
81
83
|
});
|
|
82
84
|
});
|
|
@@ -102,12 +104,161 @@ function compressInMemory(id, imageQuality) {
|
|
|
102
104
|
reject(`${err.name}/n${err.message}`);
|
|
103
105
|
});
|
|
104
106
|
stream.on("end", () => {
|
|
105
|
-
resolve(Buffer.concat(buffers));
|
|
107
|
+
resolve(buffer_1.Buffer.concat(buffers));
|
|
106
108
|
});
|
|
107
109
|
});
|
|
108
110
|
});
|
|
109
111
|
}
|
|
110
112
|
exports.compressInMemory = compressInMemory;
|
|
113
|
+
/**
|
|
114
|
+
* Map a user-facing {@link AdvancedCompressionOptions} onto the wire
|
|
115
|
+
* {@link QPdfCompressionFlagsP}.
|
|
116
|
+
*/
|
|
117
|
+
function toFlagsProto(options) {
|
|
118
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
119
|
+
const pdfiumWillReEncode = options.targetImageDpi !== null &&
|
|
120
|
+
options.targetImageDpi !== undefined &&
|
|
121
|
+
options.targetImageDpi > 0;
|
|
122
|
+
const optimizeImages = !pdfiumWillReEncode && options.jpegQuality != null;
|
|
123
|
+
const flags = {
|
|
124
|
+
compressStreams: (_a = options.compressStreams) !== null && _a !== void 0 ? _a : true,
|
|
125
|
+
coalesceContents: (_b = options.coalesceContents) !== null && _b !== void 0 ? _b : true,
|
|
126
|
+
removeUnreferencedResources: (_c = options.removeUnreferencedResources) !== null && _c !== void 0 ? _c : true,
|
|
127
|
+
recompressFlate: (_d = options.recompressFlate) !== null && _d !== void 0 ? _d : true,
|
|
128
|
+
compressionLevel: (_e = options.compressionLevel) !== null && _e !== void 0 ? _e : 9,
|
|
129
|
+
objectStreams: ((_f = options.objectStreams) !== null && _f !== void 0 ? _f : compression_1.ObjectStreamMode.Generate),
|
|
130
|
+
decodeGeneralizedStreams: (_g = options.decodeGeneralizedStreams) !== null && _g !== void 0 ? _g : true,
|
|
131
|
+
optimizeImagesMinWidth: (_h = options.optimizeImagesMinWidth) !== null && _h !== void 0 ? _h : 0,
|
|
132
|
+
optimizeImagesMinHeight: (_j = options.optimizeImagesMinHeight) !== null && _j !== void 0 ? _j : 0,
|
|
133
|
+
optimizeImagesMinArea: (_k = options.optimizeImagesMinArea) !== null && _k !== void 0 ? _k : 0,
|
|
134
|
+
optimizeImages: optimizeImages,
|
|
135
|
+
};
|
|
136
|
+
if (!pdfiumWillReEncode && options.jpegQuality != null) {
|
|
137
|
+
flags.jpegQuality = options.jpegQuality;
|
|
138
|
+
}
|
|
139
|
+
return flags;
|
|
140
|
+
}
|
|
141
|
+
exports.toFlagsProto = toFlagsProto;
|
|
142
|
+
/**
|
|
143
|
+
* Apply the advanced compression pipeline to an open engine document and
|
|
144
|
+
* save the result to disk. Mirrors .NET
|
|
145
|
+
* {@code PdfDocument.CompressAndSaveAs(string, AdvancedCompressionOptions)}.
|
|
146
|
+
*
|
|
147
|
+
* Step 1 (optional) — pdfium image re-encoding when {@code targetImageDpi > 0}
|
|
148
|
+
* and {@code jpegQuality != null}.
|
|
149
|
+
* Step 2 (optional) — struct-tree removal when {@code removeStructureTree}.
|
|
150
|
+
* Step 3 — qpdf {@code QPdf_Compression_CompressAndSaveAsAdvanced}.
|
|
151
|
+
*/
|
|
152
|
+
function compressAndSaveAsAdvanced(id, outputPath, options, password = "") {
|
|
153
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
154
|
+
if (!options) {
|
|
155
|
+
throw new Error("options must not be null");
|
|
156
|
+
}
|
|
157
|
+
// Step 1 — pdfium image re-encoding (gated, matches .NET pdfiumWillReEncode).
|
|
158
|
+
const pdfiumWillReEncode = options.targetImageDpi !== null &&
|
|
159
|
+
options.targetImageDpi !== undefined &&
|
|
160
|
+
options.targetImageDpi > 0;
|
|
161
|
+
if (pdfiumWillReEncode && options.jpegQuality != null) {
|
|
162
|
+
yield compressImage(id, options.jpegQuality, false /* scaleToVisibleSize */);
|
|
163
|
+
}
|
|
164
|
+
// Step 2 — struct-tree removal.
|
|
165
|
+
if (options.removeStructureTree) {
|
|
166
|
+
yield compressStructTree(id);
|
|
167
|
+
}
|
|
168
|
+
// Step 3 — qpdf advanced pass.
|
|
169
|
+
const client = yield access_1.Access.ensureConnection();
|
|
170
|
+
return new Promise((resolve, reject) => {
|
|
171
|
+
const stream = client.QPdf_Compression_CompressAndSaveAsAdvanced({
|
|
172
|
+
document: { documentId: id },
|
|
173
|
+
outputPath: "",
|
|
174
|
+
password: password !== null && password !== void 0 ? password : "",
|
|
175
|
+
flags: toFlagsProto(options),
|
|
176
|
+
});
|
|
177
|
+
const buffers = [];
|
|
178
|
+
stream.on("data", (data) => {
|
|
179
|
+
if (data.exception) {
|
|
180
|
+
(0, util_1.handleRemoteException)(data.exception, reject);
|
|
181
|
+
}
|
|
182
|
+
else if (data.resultChunk) {
|
|
183
|
+
buffers.push(data.resultChunk);
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
stream.on("error", (err) => {
|
|
187
|
+
reject(`${err.name}/n${err.message}`);
|
|
188
|
+
});
|
|
189
|
+
stream.on("end", () => {
|
|
190
|
+
try {
|
|
191
|
+
fs_1.default.writeFileSync(outputPath, buffer_1.Buffer.concat(buffers));
|
|
192
|
+
}
|
|
193
|
+
catch (e) {
|
|
194
|
+
reject(`Failed to write compressed PDF to ${outputPath}: ${e.message}`);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
resolve();
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
exports.compressAndSaveAsAdvanced = compressAndSaveAsAdvanced;
|
|
203
|
+
/**
|
|
204
|
+
* Apply the advanced compression pipeline to byte[] input and stream the
|
|
205
|
+
* result back. Mirrors .NET
|
|
206
|
+
* {@code PdfDocument.CompressAndSaveAs(byte[], string, AdvancedCompressionOptions, ...)}.
|
|
207
|
+
*
|
|
208
|
+
* Returns the compressed bytes; when {@code outputPath} is non-empty also
|
|
209
|
+
* writes them to disk.
|
|
210
|
+
*/
|
|
211
|
+
function compressAndSaveAsAdvancedFromBytes(pdfBytes, outputPath, options, password = "") {
|
|
212
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
213
|
+
if (!pdfBytes || pdfBytes.length === 0) {
|
|
214
|
+
throw new Error("pdfBytes must not be null or empty");
|
|
215
|
+
}
|
|
216
|
+
if (!options) {
|
|
217
|
+
throw new Error("options must not be null");
|
|
218
|
+
}
|
|
219
|
+
const client = yield access_1.Access.ensureConnection();
|
|
220
|
+
return new Promise((resolve, reject) => {
|
|
221
|
+
const stream = client.QPdf_Compression_CompressAndSaveAsAdvancedFromBytes();
|
|
222
|
+
const buffers = [];
|
|
223
|
+
stream.on("data", (data) => {
|
|
224
|
+
if (data.exception) {
|
|
225
|
+
(0, util_1.handleRemoteException)(data.exception, reject);
|
|
226
|
+
}
|
|
227
|
+
else if (data.resultChunk) {
|
|
228
|
+
buffers.push(data.resultChunk);
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
stream.on("error", (err) => {
|
|
232
|
+
reject(`${err.name}/n${err.message}`);
|
|
233
|
+
});
|
|
234
|
+
stream.on("end", () => {
|
|
235
|
+
const result = buffer_1.Buffer.concat(buffers);
|
|
236
|
+
if (outputPath) {
|
|
237
|
+
try {
|
|
238
|
+
fs_1.default.writeFileSync(outputPath, result);
|
|
239
|
+
}
|
|
240
|
+
catch (e) {
|
|
241
|
+
reject(`Failed to write compressed PDF to ${outputPath}: ${e.message}`);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
resolve(result);
|
|
246
|
+
});
|
|
247
|
+
stream.write({
|
|
248
|
+
info: {
|
|
249
|
+
outputPath: outputPath !== null && outputPath !== void 0 ? outputPath : "",
|
|
250
|
+
password: password !== null && password !== void 0 ? password : "",
|
|
251
|
+
flags: toFlagsProto(options),
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
for (const chunk of (0, util_1.chunkBuffer)(pdfBytes)) {
|
|
255
|
+
stream.write({ pdfBytesChunk: chunk });
|
|
256
|
+
}
|
|
257
|
+
stream.end();
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
exports.compressAndSaveAsAdvancedFromBytes = compressAndSaveAsAdvancedFromBytes;
|
|
111
262
|
function compressInMemoryStream(id, imageQuality) {
|
|
112
263
|
return __awaiter(this, void 0, void 0, function* () {
|
|
113
264
|
const client = yield access_1.Access.ensureConnection();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compress.js","sourceRoot":"","sources":["compress.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAGA,yCAAoC;
|
|
1
|
+
{"version":3,"file":"compress.js","sourceRoot":"","sources":["compress.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAGA,yCAAoC;AAOpC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GAAG,CAAC,CAAC;QAC1B,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE;YACrB,WAAW,CAAC,GAAG,EAAE,CAAC;QACnB,CAAC,CAAC,CAAC;QAEH,OAAO,WAAW,CAAC;IACpB,CAAC;CAAA;AApCD,wDAoCC"}
|
|
@@ -4,9 +4,15 @@ import {IronPdfServiceClient} from "../../generated_proto/ironpdfengineproto/Iro
|
|
|
4
4
|
import {Access} from "../../access";
|
|
5
5
|
import {EmptyResultP__Output} from "../../generated_proto/ironpdfengineproto/EmptyResultP";
|
|
6
6
|
import {BytesResultStreamP__Output} from "../../generated_proto/ironpdfengineproto/BytesResultStreamP";
|
|
7
|
-
import {
|
|
7
|
+
import {QPdfCompressionFlagsP} from "../../generated_proto/ironpdfengineproto/QPdfCompressionFlagsP";
|
|
8
|
+
import {
|
|
9
|
+
QPdfCompressAndSaveAsAdvancedFromBytesRequestStreamP
|
|
10
|
+
} from "../../generated_proto/ironpdfengineproto/QPdfCompressAndSaveAsAdvancedFromBytesRequestStreamP";
|
|
11
|
+
import {chunkBuffer, handleEmptyResultP__Output, handleRemoteException} from "../util";
|
|
8
12
|
import {PassThrough, Readable} from "stream";
|
|
9
13
|
import fs from "fs";
|
|
14
|
+
import {Buffer} from "buffer";
|
|
15
|
+
import {AdvancedCompressionOptions, ObjectStreamMode} from "../../../public/compression";
|
|
10
16
|
|
|
11
17
|
export async function compressImage(
|
|
12
18
|
id: string,
|
|
@@ -144,6 +150,190 @@ export async function compressInMemory(
|
|
|
144
150
|
);
|
|
145
151
|
}
|
|
146
152
|
|
|
153
|
+
/**
|
|
154
|
+
* Map a user-facing {@link AdvancedCompressionOptions} onto the wire
|
|
155
|
+
* {@link QPdfCompressionFlagsP}.
|
|
156
|
+
*/
|
|
157
|
+
export function toFlagsProto(options: AdvancedCompressionOptions): QPdfCompressionFlagsP {
|
|
158
|
+
const pdfiumWillReEncode =
|
|
159
|
+
options.targetImageDpi !== null &&
|
|
160
|
+
options.targetImageDpi !== undefined &&
|
|
161
|
+
options.targetImageDpi > 0;
|
|
162
|
+
const optimizeImages = !pdfiumWillReEncode && options.jpegQuality != null;
|
|
163
|
+
|
|
164
|
+
const flags: QPdfCompressionFlagsP = {
|
|
165
|
+
compressStreams: options.compressStreams ?? true,
|
|
166
|
+
coalesceContents: options.coalesceContents ?? true,
|
|
167
|
+
removeUnreferencedResources: options.removeUnreferencedResources ?? true,
|
|
168
|
+
recompressFlate: options.recompressFlate ?? true,
|
|
169
|
+
compressionLevel: options.compressionLevel ?? 9,
|
|
170
|
+
objectStreams: (options.objectStreams ?? ObjectStreamMode.Generate) as string,
|
|
171
|
+
decodeGeneralizedStreams: options.decodeGeneralizedStreams ?? true,
|
|
172
|
+
optimizeImagesMinWidth: options.optimizeImagesMinWidth ?? 0,
|
|
173
|
+
optimizeImagesMinHeight: options.optimizeImagesMinHeight ?? 0,
|
|
174
|
+
optimizeImagesMinArea: options.optimizeImagesMinArea ?? 0,
|
|
175
|
+
optimizeImages: optimizeImages,
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
if (!pdfiumWillReEncode && options.jpegQuality != null) {
|
|
179
|
+
flags.jpegQuality = options.jpegQuality;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return flags;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Apply the advanced compression pipeline to an open engine document and
|
|
187
|
+
* save the result to disk. Mirrors .NET
|
|
188
|
+
* {@code PdfDocument.CompressAndSaveAs(string, AdvancedCompressionOptions)}.
|
|
189
|
+
*
|
|
190
|
+
* Step 1 (optional) — pdfium image re-encoding when {@code targetImageDpi > 0}
|
|
191
|
+
* and {@code jpegQuality != null}.
|
|
192
|
+
* Step 2 (optional) — struct-tree removal when {@code removeStructureTree}.
|
|
193
|
+
* Step 3 — qpdf {@code QPdf_Compression_CompressAndSaveAsAdvanced}.
|
|
194
|
+
*/
|
|
195
|
+
export async function compressAndSaveAsAdvanced(
|
|
196
|
+
id: string,
|
|
197
|
+
outputPath: string,
|
|
198
|
+
options: AdvancedCompressionOptions,
|
|
199
|
+
password = ""
|
|
200
|
+
): Promise<void> {
|
|
201
|
+
if (!options) {
|
|
202
|
+
throw new Error("options must not be null");
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Step 1 — pdfium image re-encoding (gated, matches .NET pdfiumWillReEncode).
|
|
206
|
+
const pdfiumWillReEncode =
|
|
207
|
+
options.targetImageDpi !== null &&
|
|
208
|
+
options.targetImageDpi !== undefined &&
|
|
209
|
+
options.targetImageDpi > 0;
|
|
210
|
+
if (pdfiumWillReEncode && options.jpegQuality != null) {
|
|
211
|
+
await compressImage(
|
|
212
|
+
id,
|
|
213
|
+
options.jpegQuality,
|
|
214
|
+
false /* scaleToVisibleSize */
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Step 2 — struct-tree removal.
|
|
219
|
+
if (options.removeStructureTree) {
|
|
220
|
+
await compressStructTree(id);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Step 3 — qpdf advanced pass.
|
|
224
|
+
const client: IronPdfServiceClient = await Access.ensureConnection();
|
|
225
|
+
|
|
226
|
+
return new Promise(
|
|
227
|
+
(resolve: () => void, reject: (errorMsg: string) => void) => {
|
|
228
|
+
const stream: grpc.ClientReadableStream<BytesResultStreamP__Output> =
|
|
229
|
+
client.QPdf_Compression_CompressAndSaveAsAdvanced({
|
|
230
|
+
document: {documentId: id},
|
|
231
|
+
outputPath: "",
|
|
232
|
+
password: password ?? "",
|
|
233
|
+
flags: toFlagsProto(options),
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
const buffers: Buffer[] = [];
|
|
237
|
+
stream.on("data", (data: BytesResultStreamP__Output) => {
|
|
238
|
+
if (data.exception) {
|
|
239
|
+
handleRemoteException(data.exception, reject);
|
|
240
|
+
} else if (data.resultChunk) {
|
|
241
|
+
buffers.push(data.resultChunk);
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
stream.on("error", (err: Error) => {
|
|
246
|
+
reject(`${err.name}/n${err.message}`);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
stream.on("end", () => {
|
|
250
|
+
try {
|
|
251
|
+
fs.writeFileSync(outputPath, Buffer.concat(buffers));
|
|
252
|
+
} catch (e) {
|
|
253
|
+
reject(
|
|
254
|
+
`Failed to write compressed PDF to ${outputPath}: ${(e as Error).message}`
|
|
255
|
+
);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
resolve();
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Apply the advanced compression pipeline to byte[] input and stream the
|
|
266
|
+
* result back. Mirrors .NET
|
|
267
|
+
* {@code PdfDocument.CompressAndSaveAs(byte[], string, AdvancedCompressionOptions, ...)}.
|
|
268
|
+
*
|
|
269
|
+
* Returns the compressed bytes; when {@code outputPath} is non-empty also
|
|
270
|
+
* writes them to disk.
|
|
271
|
+
*/
|
|
272
|
+
export async function compressAndSaveAsAdvancedFromBytes(
|
|
273
|
+
pdfBytes: Buffer,
|
|
274
|
+
outputPath: string,
|
|
275
|
+
options: AdvancedCompressionOptions,
|
|
276
|
+
password = ""
|
|
277
|
+
): Promise<Buffer> {
|
|
278
|
+
if (!pdfBytes || pdfBytes.length === 0) {
|
|
279
|
+
throw new Error("pdfBytes must not be null or empty");
|
|
280
|
+
}
|
|
281
|
+
if (!options) {
|
|
282
|
+
throw new Error("options must not be null");
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const client: IronPdfServiceClient = await Access.ensureConnection();
|
|
286
|
+
|
|
287
|
+
return new Promise(
|
|
288
|
+
(resolve: (_: Buffer) => void, reject: (errorMsg: string) => void) => {
|
|
289
|
+
const stream: grpc.ClientDuplexStream<
|
|
290
|
+
QPdfCompressAndSaveAsAdvancedFromBytesRequestStreamP,
|
|
291
|
+
BytesResultStreamP__Output
|
|
292
|
+
> = client.QPdf_Compression_CompressAndSaveAsAdvancedFromBytes();
|
|
293
|
+
|
|
294
|
+
const buffers: Buffer[] = [];
|
|
295
|
+
stream.on("data", (data: BytesResultStreamP__Output) => {
|
|
296
|
+
if (data.exception) {
|
|
297
|
+
handleRemoteException(data.exception, reject);
|
|
298
|
+
} else if (data.resultChunk) {
|
|
299
|
+
buffers.push(data.resultChunk);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
stream.on("error", (err: Error) => {
|
|
304
|
+
reject(`${err.name}/n${err.message}`);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
stream.on("end", () => {
|
|
308
|
+
const result = Buffer.concat(buffers);
|
|
309
|
+
if (outputPath) {
|
|
310
|
+
try {
|
|
311
|
+
fs.writeFileSync(outputPath, result);
|
|
312
|
+
} catch (e) {
|
|
313
|
+
reject(
|
|
314
|
+
`Failed to write compressed PDF to ${outputPath}: ${(e as Error).message}`
|
|
315
|
+
);
|
|
316
|
+
return;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
resolve(result);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
stream.write({
|
|
323
|
+
info: {
|
|
324
|
+
outputPath: outputPath ?? "",
|
|
325
|
+
password: password ?? "",
|
|
326
|
+
flags: toFlagsProto(options),
|
|
327
|
+
},
|
|
328
|
+
});
|
|
329
|
+
for (const chunk of chunkBuffer(pdfBytes)) {
|
|
330
|
+
stream.write({pdfBytesChunk: chunk});
|
|
331
|
+
}
|
|
332
|
+
stream.end();
|
|
333
|
+
}
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
|
|
147
337
|
export async function compressInMemoryStream(
|
|
148
338
|
id: string,
|
|
149
339
|
imageQuality?: number
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { Buffer } from "buffer";
|
|
4
|
+
/**
|
|
5
|
+
* Stream a font (or just a name reference) to the engine so subsequent
|
|
6
|
+
* form-field fills with non-ASCII values use the supplied font instead of
|
|
7
|
+
* triggering the automatic Tahoma/Arial fallback embed.
|
|
8
|
+
*/
|
|
9
|
+
export declare function setFormFont(id: string, fontName: string, fontData: Buffer | undefined, forceEmbed: boolean): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Suppress the engine's automatic Tahoma/Arial fallback embed for non-ASCII
|
|
12
|
+
* form-field values without registering a replacement font.
|
|
13
|
+
*/
|
|
14
|
+
export declare function disableFormFontFallback(id: string): Promise<void>;
|
|
15
|
+
//# sourceMappingURL=form.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"form.d.ts","sourceRoot":"","sources":["form.ts"],"names":[],"mappings":";;AACA,OAAO,EAAC,MAAM,EAAC,MAAM,QAAQ,CAAC;AAe9B;;;;GAIG;AACH,wBAAsB,WAAW,CAChC,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,UAAU,EAAE,OAAO,GACjB,OAAO,CAAC,IAAI,CAAC,CA0Df;AAED;;;GAGG;AACH,wBAAsB,uBAAuB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CA4BvE"}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.disableFormFontFallback = exports.setFormFont = void 0;
|
|
13
|
+
const access_1 = require("../../access");
|
|
14
|
+
const util_1 = require("../util");
|
|
15
|
+
const formFontValidator_1 = require("./formFontValidator");
|
|
16
|
+
/**
|
|
17
|
+
* Stream a font (or just a name reference) to the engine so subsequent
|
|
18
|
+
* form-field fills with non-ASCII values use the supplied font instead of
|
|
19
|
+
* triggering the automatic Tahoma/Arial fallback embed.
|
|
20
|
+
*/
|
|
21
|
+
function setFormFont(id, fontName, fontData, forceEmbed) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
(0, formFontValidator_1.validateFormFontName)(fontName);
|
|
24
|
+
// Distinguish between "name-only mode" (caller passed undefined/null) and
|
|
25
|
+
// "caller passed bytes". An explicit Buffer — even an empty one — runs the
|
|
26
|
+
// data validator, which rejects empty/oversize/non-sfnt payloads. Mirrors
|
|
27
|
+
// .NET FormFontValidator behaviour where an empty byte[] is a caller error.
|
|
28
|
+
if (fontData !== undefined && fontData !== null) {
|
|
29
|
+
(0, formFontValidator_1.validateFormFontData)(fontData);
|
|
30
|
+
}
|
|
31
|
+
const client = yield access_1.Access.ensureConnection();
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
let resolved = false;
|
|
34
|
+
const stream = client.Pdfium_Form_SetFormFont((err, value) => {
|
|
35
|
+
if (resolved)
|
|
36
|
+
return;
|
|
37
|
+
resolved = true;
|
|
38
|
+
if (err) {
|
|
39
|
+
reject(`${err.name}/n${err.message}`);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
if (!value) {
|
|
43
|
+
reject("No response from IronPdfEngine for setFormFont");
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
if (value.exception) {
|
|
47
|
+
(0, util_1.handleRemoteException)(value.exception, reject);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
resolve();
|
|
51
|
+
});
|
|
52
|
+
// Send Info first.
|
|
53
|
+
stream.write({
|
|
54
|
+
info: {
|
|
55
|
+
document: { documentId: id },
|
|
56
|
+
fontName: fontName,
|
|
57
|
+
fontBytesSize: fontData ? fontData.length : 0,
|
|
58
|
+
forceEmbed: forceEmbed,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
// Stream bytes (if any).
|
|
62
|
+
if (fontData && fontData.length > 0) {
|
|
63
|
+
for (const chunk of (0, util_1.chunkBuffer)(fontData)) {
|
|
64
|
+
stream.write({ fontBytesChunk: chunk });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
stream.end();
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
exports.setFormFont = setFormFont;
|
|
72
|
+
/**
|
|
73
|
+
* Suppress the engine's automatic Tahoma/Arial fallback embed for non-ASCII
|
|
74
|
+
* form-field values without registering a replacement font.
|
|
75
|
+
*/
|
|
76
|
+
function disableFormFontFallback(id) {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
const client = yield access_1.Access.ensureConnection();
|
|
79
|
+
return new Promise((resolve, reject) => {
|
|
80
|
+
client.Pdfium_Form_DisableFormFontFallback({ document: { documentId: id } }, (err, value) => {
|
|
81
|
+
if (err) {
|
|
82
|
+
reject(`${err.name}/n${err.message}`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
if (!value) {
|
|
86
|
+
reject("No response from IronPdfEngine for disableFormFontFallback");
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (value.exception) {
|
|
90
|
+
(0, util_1.handleRemoteException)(value.exception, reject);
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
resolve();
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
exports.disableFormFontFallback = disableFormFontFallback;
|
|
99
|
+
//# sourceMappingURL=form.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"form.js","sourceRoot":"","sources":["form.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,yCAAoC;AAWpC,kCAA2D;AAC3D,2DAA+E;AAE/E;;;;GAIG;AACH,SAAsB,WAAW,CAChC,EAAU,EACV,QAAgB,EAChB,QAA4B,EAC5B,UAAmB;;QAEnB,IAAA,wCAAoB,EAAC,QAAQ,CAAC,CAAC;QAC/B,0EAA0E;QAC1E,2EAA2E;QAC3E,0EAA0E;QAC1E,4EAA4E;QAC5E,IAAI,QAAQ,KAAK,SAAS,IAAI,QAAQ,KAAK,IAAI,EAAE;YAChD,IAAA,wCAAoB,EAAC,QAAQ,CAAC,CAAC;SAC/B;QAED,MAAM,MAAM,GAAyB,MAAM,eAAM,CAAC,gBAAgB,EAAE,CAAC;QAErE,OAAO,IAAI,OAAO,CACjB,CAAC,OAAmB,EAAE,MAAkC,EAAE,EAAE;YAC3D,IAAI,QAAQ,GAAG,KAAK,CAAC;YACrB,MAAM,MAAM,GACX,MAAM,CAAC,uBAAuB,CAC7B,CACC,GAA6B,EAC7B,KAAmD,EAClD,EAAE;gBACH,IAAI,QAAQ;oBAAE,OAAO;gBACrB,QAAQ,GAAG,IAAI,CAAC;gBAChB,IAAI,GAAG,EAAE;oBACR,MAAM,CAAC,GAAG,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;oBACtC,OAAO;iBACP;gBACD,IAAI,CAAC,KAAK,EAAE;oBACX,MAAM,CAAC,gDAAgD,CAAC,CAAC;oBACzD,OAAO;iBACP;gBACD,IAAI,KAAK,CAAC,SAAS,EAAE;oBACpB,IAAA,4BAAqB,EAAC,KAAK,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;oBAC/C,OAAO;iBACP;gBACD,OAAO,EAAE,CAAC;YACX,CAAC,CACD,CAAC;YAEH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