pdf-parser.js 4.5.5 → 4.6.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/write.cjs CHANGED
@@ -414,7 +414,7 @@ function writePdf(doc, options = {}) {
414
414
  const formObjectNums = [];
415
415
  const countFieldObjects = (fields) => {
416
416
  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);
417
+ for (const field of fields) n += 1 + (field.fieldType !== "group" && field.widgets.length > 1 ? field.widgets.length : 0) + countFieldObjects(field.children);
418
418
  return n;
419
419
  };
420
420
  for (let i = 0; i < countFieldObjects(doc.form ?? []); i += 1) formObjectNums.push(nextObjNum++);
@@ -424,7 +424,7 @@ function writePdf(doc, options = {}) {
424
424
  const claimFormNums = (fields) => {
425
425
  for (const field of fields) {
426
426
  formNumByField.set(field, formObjectNums[formNumCursor++]);
427
- if (field.fieldType !== "group" && field.widgets.length > 1) formExtraWidgetNums.set(field, field.widgets.slice(1).map(() => formObjectNums[formNumCursor++]));
427
+ if (field.fieldType !== "group" && field.widgets.length > 1) formExtraWidgetNums.set(field, field.widgets.map(() => formObjectNums[formNumCursor++]));
428
428
  claimFormNums(field.children);
429
429
  }
430
430
  };
@@ -576,6 +576,12 @@ function writePdf(doc, options = {}) {
576
576
  })
577
577
  });
578
578
  }
579
+ const widgetAnnotsByPage = /* @__PURE__ */ new Map();
580
+ const noteWidgetAnnot = (widget, ref) => {
581
+ const existing = widgetAnnotsByPage.get(widget.pageIndex);
582
+ if (existing === void 0) widgetAnnotsByPage.set(widget.pageIndex, [ref]);
583
+ else existing.push(ref);
584
+ };
579
585
  const widgetRectArray = (widget) => require_objects.pdfArray([
580
586
  widget.xPt,
581
587
  widget.yPt,
@@ -624,9 +630,14 @@ function writePdf(doc, options = {}) {
624
630
  entries.push(["Subtype", require_objects.pdfName("Widget")]);
625
631
  entries.push(["Rect", widgetRectArray(firstWidget)]);
626
632
  entries.push(["P", require_objects.pdfRef(pageAllocs[firstWidget.pageIndex].pageNum, 0)]);
633
+ noteWidgetAnnot(firstWidget, require_objects.pdfRef(formNumOf(field), 0));
627
634
  } else if (field.widgets.length > 1 && firstWidget !== void 0) {
628
635
  const extraNums = formExtraWidgetNums.get(field) ?? [];
629
- entries.push(["Kids", require_objects.pdfArray([widgetDict(firstWidget), ...extraNums.map((num) => require_objects.pdfRef(num, 0))])]);
636
+ entries.push(["Kids", require_objects.pdfArray(extraNums.map((num) => require_objects.pdfRef(num, 0)))]);
637
+ for (const [index, num] of extraNums.entries()) {
638
+ const widget = field.widgets[index];
639
+ if (widget !== void 0) noteWidgetAnnot(widget, require_objects.pdfRef(num, 0));
640
+ }
630
641
  }
631
642
  }
632
643
  objects.push({
@@ -635,8 +646,8 @@ function writePdf(doc, options = {}) {
635
646
  });
636
647
  const extraNums = formExtraWidgetNums.get(field) ?? [];
637
648
  for (const [index, num] of extraNums.entries()) {
638
- const widget = field.widgets[index + 1];
639
- objects.push({
649
+ const widget = field.widgets[index];
650
+ if (widget !== void 0) objects.push({
640
651
  num,
641
652
  value: widgetDict(widget)
642
653
  });
@@ -833,6 +844,7 @@ function writePdf(doc, options = {}) {
833
844
  });
834
845
  const annots = [...page.items.filter(isLinkItem).map((link) => buildLinkAnnotDict(link)), ...page.items.filter(isInternalLinkItem).map((link) => buildInternalLinkAnnotDict(link, doc, pageAllocs))];
835
846
  if (page.notes !== void 0 && page.notes.length > 0) annots.push(buildNotesAnnotDict(page.notes));
847
+ annots.push(...widgetAnnotsByPage.get(pageIndex) ?? []);
836
848
  const pageEntries = /* @__PURE__ */ new Map([
837
849
  ["Type", require_objects.pdfName("Page")],
838
850
  ["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
@@ -413,7 +413,7 @@ function writePdf(doc, options = {}) {
413
413
  const formObjectNums = [];
414
414
  const countFieldObjects = (fields) => {
415
415
  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);
416
+ for (const field of fields) n += 1 + (field.fieldType !== "group" && field.widgets.length > 1 ? field.widgets.length : 0) + countFieldObjects(field.children);
417
417
  return n;
418
418
  };
419
419
  for (let i = 0; i < countFieldObjects(doc.form ?? []); i += 1) formObjectNums.push(nextObjNum++);
@@ -423,7 +423,7 @@ function writePdf(doc, options = {}) {
423
423
  const claimFormNums = (fields) => {
424
424
  for (const field of fields) {
425
425
  formNumByField.set(field, formObjectNums[formNumCursor++]);
426
- if (field.fieldType !== "group" && field.widgets.length > 1) formExtraWidgetNums.set(field, field.widgets.slice(1).map(() => formObjectNums[formNumCursor++]));
426
+ if (field.fieldType !== "group" && field.widgets.length > 1) formExtraWidgetNums.set(field, field.widgets.map(() => formObjectNums[formNumCursor++]));
427
427
  claimFormNums(field.children);
428
428
  }
429
429
  };
@@ -575,6 +575,12 @@ function writePdf(doc, options = {}) {
575
575
  })
576
576
  });
577
577
  }
578
+ const widgetAnnotsByPage = /* @__PURE__ */ new Map();
579
+ const noteWidgetAnnot = (widget, ref) => {
580
+ const existing = widgetAnnotsByPage.get(widget.pageIndex);
581
+ if (existing === void 0) widgetAnnotsByPage.set(widget.pageIndex, [ref]);
582
+ else existing.push(ref);
583
+ };
578
584
  const widgetRectArray = (widget) => pdfArray([
579
585
  widget.xPt,
580
586
  widget.yPt,
@@ -623,9 +629,14 @@ function writePdf(doc, options = {}) {
623
629
  entries.push(["Subtype", pdfName("Widget")]);
624
630
  entries.push(["Rect", widgetRectArray(firstWidget)]);
625
631
  entries.push(["P", pdfRef(pageAllocs[firstWidget.pageIndex].pageNum, 0)]);
632
+ noteWidgetAnnot(firstWidget, pdfRef(formNumOf(field), 0));
626
633
  } else if (field.widgets.length > 1 && firstWidget !== void 0) {
627
634
  const extraNums = formExtraWidgetNums.get(field) ?? [];
628
- entries.push(["Kids", pdfArray([widgetDict(firstWidget), ...extraNums.map((num) => pdfRef(num, 0))])]);
635
+ entries.push(["Kids", pdfArray(extraNums.map((num) => pdfRef(num, 0)))]);
636
+ for (const [index, num] of extraNums.entries()) {
637
+ const widget = field.widgets[index];
638
+ if (widget !== void 0) noteWidgetAnnot(widget, pdfRef(num, 0));
639
+ }
629
640
  }
630
641
  }
631
642
  objects.push({
@@ -634,8 +645,8 @@ function writePdf(doc, options = {}) {
634
645
  });
635
646
  const extraNums = formExtraWidgetNums.get(field) ?? [];
636
647
  for (const [index, num] of extraNums.entries()) {
637
- const widget = field.widgets[index + 1];
638
- objects.push({
648
+ const widget = field.widgets[index];
649
+ if (widget !== void 0) objects.push({
639
650
  num,
640
651
  value: widgetDict(widget)
641
652
  });
@@ -832,6 +843,7 @@ function writePdf(doc, options = {}) {
832
843
  });
833
844
  const annots = [...page.items.filter(isLinkItem).map((link) => buildLinkAnnotDict(link)), ...page.items.filter(isInternalLinkItem).map((link) => buildInternalLinkAnnotDict(link, doc, pageAllocs))];
834
845
  if (page.notes !== void 0 && page.notes.length > 0) annots.push(buildNotesAnnotDict(page.notes));
846
+ annots.push(...widgetAnnotsByPage.get(pageIndex) ?? []);
835
847
  const pageEntries = /* @__PURE__ */ new Map([
836
848
  ["Type", pdfName("Page")],
837
849
  ["Parent", pdfRef(pagesNum, 0)],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdf-parser.js",
3
- "version": "4.5.5",
3
+ "version": "4.6.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": {
@@ -73,7 +73,7 @@
73
73
  "license": "MIT",
74
74
  "dependencies": {
75
75
  "byte-codec": "1.4.2",
76
- "document-schema.js": "7.9.0",
76
+ "document-schema.js": "7.10.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 };