@bentopdf/pymupdf-wasm 0.11.14 → 0.11.16

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 (2) hide show
  1. package/dist/index.js +77 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -2320,12 +2320,88 @@ doc.scrub(
2320
2320
 
2321
2321
  # 2. Image compression
2322
2322
  if ${compressImages ? "True" : "False"}:
2323
+ import math as _math
2324
+
2325
+ _dpi_target = ${dpiTarget}
2326
+ _dpi_threshold = ${dpiThreshold}
2327
+ _set_to_gray = ${convertToGray ? "True" : "False"}
2328
+ _effective_threshold = max(_dpi_threshold or 0, (_dpi_target or 0) + 10) if _dpi_target else None
2329
+
2330
+ # Pass 1: Handle lossless (PNG/Flate) images via page.replace_image()
2331
+ # Calculate DPI for each xref
2332
+ _xref_dpi = {}
2333
+ for _page in doc:
2334
+ for _info in _page.get_image_info(hashes=False, xrefs=True):
2335
+ _xref = _info.get("xref", 0)
2336
+ if _xref <= 0:
2337
+ continue
2338
+ _bbox = _info.get("bbox")
2339
+ _w = _info.get("width", 0)
2340
+ _h = _info.get("height", 0)
2341
+ if _bbox and _w > 0 and _h > 0:
2342
+ _disp_w = abs(_bbox[2] - _bbox[0])
2343
+ _disp_h = abs(_bbox[3] - _bbox[1])
2344
+ if _disp_w > 0 and _disp_h > 0:
2345
+ _dpi = min(_w / _disp_w * 72, _h / _disp_h * 72)
2346
+ if _xref not in _xref_dpi or _dpi < _xref_dpi[_xref]:
2347
+ _xref_dpi[_xref] = _dpi
2348
+
2349
+ _handled = set()
2350
+ for _page in doc:
2351
+ for _img in _page.get_images():
2352
+ _xref = _img[0]
2353
+ if _xref in _handled:
2354
+ continue
2355
+ _handled.add(_xref)
2356
+
2357
+ _mask_xref = _img[1]
2358
+ _xref_obj = doc.xref_object(_xref)
2359
+
2360
+ if "FlateDecode" not in _xref_obj:
2361
+ continue
2362
+
2363
+ _min_dpi = _xref_dpi.get(_xref, float("inf"))
2364
+ _needs_downscale = bool(
2365
+ _dpi_target and _effective_threshold
2366
+ and _min_dpi != float("inf")
2367
+ and _min_dpi > _effective_threshold
2368
+ )
2369
+ if not _needs_downscale and not _set_to_gray:
2370
+ continue
2371
+
2372
+ try:
2373
+ _base = pymupdf.Pixmap(doc, _xref)
2374
+
2375
+ if _base.alpha:
2376
+ _base = pymupdf.Pixmap(_base, 0)
2377
+
2378
+ if _mask_xref:
2379
+ _mask = pymupdf.Pixmap(doc, _mask_xref)
2380
+ _base = pymupdf.Pixmap(_base, _mask)
2381
+
2382
+ if _set_to_gray and _base.colorspace and _base.colorspace.n > 1:
2383
+ _base = pymupdf.Pixmap(pymupdf.csGRAY, _base)
2384
+ elif _base.colorspace and _base.colorspace.n > 3:
2385
+ _base = pymupdf.Pixmap(pymupdf.csRGB, _base)
2386
+
2387
+ if _needs_downscale:
2388
+ _ratio = _min_dpi / _dpi_target
2389
+ _shrink_n = max(0, min(7, int(_math.log2(_ratio))))
2390
+ if _shrink_n > 0:
2391
+ _base.shrink(_shrink_n)
2392
+
2393
+ _page.replace_image(_xref, pixmap=_base)
2394
+ _base = None
2395
+ except Exception as _e:
2396
+ pass
2397
+
2398
+ # Pass 2: Handle lossy (JPEG) images via rewrite_images
2323
2399
  doc.rewrite_images(
2324
2400
  dpi_threshold=${dpiThreshold},
2325
2401
  dpi_target=${dpiTarget},
2326
2402
  quality=${imageQuality},
2403
+ lossless=False,
2327
2404
  lossy=${processLossy ? "True" : "False"},
2328
- lossless=${processLossless ? "True" : "False"},
2329
2405
  bitonal=${processBitonal ? "True" : "False"},
2330
2406
  color=${processColor ? "True" : "False"},
2331
2407
  gray=${processGray ? "True" : "False"},
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bentopdf/pymupdf-wasm",
3
- "version": "0.11.14",
3
+ "version": "0.11.16",
4
4
  "description": "PyMuPDF compiled to WebAssembly - Full PDF manipulation in the browser",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",