@signiphi/page-assembly 0.2.0-beta.14 → 0.2.0-beta.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.
package/dist/index.mjs CHANGED
@@ -14,7 +14,7 @@ import * as AlertDialogPrimitive from '@radix-ui/react-alert-dialog';
14
14
  import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu';
15
15
  import * as TooltipPrimitive from '@radix-ui/react-tooltip';
16
16
  import * as pdfjsLib from 'pdfjs-dist';
17
- import { PDFDocument, PDFName, PDFTextField, PDFCheckBox, PDFDropdown, PDFRadioGroup, PDFHexString, StandardFonts, rgb, PDFString, PDFDict, PDFArray, PDFInvalidObject, PDFObjectParser, PDFRawStream, PDFObjectStreamParser, PDFRef, PDFBool, PDFNumber } from 'pdf-lib';
17
+ import { PDFDocument, PDFName, PDFTextField, PDFCheckBox, PDFDropdown, PDFRadioGroup, PDFHexString, StandardFonts, rgb, PDFString, PDFOperator, PDFOperatorNames, PDFDict, PDFArray, PDFInvalidObject, PDFObjectParser, PDFRawStream, PDFObjectStreamParser, PDFRef, PDFBool, PDFNumber } from 'pdf-lib';
18
18
 
19
19
  // src/components/PageAssembler.tsx
20
20
  function cn(...inputs) {
@@ -1386,6 +1386,7 @@ function mergeSourceMetadata(sources) {
1386
1386
  const merged = { version: "1.0", fields: {}, fieldIdIndex: {} };
1387
1387
  let radioLabelsBaked;
1388
1388
  let textLabelsBaked;
1389
+ let checkboxLabelsBaked;
1389
1390
  for (const source of sources) {
1390
1391
  const meta = source.metadata;
1391
1392
  if (!meta || source.fields.length === 0) continue;
@@ -1393,6 +1394,8 @@ function mergeSourceMetadata(sources) {
1393
1394
  else if (meta.radioLabelsBaked === true && radioLabelsBaked === void 0) radioLabelsBaked = true;
1394
1395
  if (meta.textLabelsBaked === false) textLabelsBaked = false;
1395
1396
  else if (meta.textLabelsBaked === true && textLabelsBaked === void 0) textLabelsBaked = true;
1397
+ if (meta.checkboxLabelsBaked === false) checkboxLabelsBaked = false;
1398
+ else if (meta.checkboxLabelsBaked === true && checkboxLabelsBaked === void 0) checkboxLabelsBaked = true;
1396
1399
  }
1397
1400
  for (const source of sources) {
1398
1401
  const meta = source.metadata;
@@ -1411,8 +1414,9 @@ function mergeSourceMetadata(sources) {
1411
1414
  }
1412
1415
  if (radioLabelsBaked !== void 0) merged.radioLabelsBaked = radioLabelsBaked;
1413
1416
  if (textLabelsBaked !== void 0) merged.textLabelsBaked = textLabelsBaked;
1417
+ if (checkboxLabelsBaked !== void 0) merged.checkboxLabelsBaked = checkboxLabelsBaked;
1414
1418
  const hasEntries = Object.keys(merged.fields).length > 0;
1415
- const hasMarkers = radioLabelsBaked !== void 0 || textLabelsBaked !== void 0;
1419
+ const hasMarkers = radioLabelsBaked !== void 0 || textLabelsBaked !== void 0 || checkboxLabelsBaked !== void 0;
1416
1420
  if (!hasEntries && !hasMarkers) return null;
1417
1421
  if (Object.keys(merged.fieldIdIndex).length === 0) delete merged.fieldIdIndex;
1418
1422
  return merged;
@@ -1641,6 +1645,7 @@ function setSigniphiMetadata(pdfDoc, metadata) {
1641
1645
  throw error;
1642
1646
  }
1643
1647
  }
1648
+ var SIGNIPHI_CHECKBOX_LABEL_MARKER = "SiPL_Check";
1644
1649
  var FormFieldType = /* @__PURE__ */ ((FormFieldType2) => {
1645
1650
  FormFieldType2["TEXT"] = "text";
1646
1651
  FormFieldType2["SIGNATURE"] = "signature";
@@ -1726,6 +1731,7 @@ async function addFormFieldsToPdf(pdfBytes, formFields, options = {}) {
1726
1731
  }
1727
1732
  }
1728
1733
  const font = await pdfDoc.embedFont(StandardFonts.Helvetica);
1734
+ const bakedCheckboxCaptions = /* @__PURE__ */ new Map();
1729
1735
  const fieldNamePages = /* @__PURE__ */ new Map();
1730
1736
  for (const field of formFields) {
1731
1737
  const baseName = field.name;
@@ -1822,10 +1828,15 @@ async function addFormFieldsToPdf(pdfBytes, formFields, options = {}) {
1822
1828
  } catch {
1823
1829
  }
1824
1830
  if (drawLabels) {
1825
- const fontSize = Math.min(12, field.position.height * 0.6);
1831
+ const fontSize = Math.max(1, field.position.height * 0.4);
1826
1832
  const labelX = pdfX + field.position.width + 5;
1827
1833
  const labelY = pdfY + (field.position.height - fontSize) / 2;
1834
+ page.pushOperators(
1835
+ PDFOperator.of(PDFOperatorNames.BeginMarkedContent, [PDFName.of(SIGNIPHI_CHECKBOX_LABEL_MARKER)])
1836
+ );
1828
1837
  page.drawText(field.label, { x: labelX, y: labelY, size: fontSize, font, color: rgb(0, 0, 0) });
1838
+ page.pushOperators(PDFOperator.of(PDFOperatorNames.EndMarkedContent));
1839
+ bakedCheckboxCaptions.set(encodedName, field);
1829
1840
  }
1830
1841
  }
1831
1842
  break;
@@ -1977,8 +1988,28 @@ async function addFormFieldsToPdf(pdfBytes, formFields, options = {}) {
1977
1988
  console.error(`Error adding field ${field.name}:`, error);
1978
1989
  }
1979
1990
  }
1991
+ recordBakedCheckboxCaptions(pdfDoc, bakedCheckboxCaptions);
1980
1992
  return await pdfDoc.save();
1981
1993
  }
1994
+ function recordBakedCheckboxCaptions(pdfDoc, captions) {
1995
+ if (captions.size === 0) return;
1996
+ try {
1997
+ const metadata = getSigniphiMetadata(pdfDoc);
1998
+ if (!metadata) return;
1999
+ metadata.fields ?? (metadata.fields = {});
2000
+ for (const [name, field] of captions) {
2001
+ metadata.fields[name] = {
2002
+ signer: field.assignedSignerEmail,
2003
+ label: field.label,
2004
+ ...metadata.fields[name],
2005
+ checkboxCaptionBaked: true
2006
+ };
2007
+ }
2008
+ setSigniphiMetadata(pdfDoc, metadata);
2009
+ } catch (error) {
2010
+ console.warn("Failed to record baked checkbox captions:", error);
2011
+ }
2012
+ }
1982
2013
  function mapFieldPositionsAfterAssembly(fields, pageMapping) {
1983
2014
  return fields.map((field) => {
1984
2015
  const mappingKey = String(field.position.page - 1);