@arbidocs/react 0.4.60 → 0.4.61

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.
@@ -1,6 +1,7 @@
1
1
  import { isEffectiveRedaction, parseLayer } from '../../chunk-JWXIRUWN.js';
2
- import { PDFDocument, EncryptedPDFError, StandardFonts, rgb, PDFName, PDFHexString, BlendMode, degrees } from 'pdf-lib';
2
+ import { PDFDocument, StandardFonts, rgb, PDFName, PDFHexString, EncryptedPDFError, BlendMode, degrees } from 'pdf-lib';
3
3
  import { pdfjs } from 'react-pdf';
4
+ import qrcode from 'qrcode-generator';
4
5
 
5
6
  // src/annotations/freeze/coords.ts
6
7
  function normaliseRotation(angle) {
@@ -585,16 +586,18 @@ function checkPages(marks, pages) {
585
586
  function checkAborted(signal) {
586
587
  if (signal?.aborted) throw new DOMException("Freeze aborted", "AbortError");
587
588
  }
588
- async function freezeAsVector(original, marks, comments, now, warnings) {
589
- let doc;
589
+ async function loadPdfDocument(bytes) {
590
590
  try {
591
- doc = await PDFDocument.load(original);
591
+ return await PDFDocument.load(bytes);
592
592
  } catch (error) {
593
593
  if (error instanceof EncryptedPDFError || /is encrypted/i.test(error?.message ?? "")) {
594
594
  throw new FreezeError("encrypted", ENCRYPTED_MESSAGE);
595
595
  }
596
596
  throw new FreezeError("invalid-pdf", `The PDF could not be read: ${error.message}`);
597
597
  }
598
+ }
599
+ async function freezeAsVector(original, marks, comments, now, warnings) {
600
+ const doc = await loadPdfDocument(original);
598
601
  const pages = doc.getPages();
599
602
  checkPages(marks, pages.length);
600
603
  const ctx = await createVectorContext(doc, marks, comments);
@@ -732,7 +735,267 @@ async function freezePdfInBrowser(bytes, layer, { assets, password, ...options }
732
735
  }) : void 0;
733
736
  return freezePdf(bytes, layer, { ...options, rasterizer });
734
737
  }
738
+ var EDGE_MARGIN_PT = 18;
739
+ var BATES_FONT_SIZE_PT = 9;
740
+ var QR_SIZE_PT = 56;
741
+ var QR_QUIET_MODULES = 2;
742
+ var PLATE_PADDING_PT = 2;
743
+ function batesLabel(options, index) {
744
+ const digits = options.digits ?? 6;
745
+ const number = String(Math.floor((options.start ?? 1) + index)).padStart(digits, "0");
746
+ return `${options.prefix ?? ""}${number}`;
747
+ }
748
+ function validateBates(options) {
749
+ const { start = 1, digits = 6 } = options;
750
+ if (!Number.isInteger(start) || start < 0) {
751
+ throw new FreezeError("invalid-option", "Bates start must be a whole number, 0 or more");
752
+ }
753
+ if (!Number.isInteger(digits) || digits < 1 || digits > 12) {
754
+ throw new FreezeError("invalid-option", "Bates digits must be a whole number from 1 to 12");
755
+ }
756
+ }
757
+ function plateOrigin(g, corner, w, h) {
758
+ const size = displayedSize(g);
759
+ return {
760
+ x: corner.endsWith("left") ? EDGE_MARGIN_PT : size.width - EDGE_MARGIN_PT - w,
761
+ y: corner.startsWith("top") ? EDGE_MARGIN_PT : size.height - EDGE_MARGIN_PT - h
762
+ };
763
+ }
764
+ function drawPlate(page, g, box) {
765
+ const size = displayedSize(g);
766
+ const u = normToUserRect(g, {
767
+ x: box.x / size.width,
768
+ y: box.y / size.height,
769
+ w: box.w / size.width,
770
+ h: box.h / size.height
771
+ });
772
+ page.drawRectangle({ x: u.x, y: u.y, width: u.width, height: u.height, color: rgb(1, 1, 1) });
773
+ }
774
+ function drawBatesLabel(page, label, font, corner) {
775
+ const g = pageGeometry(page);
776
+ const textWidth = font.widthOfTextAtSize(label, BATES_FONT_SIZE_PT);
777
+ const w = textWidth + PLATE_PADDING_PT * 2;
778
+ const h = BATES_FONT_SIZE_PT + PLATE_PADDING_PT * 2;
779
+ const { x, y } = plateOrigin(g, corner, w, h);
780
+ drawPlate(page, g, { x, y, w, h });
781
+ const origin = displayedToUser(
782
+ g,
783
+ x + PLATE_PADDING_PT,
784
+ y + PLATE_PADDING_PT + BATES_FONT_SIZE_PT * 0.8
785
+ );
786
+ page.drawText(label, {
787
+ x: origin.x,
788
+ y: origin.y,
789
+ size: BATES_FONT_SIZE_PT,
790
+ font,
791
+ color: rgb(0, 0, 0),
792
+ rotate: degrees(g.rotation)
793
+ });
794
+ }
795
+ function buildQr(text) {
796
+ try {
797
+ const qr = qrcode(0, "M");
798
+ qr.addData(text, "Byte");
799
+ qr.make();
800
+ return { modules: qr.getModuleCount(), isDark: (r, c) => qr.isDark(r, c) };
801
+ } catch (error) {
802
+ throw new FreezeError(
803
+ "invalid-option",
804
+ `The QR link could not be encoded: ${error.message}`
805
+ );
806
+ }
807
+ }
808
+ function drawQr(page, qr, corner) {
809
+ const g = pageGeometry(page);
810
+ const total = qr.modules + QR_QUIET_MODULES * 2;
811
+ const module = QR_SIZE_PT / total;
812
+ const { x, y } = plateOrigin(g, corner, QR_SIZE_PT, QR_SIZE_PT);
813
+ drawPlate(page, g, { x, y, w: QR_SIZE_PT, h: QR_SIZE_PT });
814
+ const size = displayedSize(g);
815
+ for (let row = 0; row < qr.modules; row++) {
816
+ let col = 0;
817
+ while (col < qr.modules) {
818
+ if (!qr.isDark(row, col)) {
819
+ col++;
820
+ continue;
821
+ }
822
+ const startCol = col;
823
+ while (col < qr.modules && qr.isDark(row, col)) col++;
824
+ const u = normToUserRect(g, {
825
+ x: (x + (QR_QUIET_MODULES + startCol) * module) / size.width,
826
+ y: (y + (QR_QUIET_MODULES + row) * module) / size.height,
827
+ w: (col - startCol) * module / size.width,
828
+ h: module / size.height
829
+ });
830
+ page.drawRectangle({
831
+ x: u.x,
832
+ y: u.y,
833
+ width: u.width,
834
+ height: u.height,
835
+ color: rgb(0, 0, 0)
836
+ });
837
+ }
838
+ }
839
+ }
840
+
841
+ // src/annotations/freeze/bundle.ts
842
+ var BUNDLE_KEY = "ArbiBundle";
843
+ var DEFAULT_QR_CORNER = "bottom-left";
844
+ var DEFAULT_BATES_CORNER = "bottom-right";
845
+ function checkAborted2(signal) {
846
+ if (signal?.aborted) throw new DOMException("Bundle aborted", "AbortError");
847
+ }
848
+ function outputName(name, used) {
849
+ const base = (name.replace(/\.pdf$/i, "").replace(/[\\/:*?"<>|]+/g, "_").trim() || "document") + ".pdf";
850
+ let candidate = base;
851
+ for (let n = 2; used.has(candidate); n++) candidate = base.replace(/\.pdf$/, `-${n}.pdf`);
852
+ used.add(candidate);
853
+ return candidate;
854
+ }
855
+ function validate(items, options) {
856
+ if (items.length === 0)
857
+ throw new FreezeError("invalid-option", "A bundle needs at least one document");
858
+ validateBates(options.bates);
859
+ if (options.qr) {
860
+ const missing = items.find((i) => !i.qrUrl);
861
+ if (missing) {
862
+ throw new FreezeError("invalid-option", `"${missing.name}" has no link for its QR code`);
863
+ }
864
+ }
865
+ const corner = options.bates.corner ?? DEFAULT_BATES_CORNER;
866
+ if (options.qr && (options.qrCorner ?? DEFAULT_QR_CORNER) === corner) {
867
+ throw new FreezeError(
868
+ "invalid-option",
869
+ "The Bates label and the QR code need different corners"
870
+ );
871
+ }
872
+ }
873
+ async function stampPages(doc, items, firstIndex, options) {
874
+ const font = await doc.embedFont(StandardFonts.Helvetica);
875
+ const sanitiser = new TextSanitiser(font);
876
+ const label = batesLabel(options.bates, firstIndex);
877
+ if (!sanitiser.supports(label)) {
878
+ throw new FreezeError(
879
+ "unsupported-text",
880
+ `Bates prefix "${options.bates.prefix}" uses characters the built-in font cannot draw`
881
+ );
882
+ }
883
+ const pages = doc.getPages();
884
+ const corner = options.bates.corner ?? DEFAULT_BATES_CORNER;
885
+ let at = 0;
886
+ for (const item of items) {
887
+ const qr = options.qr && item.qrUrl ? buildQr(item.qrUrl) : void 0;
888
+ for (let p = 0; p < item.pages; p++, at++) {
889
+ const page = pages[at];
890
+ drawBatesLabel(page, batesLabel(options.bates, firstIndex + at), font, corner);
891
+ if (qr) drawQr(page, qr, options.qrCorner ?? DEFAULT_QR_CORNER);
892
+ }
893
+ }
894
+ }
895
+ function setBundleRecord(doc, record) {
896
+ const info = doc.context.lookup(doc.context.trailerInfo.Info);
897
+ if (!info || !("set" in info)) return;
898
+ info.set(
899
+ PDFName.of(BUNDLE_KEY),
900
+ PDFHexString.fromText(JSON.stringify(record))
901
+ );
902
+ }
903
+ async function buildBundle(items, options) {
904
+ validate(items, options);
905
+ const now = options.now ?? (/* @__PURE__ */ new Date()).toISOString();
906
+ const used = /* @__PURE__ */ new Set();
907
+ const files = [];
908
+ const index = [];
909
+ const loaded = [];
910
+ for (const item of items) {
911
+ checkAborted2(options.signal);
912
+ const doc = await loadPdfDocument(item.bytes);
913
+ loaded.push({ item, doc, pages: doc.getPageCount() });
914
+ }
915
+ if (options.layout === "merged") {
916
+ const out = await PDFDocument.create();
917
+ out.setProducer("ARBI");
918
+ out.setCreator("ARBI");
919
+ out.setCreationDate(new Date(now));
920
+ out.setModificationDate(new Date(now));
921
+ const provenance = {};
922
+ for (const { item, doc } of loaded) {
923
+ checkAborted2(options.signal);
924
+ for (const page of await out.copyPages(doc, doc.getPageIndices())) out.addPage(page);
925
+ provenance[item.documentId] = await readProvenance(item.bytes).catch(() => null);
926
+ }
927
+ await stampPages(
928
+ out,
929
+ loaded.map(({ item, pages }) => ({ pages, qrUrl: item.qrUrl })),
930
+ 0,
931
+ options
932
+ );
933
+ const file = outputName(options.mergedName ?? "production.pdf", used);
934
+ let first2 = 0;
935
+ for (const { item, pages } of loaded) {
936
+ index.push({
937
+ documentId: item.documentId,
938
+ name: item.name,
939
+ file,
940
+ pages,
941
+ firstLabel: batesLabel(options.bates, first2),
942
+ lastLabel: batesLabel(options.bates, first2 + pages - 1)
943
+ });
944
+ first2 += pages;
945
+ }
946
+ setBundleRecord(out, {
947
+ schema: 1,
948
+ producedAt: now,
949
+ ...options.author ? { producedBy: options.author } : {},
950
+ layout: "merged",
951
+ items: index.map((e) => ({
952
+ documentId: e.documentId,
953
+ name: e.name,
954
+ pages: e.pages,
955
+ firstLabel: e.firstLabel,
956
+ lastLabel: e.lastLabel,
957
+ frozen: provenance[e.documentId] ?? null
958
+ }))
959
+ });
960
+ const total = first2;
961
+ files.push({
962
+ name: file,
963
+ bytes: await out.save(),
964
+ pages: total,
965
+ firstLabel: batesLabel(options.bates, 0),
966
+ lastLabel: batesLabel(options.bates, total - 1)
967
+ });
968
+ options.onProgress?.(loaded.length, loaded.length);
969
+ return { files, index };
970
+ }
971
+ let first = 0;
972
+ for (const [n, { item, doc, pages }] of loaded.entries()) {
973
+ checkAborted2(options.signal);
974
+ await stampPages(doc, [{ pages, qrUrl: item.qrUrl }], first, options);
975
+ doc.setModificationDate(new Date(now));
976
+ const file = outputName(item.name, used);
977
+ const entry = {
978
+ documentId: item.documentId,
979
+ name: item.name,
980
+ file,
981
+ pages,
982
+ firstLabel: batesLabel(options.bates, first),
983
+ lastLabel: batesLabel(options.bates, first + pages - 1)
984
+ };
985
+ index.push(entry);
986
+ files.push({
987
+ name: file,
988
+ bytes: await doc.save(),
989
+ pages,
990
+ firstLabel: entry.firstLabel,
991
+ lastLabel: entry.lastLabel
992
+ });
993
+ first += pages;
994
+ options.onProgress?.(n + 1, loaded.length);
995
+ }
996
+ return { files, index };
997
+ }
735
998
 
736
- export { DEFAULT_DPI, FreezeError, MAX_PAGE_PIXELS, browserCanvasFactory, burnMarks, createPdfJsRasterizer, freezePdf, freezePdfInBrowser, readProvenance };
999
+ export { DEFAULT_DPI, FreezeError, MAX_PAGE_PIXELS, batesLabel, browserCanvasFactory, buildBundle, burnMarks, createPdfJsRasterizer, freezePdf, freezePdfInBrowser, readProvenance };
737
1000
  //# sourceMappingURL=index.js.map
738
1001
  //# sourceMappingURL=index.js.map