pdf-parser.js 4.6.0 → 4.8.0

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/dist/read.cjs CHANGED
@@ -497,13 +497,18 @@ function imagePlacementFrom(matrix) {
497
497
  rotationDeg: rotationDeg !== 0 ? rotationDeg : void 0
498
498
  };
499
499
  }
500
- function registerExtractedImage(format, bytes, widthPx, heightPx, images) {
500
+ function registerExtractedImage(format, bytes, widthPx, heightPx, images, original) {
501
501
  const imageId = `img${require_bytes_crc32.crc32(bytes).toString(16)}`;
502
502
  if (!(imageId in images)) images[imageId] = {
503
503
  format,
504
504
  base64: require_util_base64.bytesToBase64(bytes),
505
505
  widthPx,
506
- heightPx
506
+ heightPx,
507
+ ...original !== void 0 ? { original: {
508
+ filter: original.filter,
509
+ base64: require_util_base64.bytesToBase64(original.bytes),
510
+ ...original.globalsBytes !== void 0 ? { jbig2GlobalsBase64: require_util_base64.bytesToBase64(original.globalsBytes) } : {}
511
+ } } : {}
507
512
  };
508
513
  return imageId;
509
514
  }
@@ -514,7 +519,7 @@ function resolveCachedImageId(dict, raw, images, cache, resolver, sink) {
514
519
  cache.set(dict, null);
515
520
  return;
516
521
  }
517
- const imageId = registerExtractedImage(decoded.format, decoded.bytes, decoded.widthPx, decoded.heightPx, images);
522
+ const imageId = registerExtractedImage(decoded.format, decoded.bytes, decoded.widthPx, decoded.heightPx, images, decoded.original);
518
523
  cache.set(dict, imageId);
519
524
  return imageId;
520
525
  }
@@ -536,7 +541,7 @@ function convertInlineImage(item, pageMatrix, images, resolver, sink) {
536
541
  if (decoded === void 0) return;
537
542
  return {
538
543
  kind: "image",
539
- imageId: registerExtractedImage(decoded.format, decoded.bytes, decoded.widthPx, decoded.heightPx, images),
544
+ imageId: registerExtractedImage(decoded.format, decoded.bytes, decoded.widthPx, decoded.heightPx, images, decoded.original),
540
545
  ...imagePlacementFrom(require_matrix.multiplyMatrices(item.matrix, pageMatrix)),
541
546
  ...item.layerName !== void 0 ? { layer: item.layerName } : {}
542
547
  };
package/dist/read.js CHANGED
@@ -496,13 +496,18 @@ function imagePlacementFrom(matrix) {
496
496
  rotationDeg: rotationDeg !== 0 ? rotationDeg : void 0
497
497
  };
498
498
  }
499
- function registerExtractedImage(format, bytes, widthPx, heightPx, images) {
499
+ function registerExtractedImage(format, bytes, widthPx, heightPx, images, original) {
500
500
  const imageId = `img${crc32(bytes).toString(16)}`;
501
501
  if (!(imageId in images)) images[imageId] = {
502
502
  format,
503
503
  base64: bytesToBase64(bytes),
504
504
  widthPx,
505
- heightPx
505
+ heightPx,
506
+ ...original !== void 0 ? { original: {
507
+ filter: original.filter,
508
+ base64: bytesToBase64(original.bytes),
509
+ ...original.globalsBytes !== void 0 ? { jbig2GlobalsBase64: bytesToBase64(original.globalsBytes) } : {}
510
+ } } : {}
506
511
  };
507
512
  return imageId;
508
513
  }
@@ -513,7 +518,7 @@ function resolveCachedImageId(dict, raw, images, cache, resolver, sink) {
513
518
  cache.set(dict, null);
514
519
  return;
515
520
  }
516
- const imageId = registerExtractedImage(decoded.format, decoded.bytes, decoded.widthPx, decoded.heightPx, images);
521
+ const imageId = registerExtractedImage(decoded.format, decoded.bytes, decoded.widthPx, decoded.heightPx, images, decoded.original);
517
522
  cache.set(dict, imageId);
518
523
  return imageId;
519
524
  }
@@ -535,7 +540,7 @@ function convertInlineImage(item, pageMatrix, images, resolver, sink) {
535
540
  if (decoded === void 0) return;
536
541
  return {
537
542
  kind: "image",
538
- imageId: registerExtractedImage(decoded.format, decoded.bytes, decoded.widthPx, decoded.heightPx, images),
543
+ imageId: registerExtractedImage(decoded.format, decoded.bytes, decoded.widthPx, decoded.heightPx, images, decoded.original),
539
544
  ...imagePlacementFrom(multiplyMatrices(item.matrix, pageMatrix)),
540
545
  ...item.layerName !== void 0 ? { layer: item.layerName } : {}
541
546
  };
package/dist/write.cjs CHANGED
@@ -206,7 +206,28 @@ function preparePngImage(bytes, compress) {
206
206
  }
207
207
  };
208
208
  }
209
+ function preparePassthroughImage(asset, compress) {
210
+ if (asset.original === void 0) throw new Error("preparePassthroughImage: asset carries no original stream");
211
+ const png = preparePngImage(require_util_base64.base64ToBytes(asset.base64), compress);
212
+ const entries = /* @__PURE__ */ new Map([
213
+ ["Type", require_objects.pdfName("XObject")],
214
+ ["Subtype", require_objects.pdfName("Image")],
215
+ ["Width", require_objects.pdfNum(asset.widthPx)],
216
+ ["Height", require_objects.pdfNum(asset.heightPx)],
217
+ ["Filter", require_objects.pdfName(asset.original.filter === "jbig2" ? "JBIG2Decode" : "JPXDecode")]
218
+ ]);
219
+ if (asset.original.filter === "jbig2") {
220
+ entries.set("ColorSpace", require_objects.pdfName("DeviceGray"));
221
+ entries.set("BitsPerComponent", require_objects.pdfNum(1));
222
+ }
223
+ return {
224
+ dict: require_objects.pdfDict(entries),
225
+ raw: require_util_base64.base64ToBytes(asset.original.base64),
226
+ ...png.alpha !== void 0 ? { alpha: png.alpha } : {}
227
+ };
228
+ }
209
229
  function prepareImage(asset, compress) {
230
+ if (asset.original !== void 0) return preparePassthroughImage(asset, compress);
210
231
  const bytes = require_util_base64.base64ToBytes(asset.base64);
211
232
  return asset.format === "jpeg" ? prepareJpegImage(bytes) : preparePngImage(bytes, compress);
212
233
  }
@@ -361,9 +382,13 @@ function writePdf(doc, options = {}) {
361
382
  const prepared = prepareImage(asset, compress);
362
383
  const imageNum = nextObjNum++;
363
384
  const smaskNum = prepared.alpha === void 0 ? void 0 : nextObjNum++;
385
+ const globalsBase64 = asset.original?.filter === "jbig2" ? asset.original.jbig2GlobalsBase64 : void 0;
386
+ const globalsNum = globalsBase64 !== void 0 ? nextObjNum++ : void 0;
364
387
  imageAllocs.set(imageId, {
365
388
  imageNum,
366
389
  smaskNum,
390
+ globalsNum,
391
+ globalsBase64,
367
392
  resourceName: `Im${index + 1}`,
368
393
  prepared
369
394
  });
@@ -708,6 +733,14 @@ function writePdf(doc, options = {}) {
708
733
  value: require_objects.pdfStream(alloc.prepared.alpha.dict, alloc.prepared.alpha.raw)
709
734
  });
710
735
  }
736
+ if (alloc.globalsNum !== void 0 && alloc.globalsBase64 !== void 0) {
737
+ const globalsRaw = require_util_base64.base64ToBytes(alloc.globalsBase64);
738
+ alloc.prepared.dict.entries.set("DecodeParms", require_objects.pdfDict({ JBIG2Globals: require_objects.pdfRef(alloc.globalsNum, 0) }));
739
+ objects.push({
740
+ num: alloc.globalsNum,
741
+ value: require_objects.pdfStream(require_objects.pdfDict(compress ? /* @__PURE__ */ new Map([["Filter", require_objects.pdfName("FlateDecode")]]) : /* @__PURE__ */ new Map()), compress ? require_bytes_flate.deflate(globalsRaw) : globalsRaw)
742
+ });
743
+ }
711
744
  objects.push({
712
745
  num: alloc.imageNum,
713
746
  value: require_objects.pdfStream(alloc.prepared.dict, alloc.prepared.raw)
package/dist/write.js CHANGED
@@ -205,7 +205,28 @@ function preparePngImage(bytes, compress) {
205
205
  }
206
206
  };
207
207
  }
208
+ function preparePassthroughImage(asset, compress) {
209
+ if (asset.original === void 0) throw new Error("preparePassthroughImage: asset carries no original stream");
210
+ const png = preparePngImage(base64ToBytes(asset.base64), compress);
211
+ const entries = /* @__PURE__ */ new Map([
212
+ ["Type", pdfName("XObject")],
213
+ ["Subtype", pdfName("Image")],
214
+ ["Width", pdfNum(asset.widthPx)],
215
+ ["Height", pdfNum(asset.heightPx)],
216
+ ["Filter", pdfName(asset.original.filter === "jbig2" ? "JBIG2Decode" : "JPXDecode")]
217
+ ]);
218
+ if (asset.original.filter === "jbig2") {
219
+ entries.set("ColorSpace", pdfName("DeviceGray"));
220
+ entries.set("BitsPerComponent", pdfNum(1));
221
+ }
222
+ return {
223
+ dict: pdfDict(entries),
224
+ raw: base64ToBytes(asset.original.base64),
225
+ ...png.alpha !== void 0 ? { alpha: png.alpha } : {}
226
+ };
227
+ }
208
228
  function prepareImage(asset, compress) {
229
+ if (asset.original !== void 0) return preparePassthroughImage(asset, compress);
209
230
  const bytes = base64ToBytes(asset.base64);
210
231
  return asset.format === "jpeg" ? prepareJpegImage(bytes) : preparePngImage(bytes, compress);
211
232
  }
@@ -360,9 +381,13 @@ function writePdf(doc, options = {}) {
360
381
  const prepared = prepareImage(asset, compress);
361
382
  const imageNum = nextObjNum++;
362
383
  const smaskNum = prepared.alpha === void 0 ? void 0 : nextObjNum++;
384
+ const globalsBase64 = asset.original?.filter === "jbig2" ? asset.original.jbig2GlobalsBase64 : void 0;
385
+ const globalsNum = globalsBase64 !== void 0 ? nextObjNum++ : void 0;
363
386
  imageAllocs.set(imageId, {
364
387
  imageNum,
365
388
  smaskNum,
389
+ globalsNum,
390
+ globalsBase64,
366
391
  resourceName: `Im${index + 1}`,
367
392
  prepared
368
393
  });
@@ -707,6 +732,14 @@ function writePdf(doc, options = {}) {
707
732
  value: pdfStream(alloc.prepared.alpha.dict, alloc.prepared.alpha.raw)
708
733
  });
709
734
  }
735
+ if (alloc.globalsNum !== void 0 && alloc.globalsBase64 !== void 0) {
736
+ const globalsRaw = base64ToBytes(alloc.globalsBase64);
737
+ alloc.prepared.dict.entries.set("DecodeParms", pdfDict({ JBIG2Globals: pdfRef(alloc.globalsNum, 0) }));
738
+ objects.push({
739
+ num: alloc.globalsNum,
740
+ value: pdfStream(pdfDict(compress ? /* @__PURE__ */ new Map([["Filter", pdfName("FlateDecode")]]) : /* @__PURE__ */ new Map()), compress ? deflate(globalsRaw) : globalsRaw)
741
+ });
742
+ }
710
743
  objects.push({
711
744
  num: alloc.imageNum,
712
745
  value: pdfStream(alloc.prepared.dict, alloc.prepared.raw)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdf-parser.js",
3
- "version": "4.6.0",
3
+ "version": "4.8.0",
4
4
  "description": "Hand-written, dependency-minimal PDF codec: parses arbitrary real-world PDFs and generates new ones, built on its own codec-owned LayoutDocument item model and Zod 4 codecs.",
5
5
  "type": "module",
6
6
  "repository": {
@@ -72,8 +72,8 @@
72
72
  ],
73
73
  "license": "MIT",
74
74
  "dependencies": {
75
- "byte-codec": "1.4.2",
76
- "document-schema.js": "7.10.0",
75
+ "byte-codec": "1.5.0",
76
+ "document-schema.js": "7.11.0",
77
77
  "fflate": "0.8.3",
78
78
  "zod": "4.4.3"
79
79
  },