@pdfme/schemas 6.1.13-dev.22 → 6.1.13-dev.24

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.
@@ -9,6 +9,13 @@ export declare const barCodeType2Bcid: (type: BarcodeTypes) => "code128" | "code
9
9
  * Strip hash from the beginning of HTML hex color codes for the bwip.js lib
10
10
  */
11
11
  export declare const mapHexColorForBwipJsLib: (color: string | undefined, fallback?: string) => string;
12
+ export type BarcodeRenderRuntime = 'document-canvas' | 'offscreencanvas' | 'node-buffer';
13
+ /**
14
+ * Pick a barcode renderer from capabilities, not `typeof window`.
15
+ * Browser Workers have neither `window` nor Node's `bwip-js.toBuffer()`.
16
+ * OffscreenCanvas without `toCanvas()` (Node bwip-js export) falls back to `toBuffer()`.
17
+ */
18
+ export declare const resolveBarcodeRenderRuntime: () => BarcodeRenderRuntime;
12
19
  export declare const createBarCode: (arg: {
13
20
  type: BarcodeTypes;
14
21
  input: string;
package/dist/index.js CHANGED
@@ -2024,6 +2024,46 @@ var barCodeType2Bcid = (type) => type === "nw7" ? "rationalizedCodabar" : type;
2024
2024
  * Strip hash from the beginning of HTML hex color codes for the bwip.js lib
2025
2025
  */
2026
2026
  var mapHexColorForBwipJsLib = (color, fallback) => color ? color.replace("#", "") : fallback ? fallback.replace("#", "") : "000000";
2027
+ var getBwipJsRenderer = () => bwipjs;
2028
+ var getDocument = () => globalThis.document;
2029
+ var getOffscreenCanvasCtor = () => globalThis.OffscreenCanvas;
2030
+ /**
2031
+ * Pick a barcode renderer from capabilities, not `typeof window`.
2032
+ * Browser Workers have neither `window` nor Node's `bwip-js.toBuffer()`.
2033
+ * OffscreenCanvas without `toCanvas()` (Node bwip-js export) falls back to `toBuffer()`.
2034
+ */
2035
+ var resolveBarcodeRenderRuntime = () => {
2036
+ const doc = getDocument();
2037
+ const renderer = getBwipJsRenderer();
2038
+ if (doc && typeof doc.createElement === "function") return "document-canvas";
2039
+ if (typeof getOffscreenCanvasCtor() === "function" && typeof renderer.toCanvas === "function") return "offscreencanvas";
2040
+ if (typeof renderer.toBuffer === "function") return "node-buffer";
2041
+ throw new Error("[@pdfme/schemas] Barcode rendering requires a document canvas, OffscreenCanvas, or bwip-js toBuffer().");
2042
+ };
2043
+ var pngBufferFromDataUrl = (dataUrl) => Buffer$1.from(b64toUint8Array(dataUrl).buffer);
2044
+ var pngBufferFromBlob = async (blob) => Buffer$1.from(await blob.arrayBuffer());
2045
+ var renderBarcodeToCanvas = (canvas, options) => {
2046
+ const toCanvas = getBwipJsRenderer().toCanvas;
2047
+ if (typeof toCanvas !== "function") throw new Error("[@pdfme/schemas] bwip-js toCanvas() is not available in this environment.");
2048
+ toCanvas(canvas, options);
2049
+ };
2050
+ var renderBarcodeToDocumentCanvas = (options) => {
2051
+ const canvas = getDocument().createElement("canvas");
2052
+ renderBarcodeToCanvas(canvas, options);
2053
+ return pngBufferFromDataUrl(canvas.toDataURL("image/png"));
2054
+ };
2055
+ var renderBarcodeToOffscreenCanvas = async (options) => {
2056
+ const OffscreenCanvasCtor = getOffscreenCanvasCtor();
2057
+ if (typeof OffscreenCanvasCtor !== "function") throw new Error("[@pdfme/schemas] OffscreenCanvas is not available in this environment.");
2058
+ const canvas = new OffscreenCanvasCtor(1, 1);
2059
+ renderBarcodeToCanvas(canvas, options);
2060
+ return pngBufferFromBlob(await canvas.convertToBlob({ type: "image/png" }));
2061
+ };
2062
+ var renderBarcodeToNodeBuffer = async (options) => {
2063
+ const toBuffer = getBwipJsRenderer().toBuffer;
2064
+ if (typeof toBuffer !== "function") throw new Error("[@pdfme/schemas] bwip-js toBuffer() is not available in this environment.");
2065
+ return toBuffer(options);
2066
+ };
2027
2067
  var createBarCode = async (arg) => {
2028
2068
  const { type, input, width, height, backgroundColor, barColor, textColor, includetext = true } = arg;
2029
2069
  const bwipjsArg = {
@@ -2038,14 +2078,10 @@ var createBarCode = async (arg) => {
2038
2078
  if (backgroundColor) bwipjsArg.backgroundcolor = mapHexColorForBwipJsLib(backgroundColor);
2039
2079
  if (barColor) bwipjsArg.barcolor = mapHexColorForBwipJsLib(barColor);
2040
2080
  if (textColor) bwipjsArg.textcolor = mapHexColorForBwipJsLib(textColor);
2041
- let res;
2042
- if (typeof window !== "undefined") {
2043
- const canvas = document.createElement("canvas");
2044
- bwipjs.toCanvas(canvas, bwipjsArg);
2045
- const dataUrl = canvas.toDataURL("image/png");
2046
- res = Buffer$1.from(b64toUint8Array(dataUrl).buffer);
2047
- } else res = await bwipjs.toBuffer(bwipjsArg);
2048
- return res;
2081
+ const runtime = resolveBarcodeRenderRuntime();
2082
+ if (runtime === "document-canvas") return renderBarcodeToDocumentCanvas(bwipjsArg);
2083
+ if (runtime === "offscreencanvas") return renderBarcodeToOffscreenCanvas(bwipjsArg);
2084
+ return renderBarcodeToNodeBuffer(bwipjsArg);
2049
2085
  };
2050
2086
  //#endregion
2051
2087
  //#region src/barcodes/pdfRender.ts