@ironsoftware/ironpdf 2026.6.1 → 2026.7.2
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 +8 -5
- package/src/internal/grpc_layer/pdfium/compress.d.ts +15 -3
- package/src/internal/grpc_layer/pdfium/compress.d.ts.map +1 -1
- package/src/internal/grpc_layer/pdfium/compress.js +87 -27
- package/src/internal/grpc_layer/pdfium/compress.js.map +1 -1
- package/src/internal/grpc_layer/pdfium/compress.ts +100 -28
- package/src/internal/grpc_layer/pdfium/io.d.ts +2 -1
- package/src/internal/grpc_layer/pdfium/io.d.ts.map +1 -1
- package/src/internal/grpc_layer/pdfium/io.js +41 -36
- package/src/internal/grpc_layer/pdfium/io.js.map +1 -1
- package/src/internal/grpc_layer/pdfium/io.ts +43 -36
- package/src/internal/grpc_layer/pdfium/signing.d.ts +29 -2
- package/src/internal/grpc_layer/pdfium/signing.d.ts.map +1 -1
- package/src/internal/grpc_layer/pdfium/signing.js +28 -10
- package/src/internal/grpc_layer/pdfium/signing.js.map +1 -1
- package/src/internal/grpc_layer/pdfium/signing.ts +51 -11
- package/src/internal/image_codec.d.ts +20 -0
- package/src/internal/image_codec.d.ts.map +1 -0
- package/src/internal/image_codec.js +127 -0
- package/src/internal/image_codec.js.map +1 -0
- package/src/internal/image_codec.ts +108 -0
- package/src/public/pdfDocument.d.ts +15 -0
- package/src/public/pdfDocument.d.ts.map +1 -1
- package/src/public/pdfDocument.js +64 -24
- package/src/public/pdfDocument.js.map +1 -1
- package/src/public/pdfDocument.ts +74 -27
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ironsoftware/ironpdf",
|
|
3
|
-
"version": "2026.
|
|
4
|
-
"ironPdfEngineVersion": "2026.
|
|
3
|
+
"version": "2026.7.2",
|
|
4
|
+
"ironPdfEngineVersion": "2026.7.2",
|
|
5
5
|
"homepage": "https://ironpdf.com/nodejs",
|
|
6
6
|
"description": "IronPDF for Node",
|
|
7
7
|
"main": "./src/index.js",
|
|
@@ -23,8 +23,10 @@
|
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@grpc/proto-loader": "^0.8.0",
|
|
26
|
+
"@types/bmp-js": "^0.1.2",
|
|
26
27
|
"@types/jest": "^29.5.2",
|
|
27
28
|
"@types/node": "^20.17.50",
|
|
29
|
+
"@types/pngjs": "^6.0.5",
|
|
28
30
|
"@typescript-eslint/eslint-plugin": "^5.60.0",
|
|
29
31
|
"@typescript-eslint/parser": "^5.60.0",
|
|
30
32
|
"eslint": "^8.43.0",
|
|
@@ -37,10 +39,11 @@
|
|
|
37
39
|
},
|
|
38
40
|
"dependencies": {
|
|
39
41
|
"@grpc/grpc-js": "^1.8.14",
|
|
40
|
-
"@jimp/types": "^0.22.8",
|
|
41
42
|
"@types/unzipper": "^0.10.5",
|
|
43
|
+
"bmp-js": "^0.1.0",
|
|
42
44
|
"glob": "^10.3.3",
|
|
43
|
-
"
|
|
45
|
+
"jpeg-js": "^0.4.4",
|
|
46
|
+
"pngjs": "^6.0.0",
|
|
44
47
|
"unzipper": "^0.10.11",
|
|
45
48
|
"zod": "^3.24.1"
|
|
46
49
|
},
|
|
@@ -48,7 +51,7 @@
|
|
|
48
51
|
"long": "5.2.1"
|
|
49
52
|
},
|
|
50
53
|
"engines": {
|
|
51
|
-
"node": ">=12"
|
|
54
|
+
"node": ">=12.13"
|
|
52
55
|
},
|
|
53
56
|
"files": [
|
|
54
57
|
"src/"
|
|
@@ -5,7 +5,7 @@ import { QPdfCompressionFlagsP } from "../../generated_proto/ironpdfengineproto/
|
|
|
5
5
|
import { Readable } from "stream";
|
|
6
6
|
import { Buffer } from "buffer";
|
|
7
7
|
import { AdvancedCompressionOptions } from "../../../public/compression";
|
|
8
|
-
export declare function compressImage(id: string, imageQuality: number, scaleToVisibleSize?: boolean): Promise<void>;
|
|
8
|
+
export declare function compressImage(id: string, imageQuality: number, scaleToVisibleSize?: boolean, useHqSampling?: boolean, targetDpi?: number): Promise<void>;
|
|
9
9
|
export declare function compressStructTree(id: string): Promise<void>;
|
|
10
10
|
export declare function compressAndSaveAs(id: string, outputPath: string, imageQuality?: number): Promise<void>;
|
|
11
11
|
export declare function compressInMemory(id: string, imageQuality?: number): Promise<Buffer>;
|
|
@@ -14,13 +14,18 @@ export declare function compressInMemory(id: string, imageQuality?: number): Pro
|
|
|
14
14
|
* {@link QPdfCompressionFlagsP}.
|
|
15
15
|
*/
|
|
16
16
|
export declare function toFlagsProto(options: AdvancedCompressionOptions): QPdfCompressionFlagsP;
|
|
17
|
+
/**
|
|
18
|
+
* Whether the pdfium image re-encode pass should run for these options
|
|
19
|
+
* (the effective target DPI is positive).
|
|
20
|
+
*/
|
|
21
|
+
export declare function pdfiumWillReEncode(options: AdvancedCompressionOptions): boolean;
|
|
17
22
|
/**
|
|
18
23
|
* Apply the advanced compression pipeline to an open engine document and
|
|
19
24
|
* save the result to disk. Mirrors .NET
|
|
20
25
|
* {@code PdfDocument.CompressAndSaveAs(string, AdvancedCompressionOptions)}.
|
|
21
26
|
*
|
|
22
|
-
* Step 1 (optional) — pdfium image re-encoding when
|
|
23
|
-
*
|
|
27
|
+
* Step 1 (optional) — pdfium image downsampling + re-encoding when
|
|
28
|
+
* {@code targetImageDpi > 0} (JpegQuality defaults to 85 when unset).
|
|
24
29
|
* Step 2 (optional) — struct-tree removal when {@code removeStructureTree}.
|
|
25
30
|
* Step 3 — qpdf {@code QPdf_Compression_CompressAndSaveAsAdvanced}.
|
|
26
31
|
*/
|
|
@@ -30,6 +35,13 @@ export declare function compressAndSaveAsAdvanced(id: string, outputPath: string
|
|
|
30
35
|
* result back. Mirrors .NET
|
|
31
36
|
* {@code PdfDocument.CompressAndSaveAs(byte[], string, AdvancedCompressionOptions, ...)}.
|
|
32
37
|
*
|
|
38
|
+
* Image DPI downsampling / struct-tree removal require a loaded document, so when
|
|
39
|
+
* either is requested the bytes are first opened into a temporary engine document
|
|
40
|
+
* (which also decrypts using the password), the shared {@link applyImageAndStructPasses}
|
|
41
|
+
* pre-passes run, and
|
|
42
|
+
* the resulting plaintext bytes are streamed to the qpdf pass. The temporary document
|
|
43
|
+
* is always released via {@code disposePdf} in a `finally` block.
|
|
44
|
+
*
|
|
33
45
|
* Returns the compressed bytes; when {@code outputPath} is non-empty also
|
|
34
46
|
* writes them to disk.
|
|
35
47
|
*/
|
|
@@ -1 +1 @@
|
|
|
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;
|
|
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;AAGzF,wBAAsB,aAAa,CAClC,EAAE,EAAE,MAAM,EACV,YAAY,EAAE,MAAM,EACpB,kBAAkB,UAAQ,EAC1B,aAAa,UAAQ,EACrB,SAAS,SAAI,GACX,OAAO,CAAC,IAAI,CAAC,CA0Bf;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,CAuBvF;AA0BD;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,0BAA0B,GAAG,OAAO,CAG/E;AA+BD;;;;;;;;;GASG;AACH,wBAAsB,yBAAyB,CAC9C,EAAE,EAAE,MAAM,EACV,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,0BAA0B,EACnC,QAAQ,SAAK,GACX,OAAO,CAAC,IAAI,CAAC,CA+Cf;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,kCAAkC,CACvD,QAAQ,EAAE,MAAM,EAChB,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,0BAA0B,EACnC,QAAQ,SAAK,GACX,OAAO,CAAC,MAAM,CAAC,CA0EjB;AAED,wBAAsB,sBAAsB,CAC3C,EAAE,EAAE,MAAM,EACV,YAAY,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC,QAAQ,CAAC,CAiCnB"}
|
|
@@ -12,22 +12,19 @@ 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.compressAndSaveAsAdvancedFromBytes = exports.compressAndSaveAsAdvanced = exports.toFlagsProto = exports.compressInMemory = exports.compressAndSaveAs = exports.compressStructTree = exports.compressImage = void 0;
|
|
15
|
+
exports.compressInMemoryStream = exports.compressAndSaveAsAdvancedFromBytes = exports.compressAndSaveAsAdvanced = exports.pdfiumWillReEncode = 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
20
|
const buffer_1 = require("buffer");
|
|
21
21
|
const compression_1 = require("../../../public/compression");
|
|
22
|
-
|
|
22
|
+
const io_1 = require("./io");
|
|
23
|
+
function compressImage(id, imageQuality, scaleToVisibleSize = false, useHqSampling = false, targetDpi = 0) {
|
|
23
24
|
return __awaiter(this, void 0, void 0, function* () {
|
|
24
25
|
const client = yield access_1.Access.ensureConnection();
|
|
25
26
|
return new Promise((resolve, reject) => {
|
|
26
|
-
client.Pdfium_Compress_CompressImages({
|
|
27
|
-
document: { documentId: id },
|
|
28
|
-
scaleToVisibleSize: scaleToVisibleSize,
|
|
29
|
-
quality: imageQuality,
|
|
30
|
-
}, (err, value) => {
|
|
27
|
+
client.Pdfium_Compress_CompressImages(Object.assign({ document: { documentId: id }, scaleToVisibleSize: scaleToVisibleSize, quality: imageQuality, useHqSampling: useHqSampling }, (targetDpi > 0 ? { targetDpi: targetDpi } : {})), (err, value) => {
|
|
31
28
|
if (err) {
|
|
32
29
|
reject(`${err.name}/n${err.message}`);
|
|
33
30
|
}
|
|
@@ -116,10 +113,8 @@ exports.compressInMemory = compressInMemory;
|
|
|
116
113
|
*/
|
|
117
114
|
function toFlagsProto(options) {
|
|
118
115
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
options.targetImageDpi > 0;
|
|
122
|
-
const optimizeImages = !pdfiumWillReEncode && options.jpegQuality != null;
|
|
116
|
+
const willReEncode = pdfiumWillReEncode(options);
|
|
117
|
+
const optimizeImages = !willReEncode && options.jpegQuality != null;
|
|
123
118
|
const flags = {
|
|
124
119
|
compressStreams: (_a = options.compressStreams) !== null && _a !== void 0 ? _a : true,
|
|
125
120
|
coalesceContents: (_b = options.coalesceContents) !== null && _b !== void 0 ? _b : true,
|
|
@@ -133,19 +128,71 @@ function toFlagsProto(options) {
|
|
|
133
128
|
optimizeImagesMinArea: (_k = options.optimizeImagesMinArea) !== null && _k !== void 0 ? _k : 0,
|
|
134
129
|
optimizeImages: optimizeImages,
|
|
135
130
|
};
|
|
136
|
-
if (!
|
|
131
|
+
if (!willReEncode && options.jpegQuality != null) {
|
|
137
132
|
flags.jpegQuality = options.jpegQuality;
|
|
138
133
|
}
|
|
139
134
|
return flags;
|
|
140
135
|
}
|
|
141
136
|
exports.toFlagsProto = toFlagsProto;
|
|
137
|
+
/**
|
|
138
|
+
* Default JPEG quality used when {@link AdvancedCompressionOptions.jpegQuality} is
|
|
139
|
+
* not set but image re-encoding still runs (because a target DPI is set).
|
|
140
|
+
*/
|
|
141
|
+
const DEFAULT_JPEG_QUALITY = 85;
|
|
142
|
+
/**
|
|
143
|
+
* Default target image DPI used when {@link AdvancedCompressionOptions.targetImageDpi}
|
|
144
|
+
* is omitted (`undefined`). Mirrors the .NET / Java class default of 150, so a
|
|
145
|
+
* default options object (e.g. `{}`) downsamples + re-encodes images. An explicit
|
|
146
|
+
* `null` disables downsampling.
|
|
147
|
+
*/
|
|
148
|
+
const DEFAULT_TARGET_IMAGE_DPI = 150;
|
|
149
|
+
/**
|
|
150
|
+
* Resolve the effective target DPI: `undefined` → 150 (the class default), `null` →
|
|
151
|
+
* disabled, otherwise the supplied value.
|
|
152
|
+
*/
|
|
153
|
+
function resolveTargetImageDpi(options) {
|
|
154
|
+
return options.targetImageDpi === undefined
|
|
155
|
+
? DEFAULT_TARGET_IMAGE_DPI
|
|
156
|
+
: options.targetImageDpi;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Whether the pdfium image re-encode pass should run for these options
|
|
160
|
+
* (the effective target DPI is positive).
|
|
161
|
+
*/
|
|
162
|
+
function pdfiumWillReEncode(options) {
|
|
163
|
+
const dpi = resolveTargetImageDpi(options);
|
|
164
|
+
return dpi !== null && dpi > 0;
|
|
165
|
+
}
|
|
166
|
+
exports.pdfiumWillReEncode = pdfiumWillReEncode;
|
|
167
|
+
/**
|
|
168
|
+
* Shared pdfium pre-passes for the advanced pipeline, run on an open engine
|
|
169
|
+
* document. Optional image DPI downsampling + re-encode (JpegQuality defaults to
|
|
170
|
+
* {@link DEFAULT_JPEG_QUALITY} when unset, so the default options still shrink images),
|
|
171
|
+
* then optional struct-tree removal.
|
|
172
|
+
*
|
|
173
|
+
* `scaleToVisibleSize` is passed as `true` to match .NET's `useVisible: true` argument
|
|
174
|
+
* (GrpcPdfClient.CompressImages maps it to ScaleToVisibleSize).
|
|
175
|
+
*/
|
|
176
|
+
function applyImageAndStructPasses(id, options) {
|
|
177
|
+
var _a, _b;
|
|
178
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
179
|
+
const dpi = resolveTargetImageDpi(options);
|
|
180
|
+
if (dpi !== null && dpi > 0) {
|
|
181
|
+
const quality = (_a = options.jpegQuality) !== null && _a !== void 0 ? _a : DEFAULT_JPEG_QUALITY;
|
|
182
|
+
yield compressImage(id, quality, true /* scaleToVisibleSize — matches .NET useVisible: true */, (_b = options.highQualityImageSubsampling) !== null && _b !== void 0 ? _b : true, dpi);
|
|
183
|
+
}
|
|
184
|
+
if (options.removeStructureTree) {
|
|
185
|
+
yield compressStructTree(id);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
142
189
|
/**
|
|
143
190
|
* Apply the advanced compression pipeline to an open engine document and
|
|
144
191
|
* save the result to disk. Mirrors .NET
|
|
145
192
|
* {@code PdfDocument.CompressAndSaveAs(string, AdvancedCompressionOptions)}.
|
|
146
193
|
*
|
|
147
|
-
* Step 1 (optional) — pdfium image re-encoding when
|
|
148
|
-
*
|
|
194
|
+
* Step 1 (optional) — pdfium image downsampling + re-encoding when
|
|
195
|
+
* {@code targetImageDpi > 0} (JpegQuality defaults to 85 when unset).
|
|
149
196
|
* Step 2 (optional) — struct-tree removal when {@code removeStructureTree}.
|
|
150
197
|
* Step 3 — qpdf {@code QPdf_Compression_CompressAndSaveAsAdvanced}.
|
|
151
198
|
*/
|
|
@@ -154,17 +201,8 @@ function compressAndSaveAsAdvanced(id, outputPath, options, password = "") {
|
|
|
154
201
|
if (!options) {
|
|
155
202
|
throw new Error("options must not be null");
|
|
156
203
|
}
|
|
157
|
-
//
|
|
158
|
-
|
|
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
|
-
}
|
|
204
|
+
// Steps 1 & 2 — pdfium image re-encoding + struct-tree removal.
|
|
205
|
+
yield applyImageAndStructPasses(id, options);
|
|
168
206
|
// Step 3 — qpdf advanced pass.
|
|
169
207
|
const client = yield access_1.Access.ensureConnection();
|
|
170
208
|
return new Promise((resolve, reject) => {
|
|
@@ -205,6 +243,13 @@ exports.compressAndSaveAsAdvanced = compressAndSaveAsAdvanced;
|
|
|
205
243
|
* result back. Mirrors .NET
|
|
206
244
|
* {@code PdfDocument.CompressAndSaveAs(byte[], string, AdvancedCompressionOptions, ...)}.
|
|
207
245
|
*
|
|
246
|
+
* Image DPI downsampling / struct-tree removal require a loaded document, so when
|
|
247
|
+
* either is requested the bytes are first opened into a temporary engine document
|
|
248
|
+
* (which also decrypts using the password), the shared {@link applyImageAndStructPasses}
|
|
249
|
+
* pre-passes run, and
|
|
250
|
+
* the resulting plaintext bytes are streamed to the qpdf pass. The temporary document
|
|
251
|
+
* is always released via {@code disposePdf} in a `finally` block.
|
|
252
|
+
*
|
|
208
253
|
* Returns the compressed bytes; when {@code outputPath} is non-empty also
|
|
209
254
|
* writes them to disk.
|
|
210
255
|
*/
|
|
@@ -216,6 +261,21 @@ function compressAndSaveAsAdvancedFromBytes(pdfBytes, outputPath, options, passw
|
|
|
216
261
|
if (!options) {
|
|
217
262
|
throw new Error("options must not be null");
|
|
218
263
|
}
|
|
264
|
+
let bytes = pdfBytes;
|
|
265
|
+
let pwd = password !== null && password !== void 0 ? password : "";
|
|
266
|
+
// Pre-pass: image downsampling + struct-tree removal need a loaded document.
|
|
267
|
+
if (pdfiumWillReEncode(options) || options.removeStructureTree) {
|
|
268
|
+
const tempId = yield (0, io_1.openPdfFileBuffer)(pdfBytes, { userPassword: pwd });
|
|
269
|
+
try {
|
|
270
|
+
yield applyImageAndStructPasses(tempId, options);
|
|
271
|
+
// Bytes are now decrypted + downsampled; the qpdf pass no longer needs the password.
|
|
272
|
+
bytes = yield (0, io_1.getBinaryData)(tempId);
|
|
273
|
+
pwd = "";
|
|
274
|
+
}
|
|
275
|
+
finally {
|
|
276
|
+
yield (0, io_1.disposePdf)(tempId);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
219
279
|
const client = yield access_1.Access.ensureConnection();
|
|
220
280
|
return new Promise((resolve, reject) => {
|
|
221
281
|
const stream = client.QPdf_Compression_CompressAndSaveAsAdvancedFromBytes();
|
|
@@ -247,11 +307,11 @@ function compressAndSaveAsAdvancedFromBytes(pdfBytes, outputPath, options, passw
|
|
|
247
307
|
stream.write({
|
|
248
308
|
info: {
|
|
249
309
|
outputPath: outputPath !== null && outputPath !== void 0 ? outputPath : "",
|
|
250
|
-
password:
|
|
310
|
+
password: pwd,
|
|
251
311
|
flags: toFlagsProto(options),
|
|
252
312
|
},
|
|
253
313
|
});
|
|
254
|
-
for (const chunk of (0, util_1.chunkBuffer)(
|
|
314
|
+
for (const chunk of (0, util_1.chunkBuffer)(bytes)) {
|
|
255
315
|
stream.write({ pdfBytesChunk: chunk });
|
|
256
316
|
}
|
|
257
317
|
stream.end();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compress.js","sourceRoot":"","sources":["compress.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAGA,yCAAoC;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;gBAC5B,WAAW,CAAC,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;aACpC;QACF,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAU,EAAE,EAAE;YACjC,WAAW,CAAC,OAAO,CAAC,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"}
|
|
@@ -13,11 +13,14 @@ import {PassThrough, Readable} from "stream";
|
|
|
13
13
|
import fs from "fs";
|
|
14
14
|
import {Buffer} from "buffer";
|
|
15
15
|
import {AdvancedCompressionOptions, ObjectStreamMode} from "../../../public/compression";
|
|
16
|
+
import {openPdfFileBuffer, getBinaryData, disposePdf} from "./io";
|
|
16
17
|
|
|
17
18
|
export async function compressImage(
|
|
18
19
|
id: string,
|
|
19
20
|
imageQuality: number,
|
|
20
|
-
scaleToVisibleSize = false
|
|
21
|
+
scaleToVisibleSize = false,
|
|
22
|
+
useHqSampling = false,
|
|
23
|
+
targetDpi = 0
|
|
21
24
|
): Promise<void> {
|
|
22
25
|
const client: IronPdfServiceClient = await Access.ensureConnection();
|
|
23
26
|
return new Promise(
|
|
@@ -27,6 +30,8 @@ export async function compressImage(
|
|
|
27
30
|
document: {documentId: id},
|
|
28
31
|
scaleToVisibleSize: scaleToVisibleSize,
|
|
29
32
|
quality: imageQuality,
|
|
33
|
+
useHqSampling: useHqSampling,
|
|
34
|
+
...(targetDpi > 0 ? {targetDpi: targetDpi} : {}),
|
|
30
35
|
},
|
|
31
36
|
(
|
|
32
37
|
err: ServiceError | null,
|
|
@@ -155,11 +160,8 @@ export async function compressInMemory(
|
|
|
155
160
|
* {@link QPdfCompressionFlagsP}.
|
|
156
161
|
*/
|
|
157
162
|
export function toFlagsProto(options: AdvancedCompressionOptions): QPdfCompressionFlagsP {
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
options.targetImageDpi !== undefined &&
|
|
161
|
-
options.targetImageDpi > 0;
|
|
162
|
-
const optimizeImages = !pdfiumWillReEncode && options.jpegQuality != null;
|
|
163
|
+
const willReEncode = pdfiumWillReEncode(options);
|
|
164
|
+
const optimizeImages = !willReEncode && options.jpegQuality != null;
|
|
163
165
|
|
|
164
166
|
const flags: QPdfCompressionFlagsP = {
|
|
165
167
|
compressStreams: options.compressStreams ?? true,
|
|
@@ -175,20 +177,82 @@ export function toFlagsProto(options: AdvancedCompressionOptions): QPdfCompressi
|
|
|
175
177
|
optimizeImages: optimizeImages,
|
|
176
178
|
};
|
|
177
179
|
|
|
178
|
-
if (!
|
|
180
|
+
if (!willReEncode && options.jpegQuality != null) {
|
|
179
181
|
flags.jpegQuality = options.jpegQuality;
|
|
180
182
|
}
|
|
181
183
|
|
|
182
184
|
return flags;
|
|
183
185
|
}
|
|
184
186
|
|
|
187
|
+
/**
|
|
188
|
+
* Default JPEG quality used when {@link AdvancedCompressionOptions.jpegQuality} is
|
|
189
|
+
* not set but image re-encoding still runs (because a target DPI is set).
|
|
190
|
+
*/
|
|
191
|
+
const DEFAULT_JPEG_QUALITY = 85;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Default target image DPI used when {@link AdvancedCompressionOptions.targetImageDpi}
|
|
195
|
+
* is omitted (`undefined`). Mirrors the .NET / Java class default of 150, so a
|
|
196
|
+
* default options object (e.g. `{}`) downsamples + re-encodes images. An explicit
|
|
197
|
+
* `null` disables downsampling.
|
|
198
|
+
*/
|
|
199
|
+
const DEFAULT_TARGET_IMAGE_DPI = 150;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Resolve the effective target DPI: `undefined` → 150 (the class default), `null` →
|
|
203
|
+
* disabled, otherwise the supplied value.
|
|
204
|
+
*/
|
|
205
|
+
function resolveTargetImageDpi(options: AdvancedCompressionOptions): number | null {
|
|
206
|
+
return options.targetImageDpi === undefined
|
|
207
|
+
? DEFAULT_TARGET_IMAGE_DPI
|
|
208
|
+
: options.targetImageDpi;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Whether the pdfium image re-encode pass should run for these options
|
|
213
|
+
* (the effective target DPI is positive).
|
|
214
|
+
*/
|
|
215
|
+
export function pdfiumWillReEncode(options: AdvancedCompressionOptions): boolean {
|
|
216
|
+
const dpi = resolveTargetImageDpi(options);
|
|
217
|
+
return dpi !== null && dpi > 0;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Shared pdfium pre-passes for the advanced pipeline, run on an open engine
|
|
222
|
+
* document. Optional image DPI downsampling + re-encode (JpegQuality defaults to
|
|
223
|
+
* {@link DEFAULT_JPEG_QUALITY} when unset, so the default options still shrink images),
|
|
224
|
+
* then optional struct-tree removal.
|
|
225
|
+
*
|
|
226
|
+
* `scaleToVisibleSize` is passed as `true` to match .NET's `useVisible: true` argument
|
|
227
|
+
* (GrpcPdfClient.CompressImages maps it to ScaleToVisibleSize).
|
|
228
|
+
*/
|
|
229
|
+
async function applyImageAndStructPasses(
|
|
230
|
+
id: string,
|
|
231
|
+
options: AdvancedCompressionOptions
|
|
232
|
+
): Promise<void> {
|
|
233
|
+
const dpi = resolveTargetImageDpi(options);
|
|
234
|
+
if (dpi !== null && dpi > 0) {
|
|
235
|
+
const quality = options.jpegQuality ?? DEFAULT_JPEG_QUALITY;
|
|
236
|
+
await compressImage(
|
|
237
|
+
id,
|
|
238
|
+
quality,
|
|
239
|
+
true /* scaleToVisibleSize — matches .NET useVisible: true */,
|
|
240
|
+
options.highQualityImageSubsampling ?? true,
|
|
241
|
+
dpi
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
if (options.removeStructureTree) {
|
|
245
|
+
await compressStructTree(id);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
185
249
|
/**
|
|
186
250
|
* Apply the advanced compression pipeline to an open engine document and
|
|
187
251
|
* save the result to disk. Mirrors .NET
|
|
188
252
|
* {@code PdfDocument.CompressAndSaveAs(string, AdvancedCompressionOptions)}.
|
|
189
253
|
*
|
|
190
|
-
* Step 1 (optional) — pdfium image re-encoding when
|
|
191
|
-
*
|
|
254
|
+
* Step 1 (optional) — pdfium image downsampling + re-encoding when
|
|
255
|
+
* {@code targetImageDpi > 0} (JpegQuality defaults to 85 when unset).
|
|
192
256
|
* Step 2 (optional) — struct-tree removal when {@code removeStructureTree}.
|
|
193
257
|
* Step 3 — qpdf {@code QPdf_Compression_CompressAndSaveAsAdvanced}.
|
|
194
258
|
*/
|
|
@@ -202,23 +266,8 @@ export async function compressAndSaveAsAdvanced(
|
|
|
202
266
|
throw new Error("options must not be null");
|
|
203
267
|
}
|
|
204
268
|
|
|
205
|
-
//
|
|
206
|
-
|
|
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
|
-
}
|
|
269
|
+
// Steps 1 & 2 — pdfium image re-encoding + struct-tree removal.
|
|
270
|
+
await applyImageAndStructPasses(id, options);
|
|
222
271
|
|
|
223
272
|
// Step 3 — qpdf advanced pass.
|
|
224
273
|
const client: IronPdfServiceClient = await Access.ensureConnection();
|
|
@@ -266,6 +315,13 @@ export async function compressAndSaveAsAdvanced(
|
|
|
266
315
|
* result back. Mirrors .NET
|
|
267
316
|
* {@code PdfDocument.CompressAndSaveAs(byte[], string, AdvancedCompressionOptions, ...)}.
|
|
268
317
|
*
|
|
318
|
+
* Image DPI downsampling / struct-tree removal require a loaded document, so when
|
|
319
|
+
* either is requested the bytes are first opened into a temporary engine document
|
|
320
|
+
* (which also decrypts using the password), the shared {@link applyImageAndStructPasses}
|
|
321
|
+
* pre-passes run, and
|
|
322
|
+
* the resulting plaintext bytes are streamed to the qpdf pass. The temporary document
|
|
323
|
+
* is always released via {@code disposePdf} in a `finally` block.
|
|
324
|
+
*
|
|
269
325
|
* Returns the compressed bytes; when {@code outputPath} is non-empty also
|
|
270
326
|
* writes them to disk.
|
|
271
327
|
*/
|
|
@@ -282,6 +338,22 @@ export async function compressAndSaveAsAdvancedFromBytes(
|
|
|
282
338
|
throw new Error("options must not be null");
|
|
283
339
|
}
|
|
284
340
|
|
|
341
|
+
let bytes = pdfBytes;
|
|
342
|
+
let pwd = password ?? "";
|
|
343
|
+
|
|
344
|
+
// Pre-pass: image downsampling + struct-tree removal need a loaded document.
|
|
345
|
+
if (pdfiumWillReEncode(options) || options.removeStructureTree) {
|
|
346
|
+
const tempId = await openPdfFileBuffer(pdfBytes, {userPassword: pwd});
|
|
347
|
+
try {
|
|
348
|
+
await applyImageAndStructPasses(tempId, options);
|
|
349
|
+
// Bytes are now decrypted + downsampled; the qpdf pass no longer needs the password.
|
|
350
|
+
bytes = await getBinaryData(tempId);
|
|
351
|
+
pwd = "";
|
|
352
|
+
} finally {
|
|
353
|
+
await disposePdf(tempId);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
285
357
|
const client: IronPdfServiceClient = await Access.ensureConnection();
|
|
286
358
|
|
|
287
359
|
return new Promise(
|
|
@@ -322,11 +394,11 @@ export async function compressAndSaveAsAdvancedFromBytes(
|
|
|
322
394
|
stream.write({
|
|
323
395
|
info: {
|
|
324
396
|
outputPath: outputPath ?? "",
|
|
325
|
-
password:
|
|
397
|
+
password: pwd,
|
|
326
398
|
flags: toFlagsProto(options),
|
|
327
399
|
},
|
|
328
400
|
});
|
|
329
|
-
for (const chunk of chunkBuffer(
|
|
401
|
+
for (const chunk of chunkBuffer(bytes)) {
|
|
330
402
|
stream.write({pdfBytesChunk: chunk});
|
|
331
403
|
}
|
|
332
404
|
stream.end();
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
import { ChangeTrackingModes, SaveOptions } from "../../../public/types";
|
|
4
|
+
import { PendingSignature } from "./signing";
|
|
4
5
|
export declare function openPdfFileBuffer(buffer: Buffer, option?: {
|
|
5
6
|
userPassword?: string | undefined;
|
|
6
7
|
ownerPassword?: string | undefined;
|
|
7
8
|
trackChanges?: ChangeTrackingModes | undefined;
|
|
8
9
|
} | undefined): Promise<string>;
|
|
9
|
-
export declare function getBinaryData(pdfDocumentId: string, saveOptions?: SaveOptions | undefined): Promise<Buffer>;
|
|
10
|
+
export declare function getBinaryData(pdfDocumentId: string, saveOptions?: SaveOptions | undefined, signatures?: PendingSignature[] | undefined): Promise<Buffer>;
|
|
10
11
|
export declare function disposePdf(pdfDocumentId: string): Promise<void>;
|
|
11
12
|
//# sourceMappingURL=io.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"io.d.ts","sourceRoot":"","sources":["io.ts"],"names":[],"mappings":";;AASA,OAAO,EAAC,mBAAmB,EAAE,WAAW,EAAC,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"io.d.ts","sourceRoot":"","sources":["io.ts"],"names":[],"mappings":";;AASA,OAAO,EAAC,mBAAmB,EAAE,WAAW,EAAC,MAAM,uBAAuB,CAAC;AAKvE,OAAO,EAAC,gBAAgB,EAAC,MAAM,WAAW,CAAC;AAE3C,wBAAsB,iBAAiB,CACtC,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE;IAAE,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,YAAY,CAAC,EAAE,mBAAmB,GAAG,SAAS,CAAA;CAAE,GAAG,SAAS,GAC5I,OAAO,CAAC,MAAM,CAAC,CAqCjB;AAED,wBAAsB,aAAa,CAClC,aAAa,EAAE,MAAM,EACrB,WAAW,CAAC,EAAE,WAAW,GAAG,SAAS,EACrC,UAAU,CAAC,EAAE,gBAAgB,EAAE,GAAG,SAAS,GACzC,OAAO,CAAC,MAAM,CAAC,CAkFjB;AAED,wBAAsB,UAAU,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAkBrE"}
|
|
@@ -41,7 +41,7 @@ function openPdfFileBuffer(buffer, option) {
|
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
43
|
exports.openPdfFileBuffer = openPdfFileBuffer;
|
|
44
|
-
function getBinaryData(pdfDocumentId, saveOptions) {
|
|
44
|
+
function getBinaryData(pdfDocumentId, saveOptions, signatures) {
|
|
45
45
|
return __awaiter(this, void 0, void 0, function* () {
|
|
46
46
|
const client = yield access_1.Access.ensureConnection();
|
|
47
47
|
return new Promise((resolve, reject) => {
|
|
@@ -69,41 +69,46 @@ function getBinaryData(pdfDocumentId, saveOptions) {
|
|
|
69
69
|
isIncremental: (_a = saveOptions === null || saveOptions === void 0 ? void 0 : saveOptions.incremental) !== null && _a !== void 0 ? _a : false,
|
|
70
70
|
},
|
|
71
71
|
});
|
|
72
|
-
//
|
|
73
|
-
//
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
//
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
72
|
+
// Digital signatures are added as placeholder fields by signPdf(), but the actual
|
|
73
|
+
// cryptographic signing only happens here, at save time. The signature descriptors
|
|
74
|
+
// (index/password/timestamp) and the raw certificate bytes must therefore be re-sent
|
|
75
|
+
// so the engine can fill in the reserved byte range. Skipping this leaves an empty
|
|
76
|
+
// placeholder and produces a PDF with an invalid signature.
|
|
77
|
+
if (signatures && signatures.length > 0) {
|
|
78
|
+
// The engine pairs each certificate with its reserved placeholder using two
|
|
79
|
+
// distinct indices (this matches the C# client, GrpcPdfClient.GetBytes):
|
|
80
|
+
//
|
|
81
|
+
// * rawSignaturesChunk.signatureIndex = the certificate's position in the
|
|
82
|
+
// `signatures` collection below. The engine groups the chunks by this value and
|
|
83
|
+
// looks the descriptor up positionally (`signatures.Signature[signatureIndex]`),
|
|
84
|
+
// so it must be the 0..n-1 array index, NOT the engine signature index.
|
|
85
|
+
// * PdfiumPdfSignatureP.index = the engine signature index returned by
|
|
86
|
+
// signPdf when the placeholder was reserved. The engine uses it to know which
|
|
87
|
+
// placeholder field this certificate fills. For an already-signed document this
|
|
88
|
+
// differs from the array position, which is exactly why the two are separate.
|
|
89
|
+
//
|
|
90
|
+
// Keep the collection order and the chunk order identical so position i in the
|
|
91
|
+
// collection always corresponds to signatureIndex i in the chunks.
|
|
92
|
+
stream.write({
|
|
93
|
+
signatures: {
|
|
94
|
+
signature: signatures.map((signature) => ({
|
|
95
|
+
index: signature.index,
|
|
96
|
+
password: signature.password,
|
|
97
|
+
timestampUrl: signature.timeStampUrl,
|
|
98
|
+
})),
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
signatures.forEach((signature, signatureIndex) => {
|
|
102
|
+
(0, util_1.chunkBuffer)(signature.certificate).forEach((chunk) => {
|
|
103
|
+
stream.write({
|
|
104
|
+
rawSignaturesChunk: {
|
|
105
|
+
rawSignatureChunk: chunk,
|
|
106
|
+
signatureIndex,
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
}
|
|
107
112
|
//request finished
|
|
108
113
|
stream.end();
|
|
109
114
|
});
|