@pdfme/schemas 6.1.13-dev.37 → 6.1.13-dev.46

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.
@@ -16,7 +16,7 @@ export type BarcodeRenderRuntime = 'document-canvas' | 'offscreencanvas' | 'node
16
16
  * OffscreenCanvas without `toCanvas()` (Node bwip-js export) falls back to `toBuffer()`.
17
17
  */
18
18
  export declare const resolveBarcodeRenderRuntime: () => BarcodeRenderRuntime;
19
- export declare const createBarCode: (arg: {
19
+ type CreateBarCodeArg = {
20
20
  type: BarcodeTypes;
21
21
  input: string;
22
22
  width: number;
@@ -25,4 +25,7 @@ export declare const createBarCode: (arg: {
25
25
  barColor?: string;
26
26
  textColor?: string;
27
27
  includetext?: boolean;
28
- }) => Promise<Buffer>;
28
+ };
29
+ export declare const createBarCodeSvg: (arg: CreateBarCodeArg) => string;
30
+ export declare const createBarCode: (arg: CreateBarCodeArg) => Promise<Buffer>;
31
+ export {};
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ import "./tables.js";
9
9
  import "./lists.js";
10
10
  import { DEFAULT_FONT_NAME, ZOOM, b64toUint8Array, getDefaultFont, getFallbackFontName, getInternalLinkTarget, mm2pt, normalizeLinkHref, px2mm } from "@pdfme/common";
11
11
  import { Buffer as Buffer$1 } from "buffer";
12
+ import { popGraphicsState, pushGraphicsState, rotateDegrees, translate } from "@pdfme/pdf-lib";
12
13
  import { Barcode, Calendar, CalendarClock, ChevronDown, Circle, CircleDot, Clock, Image, List, Minus, QrCode, Route, Square, SquareCheck, Table, Type } from "lucide";
13
14
  import jpegJsModule from "jpeg-js";
14
15
  import UPNGModule from "@pdf-lib/upng";
@@ -2198,12 +2199,13 @@ var svgSchema = {
2198
2199
  }
2199
2200
  },
2200
2201
  pdf: async (arg) => {
2201
- const { page, pdfDoc, options, schema, value, _cache } = arg;
2202
+ const { page, pdfDoc, pdfLib, options, schema, value, _cache } = arg;
2202
2203
  if (!value || !isValidSVG(value)) return;
2203
2204
  const pageHeight = page.getHeight();
2204
2205
  const { width, height, position } = convertForPdfLayoutProps({
2205
2206
  schema,
2206
- pageHeight
2207
+ pageHeight,
2208
+ applyRotateTranslate: false
2207
2209
  });
2208
2210
  const { x, y } = position;
2209
2211
  const font = options.font;
@@ -2218,14 +2220,24 @@ var svgSchema = {
2218
2220
  color: rgbColorToCmykColor(parsed.rgb),
2219
2221
  alpha: parsed.alpha
2220
2222
  }) : void 0;
2221
- await page.drawSvg(value, {
2222
- x,
2223
- y: y + height,
2224
- width,
2225
- height,
2226
- fonts,
2227
- mapColor
2228
- });
2223
+ const rotateDegrees = schema.rotate ? -schema.rotate : 0;
2224
+ if (rotateDegrees) {
2225
+ const pivotX = x + width / 2;
2226
+ const pivotY = y + height / 2;
2227
+ page.pushOperators(pdfLib.pushGraphicsState(), pdfLib.translate(pivotX, pivotY), pdfLib.rotateDegrees(rotateDegrees), pdfLib.translate(-pivotX, -pivotY));
2228
+ }
2229
+ try {
2230
+ await page.drawSvg(value, {
2231
+ x,
2232
+ y: y + height,
2233
+ width,
2234
+ height,
2235
+ fonts,
2236
+ mapColor
2237
+ });
2238
+ } finally {
2239
+ if (rotateDegrees) page.pushOperators(pdfLib.popGraphicsState());
2240
+ }
2229
2241
  },
2230
2242
  propPanel: {
2231
2243
  schema: {},
@@ -2242,7 +2254,8 @@ var svgSchema = {
2242
2254
  y: 0
2243
2255
  },
2244
2256
  width: 40,
2245
- height: 50
2257
+ height: 50,
2258
+ rotate: 0
2246
2259
  }
2247
2260
  },
2248
2261
  icon: createSvgStr(Route)
@@ -2355,7 +2368,7 @@ var renderBarcodeToNodeBuffer = async (options) => {
2355
2368
  if (typeof toBuffer !== "function") throw new Error("[@pdfme/schemas] bwip-js toBuffer() is not available in this environment.");
2356
2369
  return toBuffer(options);
2357
2370
  };
2358
- var createBarCode = async (arg) => {
2371
+ var createBwipJsRenderOptions = (arg) => {
2359
2372
  const { type, input, width, height, backgroundColor, barColor, textColor, includetext = true } = arg;
2360
2373
  const bwipjsArg = {
2361
2374
  bcid: barCodeType2Bcid(type),
@@ -2369,6 +2382,18 @@ var createBarCode = async (arg) => {
2369
2382
  if (backgroundColor) bwipjsArg.backgroundcolor = mapHexColorForBwipJsLib(backgroundColor);
2370
2383
  if (barColor) bwipjsArg.barcolor = mapHexColorForBwipJsLib(barColor);
2371
2384
  if (textColor) bwipjsArg.textcolor = mapHexColorForBwipJsLib(textColor);
2385
+ return bwipjsArg;
2386
+ };
2387
+ var createBarCodeSvg = (arg) => {
2388
+ const toSVG = getBwipJsRenderer().toSVG;
2389
+ if (typeof toSVG !== "function") throw new Error("[@pdfme/schemas] bwip-js toSVG() is not available in this environment.");
2390
+ const defaultFill = `#${mapHexColorForBwipJsLib(arg.barColor)}`;
2391
+ const svg = toSVG(createBwipJsRenderOptions(arg)).replace(/<svg\b(?![^>]*\sfill=)/, `<svg fill="${defaultFill}"`);
2392
+ if (/<image\b/i.test(svg)) throw new Error(`[@pdfme/schemas] bwip-js emitted an embedded image for ${arg.type}; vector barcode PDF rendering requires path-based SVG.`);
2393
+ return svg;
2394
+ };
2395
+ var createBarCode = async (arg) => {
2396
+ const bwipjsArg = createBwipJsRenderOptions(arg);
2372
2397
  const runtime = resolveBarcodeRenderRuntime();
2373
2398
  if (runtime === "document-canvas") return renderBarcodeToDocumentCanvas(bwipjsArg);
2374
2399
  if (runtime === "offscreencanvas") return renderBarcodeToOffscreenCanvas(bwipjsArg);
@@ -2377,35 +2402,53 @@ var createBarCode = async (arg) => {
2377
2402
  //#endregion
2378
2403
  //#region src/barcodes/pdfRender.ts
2379
2404
  var getBarcodeCacheKey = (schema, value) => {
2380
- return `${schema.type}${schema.backgroundColor}${schema.barColor}${schema.textColor}${value}${schema.includetext}`;
2405
+ return `svg:${schema.type}:${schema.width}:${schema.height}:${schema.backgroundColor}:${schema.barColor}:${schema.textColor}:${schema.includetext}:${value}`;
2406
+ };
2407
+ var addSvgOpacity = (svg, opacity) => {
2408
+ return opacity === void 0 ? svg : svg.replace(/<svg\b/, `<svg opacity="${opacity}"`);
2409
+ };
2410
+ var getSvgColorMapper = (colorType = "") => {
2411
+ return colorType.toLowerCase() === "cmyk" ? ({ parsed }) => ({
2412
+ color: rgbColorToCmykColor(parsed.rgb),
2413
+ alpha: parsed.alpha
2414
+ }) : void 0;
2381
2415
  };
2382
2416
  var pdfRender$1 = async (arg) => {
2383
- const { value, schema, pdfDoc, page, _cache } = arg;
2417
+ const { value, schema, page, options, _cache } = arg;
2384
2418
  if (!validateBarcodeInput(schema.type, value)) return;
2385
2419
  const inputBarcodeCacheKey = getBarcodeCacheKey(schema, value);
2386
- let image = _cache.get(inputBarcodeCacheKey);
2387
- if (!image) {
2388
- const imageBuf = await createBarCode({
2420
+ let svg = _cache.get(inputBarcodeCacheKey);
2421
+ if (!svg) {
2422
+ svg = createBarCodeSvg({
2389
2423
  ...schema,
2390
2424
  type: schema.type,
2391
2425
  input: value
2392
2426
  });
2393
- image = await pdfDoc.embedPng(imageBuf);
2394
- _cache.set(inputBarcodeCacheKey, image);
2427
+ _cache.set(inputBarcodeCacheKey, svg);
2395
2428
  }
2396
2429
  const pageHeight = page.getHeight();
2397
- const { width, height, rotate, position: { x, y }, opacity } = convertForPdfLayoutProps({
2430
+ const { width, height, position: { x, y }, opacity } = convertForPdfLayoutProps({
2398
2431
  schema,
2399
- pageHeight
2400
- });
2401
- page.drawImage(image, {
2402
- x,
2403
- y,
2404
- rotate,
2405
- width,
2406
- height,
2407
- opacity
2432
+ pageHeight,
2433
+ applyRotateTranslate: false
2408
2434
  });
2435
+ const pivot = {
2436
+ x: x + width / 2,
2437
+ y: y + height / 2
2438
+ };
2439
+ const rotate = schema.rotate ? -schema.rotate : 0;
2440
+ if (rotate) page.pushOperators(pushGraphicsState(), translate(pivot.x, pivot.y), rotateDegrees(rotate), translate(-pivot.x, -pivot.y));
2441
+ try {
2442
+ await page.drawSvg(addSvgOpacity(svg, opacity), {
2443
+ x,
2444
+ y: y + height,
2445
+ width,
2446
+ height,
2447
+ mapColor: getSvgColorMapper(options.colorType)
2448
+ });
2449
+ } finally {
2450
+ if (rotate) page.pushOperators(popGraphicsState());
2451
+ }
2409
2452
  };
2410
2453
  //#endregion
2411
2454
  //#region src/barcodes/propPanel.ts