pdf-parser.js 4.5.6 → 4.7.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.
Files changed (50) hide show
  1. package/README.md +5 -5
  2. package/dist/codec.d.cts +8 -0
  3. package/dist/codec.d.ts +8 -0
  4. package/dist/content-write.d.cts +1 -1
  5. package/dist/content-write.d.ts +1 -1
  6. package/dist/{embedded-font-B4p9a3O3.d.ts → embedded-font-BRhZfEN2.d.ts} +1 -1
  7. package/dist/{embedded-font-oc65VGlr.d.cts → embedded-font-DJUn9kMH.d.cts} +1 -1
  8. package/dist/embedded-font-write.d.cts +1 -1
  9. package/dist/embedded-font-write.d.ts +1 -1
  10. package/dist/embedded-font.cjs +1 -1
  11. package/dist/embedded-font.d.cts +1 -1
  12. package/dist/embedded-font.d.ts +1 -1
  13. package/dist/embedded-font.js +1 -1
  14. package/dist/filters.cjs +14 -3
  15. package/dist/filters.d.cts +4 -0
  16. package/dist/filters.d.ts +4 -0
  17. package/dist/filters.js +14 -3
  18. package/dist/font-registry.d.cts +1 -1
  19. package/dist/font-registry.d.ts +1 -1
  20. package/dist/gdef-table.cjs +48 -0
  21. package/dist/gdef-table.d.cts +15 -0
  22. package/dist/gdef-table.d.ts +15 -0
  23. package/dist/gdef-table.js +42 -0
  24. package/dist/gsub-table-Bm9kSMzk.d.cts +16 -0
  25. package/dist/gsub-table-CdfESH2-.d.ts +16 -0
  26. package/dist/gsub-table.cjs +404 -87
  27. package/dist/gsub-table.d.cts +2 -2
  28. package/dist/gsub-table.d.ts +2 -2
  29. package/dist/gsub-table.js +404 -89
  30. package/dist/images-read.cjs +12 -2
  31. package/dist/images-read.d.cts +5 -0
  32. package/dist/images-read.d.ts +5 -0
  33. package/dist/images-read.js +12 -2
  34. package/dist/index.d.cts +1 -1
  35. package/dist/index.d.ts +1 -1
  36. package/dist/layout.cjs +6 -1
  37. package/dist/layout.d.cts +16 -0
  38. package/dist/layout.d.ts +16 -0
  39. package/dist/layout.js +6 -1
  40. package/dist/read.cjs +9 -4
  41. package/dist/read.js +9 -4
  42. package/dist/tounicode.cjs +1 -1
  43. package/dist/tounicode.js +1 -1
  44. package/dist/write.cjs +50 -5
  45. package/dist/write.d.cts +1 -1
  46. package/dist/write.d.ts +1 -1
  47. package/dist/write.js +50 -5
  48. package/package.json +3 -3
  49. package/dist/gsub-table-BRTQ8EUW.d.ts +0 -10
  50. package/dist/gsub-table-URczLd85.d.cts +0 -10
@@ -248,7 +248,11 @@ function readJpeg2000Image(dict, raw, resolver, sink) {
248
248
  format: "png",
249
249
  bytes: encodePng(withAlpha),
250
250
  widthPx: image.width,
251
- heightPx: image.height
251
+ heightPx: image.height,
252
+ original: {
253
+ filter: "jpeg2000",
254
+ bytes: raw
255
+ }
252
256
  };
253
257
  }
254
258
  function readImageXObject(dict, raw, resolver, sink) {
@@ -261,6 +265,11 @@ function readImageXObject(dict, raw, resolver, sink) {
261
265
  return;
262
266
  }
263
267
  const decoded = decodeStream(raw, dict, sink, (obj) => resolver.resolve(obj));
268
+ const jbig2Original = decoded.jbig2 === void 0 ? void 0 : {
269
+ filter: "jbig2",
270
+ bytes: decoded.jbig2.encoded,
271
+ ...decoded.jbig2.globals !== void 0 ? { globalsBytes: decoded.jbig2.globals } : {}
272
+ };
264
273
  if (decoded.remainingFilter === "DCTDecode") {
265
274
  let info;
266
275
  try {
@@ -322,7 +331,8 @@ function readImageXObject(dict, raw, resolver, sink) {
322
331
  format: "png",
323
332
  bytes: encodePng(withAlpha),
324
333
  widthPx: width,
325
- heightPx: height
334
+ heightPx: height,
335
+ ...jbig2Original !== void 0 ? { original: jbig2Original } : {}
326
336
  };
327
337
  }
328
338
  //#endregion
package/dist/index.d.cts CHANGED
@@ -3,7 +3,7 @@ import { NOOP_DIAGNOSTIC_SINK, PdfDiagnostic, PdfDiagnosticSeverity, PdfDiagnost
3
3
  import { LAYOUT_FORMAT_VERSION, LayoutAnnotation, LayoutAnnotationQuad, LayoutAnnotationQuadSchema, LayoutAnnotationSchema, LayoutAttachment, LayoutAttachmentSchema, LayoutDestination, LayoutDestinationSchema, LayoutDestinationTarget, LayoutDestinationTargetSchema, LayoutDocument, LayoutDocumentSchema, LayoutEllipse, LayoutEllipseSchema, LayoutFormField, LayoutFormFieldSchema, LayoutFormWidget, LayoutFormWidgetSchema, LayoutImage, LayoutImageAsset, LayoutImageAssetSchema, LayoutImageSchema, LayoutInternalLink, LayoutInternalLinkSchema, LayoutItem, LayoutItemSchema, LayoutLayer, LayoutLayerSchema, LayoutLine, LayoutLineSchema, LayoutLink, LayoutLinkSchema, LayoutOutlineItem, LayoutOutlineItemSchema, LayoutPage, LayoutPageSchema, LayoutPath, LayoutPathSchema, LayoutPathSegment, LayoutPathSegmentSchema, LayoutRect, LayoutRectSchema, LayoutStructureElement, LayoutStructureElementSchema, LayoutSubpath, LayoutSubpathSchema, LayoutText, LayoutTextSchema } from "./layout.cjs";
4
4
  import { t as GlyphInkBounds } from "./glyph-bounds-BV_Z40KS.cjs";
5
5
  import { PdfBytesSchema, pdfCodec } from "./codec.cjs";
6
- import { n as EmbeddedFaceMetrics, r as EmbeddedFaceSubstitution, t as EmbeddedFace } from "./embedded-font-oc65VGlr.cjs";
6
+ import { n as EmbeddedFaceMetrics, r as EmbeddedFaceSubstitution, t as EmbeddedFace } from "./embedded-font-DJUn9kMH.cjs";
7
7
  import { WinAnsiSubstitution } from "./winansi.cjs";
8
8
  import { PdfEncryptionError, PdfEncryptionOptions, PdfEncryptionPermissions, PdfEncryptionScheme } from "./encrypt-write.cjs";
9
9
  import { FontFace, FontFaceParseError, readFontFace } from "./font-face.cjs";
package/dist/index.d.ts CHANGED
@@ -3,7 +3,7 @@ import { NOOP_DIAGNOSTIC_SINK, PdfDiagnostic, PdfDiagnosticSeverity, PdfDiagnost
3
3
  import { LAYOUT_FORMAT_VERSION, LayoutAnnotation, LayoutAnnotationQuad, LayoutAnnotationQuadSchema, LayoutAnnotationSchema, LayoutAttachment, LayoutAttachmentSchema, LayoutDestination, LayoutDestinationSchema, LayoutDestinationTarget, LayoutDestinationTargetSchema, LayoutDocument, LayoutDocumentSchema, LayoutEllipse, LayoutEllipseSchema, LayoutFormField, LayoutFormFieldSchema, LayoutFormWidget, LayoutFormWidgetSchema, LayoutImage, LayoutImageAsset, LayoutImageAssetSchema, LayoutImageSchema, LayoutInternalLink, LayoutInternalLinkSchema, LayoutItem, LayoutItemSchema, LayoutLayer, LayoutLayerSchema, LayoutLine, LayoutLineSchema, LayoutLink, LayoutLinkSchema, LayoutOutlineItem, LayoutOutlineItemSchema, LayoutPage, LayoutPageSchema, LayoutPath, LayoutPathSchema, LayoutPathSegment, LayoutPathSegmentSchema, LayoutRect, LayoutRectSchema, LayoutStructureElement, LayoutStructureElementSchema, LayoutSubpath, LayoutSubpathSchema, LayoutText, LayoutTextSchema } from "./layout.js";
4
4
  import { t as GlyphInkBounds } from "./glyph-bounds-BV_Z40KS.js";
5
5
  import { PdfBytesSchema, pdfCodec } from "./codec.js";
6
- import { n as EmbeddedFaceMetrics, r as EmbeddedFaceSubstitution, t as EmbeddedFace } from "./embedded-font-B4p9a3O3.js";
6
+ import { n as EmbeddedFaceMetrics, r as EmbeddedFaceSubstitution, t as EmbeddedFace } from "./embedded-font-BRhZfEN2.js";
7
7
  import { WinAnsiSubstitution } from "./winansi.js";
8
8
  import { PdfEncryptionError, PdfEncryptionOptions, PdfEncryptionPermissions, PdfEncryptionScheme } from "./encrypt-write.js";
9
9
  import { FontFace, FontFaceParseError, readFontFace } from "./font-face.js";
package/dist/layout.cjs CHANGED
@@ -178,7 +178,12 @@ const LayoutImageAssetSchema = zod.z.object({
178
178
  format: zod.z.enum(["png", "jpeg"]),
179
179
  base64: zod.z.string(),
180
180
  widthPx: zod.z.number().int().positive(),
181
- heightPx: zod.z.number().int().positive()
181
+ heightPx: zod.z.number().int().positive(),
182
+ original: zod.z.object({
183
+ filter: zod.z.enum(["jbig2", "jpeg2000"]),
184
+ base64: zod.z.string(),
185
+ jbig2GlobalsBase64: zod.z.string().optional()
186
+ }).optional()
182
187
  });
183
188
  const LayoutDestinationTargetSchema = zod.z.object({
184
189
  kind: zod.z.enum([
package/dist/layout.d.cts CHANGED
@@ -675,6 +675,14 @@ declare const LayoutImageAssetSchema: z.ZodObject<{
675
675
  base64: z.ZodString;
676
676
  widthPx: z.ZodNumber;
677
677
  heightPx: z.ZodNumber;
678
+ original: z.ZodOptional<z.ZodObject<{
679
+ filter: z.ZodEnum<{
680
+ jbig2: "jbig2";
681
+ jpeg2000: "jpeg2000";
682
+ }>;
683
+ base64: z.ZodString;
684
+ jbig2GlobalsBase64: z.ZodOptional<z.ZodString>;
685
+ }, z.core.$strip>>;
678
686
  }, z.core.$strip>;
679
687
  type LayoutImageAsset = z.infer<typeof LayoutImageAssetSchema>;
680
688
  declare const LayoutDestinationTargetSchema: z.ZodObject<{
@@ -1016,6 +1024,14 @@ declare const LayoutDocumentSchema: z.ZodObject<{
1016
1024
  base64: z.ZodString;
1017
1025
  widthPx: z.ZodNumber;
1018
1026
  heightPx: z.ZodNumber;
1027
+ original: z.ZodOptional<z.ZodObject<{
1028
+ filter: z.ZodEnum<{
1029
+ jbig2: "jbig2";
1030
+ jpeg2000: "jpeg2000";
1031
+ }>;
1032
+ base64: z.ZodString;
1033
+ jbig2GlobalsBase64: z.ZodOptional<z.ZodString>;
1034
+ }, z.core.$strip>>;
1019
1035
  }, z.core.$strip>>;
1020
1036
  destinations: z.ZodOptional<z.ZodArray<z.ZodObject<{
1021
1037
  name: z.ZodString;
package/dist/layout.d.ts CHANGED
@@ -675,6 +675,14 @@ declare const LayoutImageAssetSchema: z.ZodObject<{
675
675
  base64: z.ZodString;
676
676
  widthPx: z.ZodNumber;
677
677
  heightPx: z.ZodNumber;
678
+ original: z.ZodOptional<z.ZodObject<{
679
+ filter: z.ZodEnum<{
680
+ jbig2: "jbig2";
681
+ jpeg2000: "jpeg2000";
682
+ }>;
683
+ base64: z.ZodString;
684
+ jbig2GlobalsBase64: z.ZodOptional<z.ZodString>;
685
+ }, z.core.$strip>>;
678
686
  }, z.core.$strip>;
679
687
  type LayoutImageAsset = z.infer<typeof LayoutImageAssetSchema>;
680
688
  declare const LayoutDestinationTargetSchema: z.ZodObject<{
@@ -1016,6 +1024,14 @@ declare const LayoutDocumentSchema: z.ZodObject<{
1016
1024
  base64: z.ZodString;
1017
1025
  widthPx: z.ZodNumber;
1018
1026
  heightPx: z.ZodNumber;
1027
+ original: z.ZodOptional<z.ZodObject<{
1028
+ filter: z.ZodEnum<{
1029
+ jbig2: "jbig2";
1030
+ jpeg2000: "jpeg2000";
1031
+ }>;
1032
+ base64: z.ZodString;
1033
+ jbig2GlobalsBase64: z.ZodOptional<z.ZodString>;
1034
+ }, z.core.$strip>>;
1019
1035
  }, z.core.$strip>>;
1020
1036
  destinations: z.ZodOptional<z.ZodArray<z.ZodObject<{
1021
1037
  name: z.ZodString;
package/dist/layout.js CHANGED
@@ -177,7 +177,12 @@ const LayoutImageAssetSchema = z.object({
177
177
  format: z.enum(["png", "jpeg"]),
178
178
  base64: z.string(),
179
179
  widthPx: z.number().int().positive(),
180
- heightPx: z.number().int().positive()
180
+ heightPx: z.number().int().positive(),
181
+ original: z.object({
182
+ filter: z.enum(["jbig2", "jpeg2000"]),
183
+ base64: z.string(),
184
+ jbig2GlobalsBase64: z.string().optional()
185
+ }).optional()
181
186
  });
182
187
  const LayoutDestinationTargetSchema = z.object({
183
188
  kind: z.enum([
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
  };
@@ -14,7 +14,7 @@ function sequenceToUtf16BEHex(codePoints) {
14
14
  return hex;
15
15
  }
16
16
  function buildToUnicodeCMap(codeToSequence) {
17
- const entries = [...codeToSequence].sort((a, b) => a[0] - b[0]);
17
+ const entries = [...codeToSequence].filter(([, sequence]) => sequence.length > 0).sort((a, b) => a[0] - b[0]);
18
18
  const lines = [
19
19
  "/CIDInit /ProcSet findresource begin",
20
20
  "12 dict begin",
package/dist/tounicode.js CHANGED
@@ -13,7 +13,7 @@ function sequenceToUtf16BEHex(codePoints) {
13
13
  return hex;
14
14
  }
15
15
  function buildToUnicodeCMap(codeToSequence) {
16
- const entries = [...codeToSequence].sort((a, b) => a[0] - b[0]);
16
+ const entries = [...codeToSequence].filter(([, sequence]) => sequence.length > 0).sort((a, b) => a[0] - b[0]);
17
17
  const lines = [
18
18
  "/CIDInit /ProcSet findresource begin",
19
19
  "12 dict begin",
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
  });
@@ -414,7 +439,7 @@ function writePdf(doc, options = {}) {
414
439
  const formObjectNums = [];
415
440
  const countFieldObjects = (fields) => {
416
441
  let n = 0;
417
- for (const field of fields) n += 1 + (field.fieldType !== "group" && field.widgets.length > 1 ? field.widgets.length - 1 : 0) + countFieldObjects(field.children);
442
+ for (const field of fields) n += 1 + (field.fieldType !== "group" && field.widgets.length > 1 ? field.widgets.length : 0) + countFieldObjects(field.children);
418
443
  return n;
419
444
  };
420
445
  for (let i = 0; i < countFieldObjects(doc.form ?? []); i += 1) formObjectNums.push(nextObjNum++);
@@ -424,7 +449,7 @@ function writePdf(doc, options = {}) {
424
449
  const claimFormNums = (fields) => {
425
450
  for (const field of fields) {
426
451
  formNumByField.set(field, formObjectNums[formNumCursor++]);
427
- if (field.fieldType !== "group" && field.widgets.length > 1) formExtraWidgetNums.set(field, field.widgets.slice(1).map(() => formObjectNums[formNumCursor++]));
452
+ if (field.fieldType !== "group" && field.widgets.length > 1) formExtraWidgetNums.set(field, field.widgets.map(() => formObjectNums[formNumCursor++]));
428
453
  claimFormNums(field.children);
429
454
  }
430
455
  };
@@ -576,6 +601,12 @@ function writePdf(doc, options = {}) {
576
601
  })
577
602
  });
578
603
  }
604
+ const widgetAnnotsByPage = /* @__PURE__ */ new Map();
605
+ const noteWidgetAnnot = (widget, ref) => {
606
+ const existing = widgetAnnotsByPage.get(widget.pageIndex);
607
+ if (existing === void 0) widgetAnnotsByPage.set(widget.pageIndex, [ref]);
608
+ else existing.push(ref);
609
+ };
579
610
  const widgetRectArray = (widget) => require_objects.pdfArray([
580
611
  widget.xPt,
581
612
  widget.yPt,
@@ -624,9 +655,14 @@ function writePdf(doc, options = {}) {
624
655
  entries.push(["Subtype", require_objects.pdfName("Widget")]);
625
656
  entries.push(["Rect", widgetRectArray(firstWidget)]);
626
657
  entries.push(["P", require_objects.pdfRef(pageAllocs[firstWidget.pageIndex].pageNum, 0)]);
658
+ noteWidgetAnnot(firstWidget, require_objects.pdfRef(formNumOf(field), 0));
627
659
  } else if (field.widgets.length > 1 && firstWidget !== void 0) {
628
660
  const extraNums = formExtraWidgetNums.get(field) ?? [];
629
- entries.push(["Kids", require_objects.pdfArray([widgetDict(firstWidget), ...extraNums.map((num) => require_objects.pdfRef(num, 0))])]);
661
+ entries.push(["Kids", require_objects.pdfArray(extraNums.map((num) => require_objects.pdfRef(num, 0)))]);
662
+ for (const [index, num] of extraNums.entries()) {
663
+ const widget = field.widgets[index];
664
+ if (widget !== void 0) noteWidgetAnnot(widget, require_objects.pdfRef(num, 0));
665
+ }
630
666
  }
631
667
  }
632
668
  objects.push({
@@ -635,8 +671,8 @@ function writePdf(doc, options = {}) {
635
671
  });
636
672
  const extraNums = formExtraWidgetNums.get(field) ?? [];
637
673
  for (const [index, num] of extraNums.entries()) {
638
- const widget = field.widgets[index + 1];
639
- objects.push({
674
+ const widget = field.widgets[index];
675
+ if (widget !== void 0) objects.push({
640
676
  num,
641
677
  value: widgetDict(widget)
642
678
  });
@@ -697,6 +733,14 @@ function writePdf(doc, options = {}) {
697
733
  value: require_objects.pdfStream(alloc.prepared.alpha.dict, alloc.prepared.alpha.raw)
698
734
  });
699
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
+ }
700
744
  objects.push({
701
745
  num: alloc.imageNum,
702
746
  value: require_objects.pdfStream(alloc.prepared.dict, alloc.prepared.raw)
@@ -833,6 +877,7 @@ function writePdf(doc, options = {}) {
833
877
  });
834
878
  const annots = [...page.items.filter(isLinkItem).map((link) => buildLinkAnnotDict(link)), ...page.items.filter(isInternalLinkItem).map((link) => buildInternalLinkAnnotDict(link, doc, pageAllocs))];
835
879
  if (page.notes !== void 0 && page.notes.length > 0) annots.push(buildNotesAnnotDict(page.notes));
880
+ annots.push(...widgetAnnotsByPage.get(pageIndex) ?? []);
836
881
  const pageEntries = /* @__PURE__ */ new Map([
837
882
  ["Type", require_objects.pdfName("Page")],
838
883
  ["Parent", require_objects.pdfRef(pagesNum, 0)],
package/dist/write.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { LayoutDocument } from "./layout.cjs";
2
- import { r as EmbeddedFaceSubstitution } from "./embedded-font-oc65VGlr.cjs";
2
+ import { r as EmbeddedFaceSubstitution } from "./embedded-font-DJUn9kMH.cjs";
3
3
  import { WinAnsiSubstitution } from "./winansi.cjs";
4
4
  import { PdfEncryptionOptions } from "./encrypt-write.cjs";
5
5
  import { FontRegistry } from "./font-registry.cjs";
package/dist/write.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { LayoutDocument } from "./layout.js";
2
- import { r as EmbeddedFaceSubstitution } from "./embedded-font-B4p9a3O3.js";
2
+ import { r as EmbeddedFaceSubstitution } from "./embedded-font-BRhZfEN2.js";
3
3
  import { WinAnsiSubstitution } from "./winansi.js";
4
4
  import { PdfEncryptionOptions } from "./encrypt-write.js";
5
5
  import { FontRegistry } from "./font-registry.js";
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
  });
@@ -413,7 +438,7 @@ function writePdf(doc, options = {}) {
413
438
  const formObjectNums = [];
414
439
  const countFieldObjects = (fields) => {
415
440
  let n = 0;
416
- for (const field of fields) n += 1 + (field.fieldType !== "group" && field.widgets.length > 1 ? field.widgets.length - 1 : 0) + countFieldObjects(field.children);
441
+ for (const field of fields) n += 1 + (field.fieldType !== "group" && field.widgets.length > 1 ? field.widgets.length : 0) + countFieldObjects(field.children);
417
442
  return n;
418
443
  };
419
444
  for (let i = 0; i < countFieldObjects(doc.form ?? []); i += 1) formObjectNums.push(nextObjNum++);
@@ -423,7 +448,7 @@ function writePdf(doc, options = {}) {
423
448
  const claimFormNums = (fields) => {
424
449
  for (const field of fields) {
425
450
  formNumByField.set(field, formObjectNums[formNumCursor++]);
426
- if (field.fieldType !== "group" && field.widgets.length > 1) formExtraWidgetNums.set(field, field.widgets.slice(1).map(() => formObjectNums[formNumCursor++]));
451
+ if (field.fieldType !== "group" && field.widgets.length > 1) formExtraWidgetNums.set(field, field.widgets.map(() => formObjectNums[formNumCursor++]));
427
452
  claimFormNums(field.children);
428
453
  }
429
454
  };
@@ -575,6 +600,12 @@ function writePdf(doc, options = {}) {
575
600
  })
576
601
  });
577
602
  }
603
+ const widgetAnnotsByPage = /* @__PURE__ */ new Map();
604
+ const noteWidgetAnnot = (widget, ref) => {
605
+ const existing = widgetAnnotsByPage.get(widget.pageIndex);
606
+ if (existing === void 0) widgetAnnotsByPage.set(widget.pageIndex, [ref]);
607
+ else existing.push(ref);
608
+ };
578
609
  const widgetRectArray = (widget) => pdfArray([
579
610
  widget.xPt,
580
611
  widget.yPt,
@@ -623,9 +654,14 @@ function writePdf(doc, options = {}) {
623
654
  entries.push(["Subtype", pdfName("Widget")]);
624
655
  entries.push(["Rect", widgetRectArray(firstWidget)]);
625
656
  entries.push(["P", pdfRef(pageAllocs[firstWidget.pageIndex].pageNum, 0)]);
657
+ noteWidgetAnnot(firstWidget, pdfRef(formNumOf(field), 0));
626
658
  } else if (field.widgets.length > 1 && firstWidget !== void 0) {
627
659
  const extraNums = formExtraWidgetNums.get(field) ?? [];
628
- entries.push(["Kids", pdfArray([widgetDict(firstWidget), ...extraNums.map((num) => pdfRef(num, 0))])]);
660
+ entries.push(["Kids", pdfArray(extraNums.map((num) => pdfRef(num, 0)))]);
661
+ for (const [index, num] of extraNums.entries()) {
662
+ const widget = field.widgets[index];
663
+ if (widget !== void 0) noteWidgetAnnot(widget, pdfRef(num, 0));
664
+ }
629
665
  }
630
666
  }
631
667
  objects.push({
@@ -634,8 +670,8 @@ function writePdf(doc, options = {}) {
634
670
  });
635
671
  const extraNums = formExtraWidgetNums.get(field) ?? [];
636
672
  for (const [index, num] of extraNums.entries()) {
637
- const widget = field.widgets[index + 1];
638
- objects.push({
673
+ const widget = field.widgets[index];
674
+ if (widget !== void 0) objects.push({
639
675
  num,
640
676
  value: widgetDict(widget)
641
677
  });
@@ -696,6 +732,14 @@ function writePdf(doc, options = {}) {
696
732
  value: pdfStream(alloc.prepared.alpha.dict, alloc.prepared.alpha.raw)
697
733
  });
698
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
+ }
699
743
  objects.push({
700
744
  num: alloc.imageNum,
701
745
  value: pdfStream(alloc.prepared.dict, alloc.prepared.raw)
@@ -832,6 +876,7 @@ function writePdf(doc, options = {}) {
832
876
  });
833
877
  const annots = [...page.items.filter(isLinkItem).map((link) => buildLinkAnnotDict(link)), ...page.items.filter(isInternalLinkItem).map((link) => buildInternalLinkAnnotDict(link, doc, pageAllocs))];
834
878
  if (page.notes !== void 0 && page.notes.length > 0) annots.push(buildNotesAnnotDict(page.notes));
879
+ annots.push(...widgetAnnotsByPage.get(pageIndex) ?? []);
835
880
  const pageEntries = /* @__PURE__ */ new Map([
836
881
  ["Type", pdfName("Page")],
837
882
  ["Parent", pdfRef(pagesNum, 0)],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdf-parser.js",
3
- "version": "4.5.6",
3
+ "version": "4.7.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.9.1",
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
  },
@@ -1,10 +0,0 @@
1
- import { t as SfntFont } from "./sfnt-V08bb5ut.js";
2
- //#region src/gsub-table.d.ts
3
- interface GsubShaping {
4
- readonly glyphIds: number[];
5
- readonly spans: number[];
6
- }
7
- type GsubShaper = (glyphIds: readonly number[]) => GsubShaping;
8
- declare function buildGsubShaper(font: SfntFont): GsubShaper | undefined;
9
- //#endregion
10
- export { GsubShaping as n, buildGsubShaper as r, GsubShaper as t };
@@ -1,10 +0,0 @@
1
- import { t as SfntFont } from "./sfnt-V08bb5ut.cjs";
2
- //#region src/gsub-table.d.ts
3
- interface GsubShaping {
4
- readonly glyphIds: number[];
5
- readonly spans: number[];
6
- }
7
- type GsubShaper = (glyphIds: readonly number[]) => GsubShaping;
8
- declare function buildGsubShaper(font: SfntFont): GsubShaper | undefined;
9
- //#endregion
10
- export { GsubShaping as n, buildGsubShaper as r, GsubShaper as t };