compress-pdf-lib 1.0.1 → 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 +26 -4
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;
|
|
@@ -254,7 +254,15 @@ async function processPage(pdf, pool, pageNumber, totalPages, { quality, resolut
|
|
|
254
254
|
|
|
255
255
|
let scale;
|
|
256
256
|
|
|
257
|
-
if (
|
|
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) {
|
|
258
266
|
/*
|
|
259
267
|
* No downscaling — render at the max allowed multiplier so page
|
|
260
268
|
* quality is limited only by the JPEG quality setting, not by
|
|
@@ -337,7 +345,7 @@ async function processPage(pdf, pool, pageNumber, totalPages, { quality, resolut
|
|
|
337
345
|
PARALLEL PAGE PIPELINE
|
|
338
346
|
============================================================ */
|
|
339
347
|
|
|
340
|
-
async function processPages(pdf, pool, totalPages, { quality, resolution, workers, onProgress }) {
|
|
348
|
+
async function processPages(pdf, pool, totalPages, { quality, resolution, scale, workers, onProgress }) {
|
|
341
349
|
const results = new Array(totalPages);
|
|
342
350
|
|
|
343
351
|
const renderConcurrency = Math.max(1, Math.min(workers, 4));
|
|
@@ -356,6 +364,7 @@ async function processPages(pdf, pool, totalPages, { quality, resolution, worker
|
|
|
356
364
|
const result = await processPage(pdf, pool, pageNumber, totalPages, {
|
|
357
365
|
quality,
|
|
358
366
|
resolution,
|
|
367
|
+
scale,
|
|
359
368
|
});
|
|
360
369
|
|
|
361
370
|
results[pageNumber - 1] = result;
|
|
@@ -489,12 +498,24 @@ async function buildPdf(compressedPages, { originalBytes, startedAt, workersUsed
|
|
|
489
498
|
* @param {number|"original"} [options.resolution=1600] - max px on the longest
|
|
490
499
|
* page side. Pass "original" (or Infinity) to skip downscaling entirely and
|
|
491
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.
|
|
492
507
|
* @param {number} [options.workers] - override auto-detected worker count
|
|
493
508
|
* @param {(update: object) => void} [options.onProgress] - optional progress callback
|
|
494
509
|
* @returns {Promise<{file: File|Blob, stats: object}>}
|
|
495
510
|
*/
|
|
496
511
|
export async function compressPDF(input, options = {}) {
|
|
497
|
-
const {
|
|
512
|
+
const {
|
|
513
|
+
quality = 65,
|
|
514
|
+
resolution = 1600,
|
|
515
|
+
scale,
|
|
516
|
+
workers: workerOverride,
|
|
517
|
+
onProgress,
|
|
518
|
+
} = options;
|
|
498
519
|
|
|
499
520
|
const startedAt = performance.now();
|
|
500
521
|
|
|
@@ -519,6 +540,7 @@ export async function compressPDF(input, options = {}) {
|
|
|
519
540
|
const compressedPages = await processPages(pdf, pool, totalPages, {
|
|
520
541
|
quality,
|
|
521
542
|
resolution,
|
|
543
|
+
scale,
|
|
522
544
|
workers: workerCount,
|
|
523
545
|
onProgress,
|
|
524
546
|
});
|