compress-pdf-lib 1.0.0 → 1.0.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 +1 -1
- package/src/compress.js +43 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "compress-pdf-lib",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Client-side PDF compression: pdf.js render + mozjpeg (WASM) encoding via a parallel worker pool + pdf-lib rebuild. No server, no UI. Ships as raw ESM source for Vite-based apps (React + Vite, Astro).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
package/src/compress.js
CHANGED
|
@@ -238,7 +238,7 @@ async function normalizeInput(input) {
|
|
|
238
238
|
RENDER + COMPRESS ONE PAGE
|
|
239
239
|
============================================================ */
|
|
240
240
|
|
|
241
|
-
async function processPage(pdf, pool, pageNumber, totalPages, { quality, resolution }) {
|
|
241
|
+
async function processPage(pdf, pool, pageNumber, totalPages, { quality, resolution, scale: fixedScale }) {
|
|
242
242
|
let page = null;
|
|
243
243
|
let canvas = null;
|
|
244
244
|
let bitmap = null;
|
|
@@ -252,9 +252,28 @@ async function processPage(pdf, pool, pageNumber, totalPages, { quality, resolut
|
|
|
252
252
|
|
|
253
253
|
const largestDimension = Math.max(pdfWidth, pdfHeight);
|
|
254
254
|
|
|
255
|
-
let scale
|
|
256
|
-
|
|
257
|
-
|
|
255
|
+
let scale;
|
|
256
|
+
|
|
257
|
+
if (typeof fixedScale === "number") {
|
|
258
|
+
/*
|
|
259
|
+
* Direct scale multiplier — same units as pdf.js's own
|
|
260
|
+
* page.getViewport({ scale }). 1 = native ~72 DPI, 2 = ~144 DPI,
|
|
261
|
+
* 3 = ~216 DPI (max allowed). Proportional across any page size,
|
|
262
|
+
* unlike a fixed pixel target.
|
|
263
|
+
*/
|
|
264
|
+
scale = Math.max(0.25, Math.min(fixedScale, 3));
|
|
265
|
+
} else if (resolution === "original" || resolution === Infinity) {
|
|
266
|
+
/*
|
|
267
|
+
* No downscaling — render at the max allowed multiplier so page
|
|
268
|
+
* quality is limited only by the JPEG quality setting, not by
|
|
269
|
+
* resizing.
|
|
270
|
+
*/
|
|
271
|
+
scale = 3;
|
|
272
|
+
} else {
|
|
273
|
+
scale = resolution / largestDimension;
|
|
274
|
+
scale = Math.max(scale, 0.25);
|
|
275
|
+
scale = Math.min(scale, 3);
|
|
276
|
+
}
|
|
258
277
|
|
|
259
278
|
const viewport = page.getViewport({ scale });
|
|
260
279
|
const width = Math.ceil(viewport.width);
|
|
@@ -326,7 +345,7 @@ async function processPage(pdf, pool, pageNumber, totalPages, { quality, resolut
|
|
|
326
345
|
PARALLEL PAGE PIPELINE
|
|
327
346
|
============================================================ */
|
|
328
347
|
|
|
329
|
-
async function processPages(pdf, pool, totalPages, { quality, resolution, workers, onProgress }) {
|
|
348
|
+
async function processPages(pdf, pool, totalPages, { quality, resolution, scale, workers, onProgress }) {
|
|
330
349
|
const results = new Array(totalPages);
|
|
331
350
|
|
|
332
351
|
const renderConcurrency = Math.max(1, Math.min(workers, 4));
|
|
@@ -345,6 +364,7 @@ async function processPages(pdf, pool, totalPages, { quality, resolution, worker
|
|
|
345
364
|
const result = await processPage(pdf, pool, pageNumber, totalPages, {
|
|
346
365
|
quality,
|
|
347
366
|
resolution,
|
|
367
|
+
scale,
|
|
348
368
|
});
|
|
349
369
|
|
|
350
370
|
results[pageNumber - 1] = result;
|
|
@@ -475,13 +495,27 @@ async function buildPdf(compressedPages, { originalBytes, startedAt, workersUsed
|
|
|
475
495
|
* @param {File|Blob|ArrayBuffer|ArrayBufferView} input
|
|
476
496
|
* @param {Object} [options]
|
|
477
497
|
* @param {number} [options.quality=65] - JPEG quality, 0-100
|
|
478
|
-
* @param {number} [options.resolution=1600] - max px on the longest
|
|
498
|
+
* @param {number|"original"} [options.resolution=1600] - max px on the longest
|
|
499
|
+
* page side. Pass "original" (or Infinity) to skip downscaling entirely and
|
|
500
|
+
* rely on `quality` alone — renders at the max supported multiplier (3x).
|
|
501
|
+
* Ignored if `scale` is set.
|
|
502
|
+
* @param {number} [options.scale] - direct render-scale multiplier (same units
|
|
503
|
+
* as pdf.js's `page.getViewport({ scale })`), clamped to 0.25–3.
|
|
504
|
+
* 1 = native ~72 DPI, 2 ≈ 144 DPI, 3 ≈ 216 DPI (max). Proportional across
|
|
505
|
+
* any page size, unlike `resolution`'s fixed pixel target. Takes
|
|
506
|
+
* precedence over `resolution` when provided.
|
|
479
507
|
* @param {number} [options.workers] - override auto-detected worker count
|
|
480
508
|
* @param {(update: object) => void} [options.onProgress] - optional progress callback
|
|
481
509
|
* @returns {Promise<{file: File|Blob, stats: object}>}
|
|
482
510
|
*/
|
|
483
511
|
export async function compressPDF(input, options = {}) {
|
|
484
|
-
const {
|
|
512
|
+
const {
|
|
513
|
+
quality = 65,
|
|
514
|
+
resolution = 1600,
|
|
515
|
+
scale,
|
|
516
|
+
workers: workerOverride,
|
|
517
|
+
onProgress,
|
|
518
|
+
} = options;
|
|
485
519
|
|
|
486
520
|
const startedAt = performance.now();
|
|
487
521
|
|
|
@@ -506,6 +540,7 @@ export async function compressPDF(input, options = {}) {
|
|
|
506
540
|
const compressedPages = await processPages(pdf, pool, totalPages, {
|
|
507
541
|
quality,
|
|
508
542
|
resolution,
|
|
543
|
+
scale,
|
|
509
544
|
workers: workerCount,
|
|
510
545
|
onProgress,
|
|
511
546
|
});
|
|
@@ -534,4 +569,4 @@ export async function compressPDF(input, options = {}) {
|
|
|
534
569
|
|
|
535
570
|
pool.destroy();
|
|
536
571
|
}
|
|
537
|
-
}
|
|
572
|
+
}
|