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