template-designer 0.10.7 → 0.10.8

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.
Files changed (2) hide show
  1. package/README.md +161 -33
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -289,13 +289,20 @@ const config = {
289
289
 
290
290
  ### 2、当前模板中控件的旋转中心为控件的中心,每个组件的数据都包含x1,y1,为左上角旋转之后的实时坐标,旋转角度为顺时针
291
291
 
292
- ## 渲染导出工具:renderTemplateToPdfBase64
292
+ ### 字段替换规则
293
293
 
294
- 提供一个独立工具,将“模板数据 + 打印数据”渲染为 PDF 或 PNG,并返回 base64 或 dataURL
294
+ - 适用组件:text、barcode、qrcode。
295
+ - FieldName 支持 string 或数组:
296
+ - string:从 `printData[key]` 取值
297
+ - array:从 `printData` 逐个取值,使用组件 `FieldConnector` 连接(默认空格)
298
+ - 覆盖策略:导出前始终用打印数据覆盖组件测试值:
299
+ - text → 覆盖 `Text`
300
+ - barcode → 覆盖 `BarcodeValue`
301
+ - qrcode → 覆盖 `QRCodeValue`
295
302
 
296
- ## renderTemplateToHtml
303
+ ## 渲染导出工具:renderTemplateToPdfBase64
297
304
 
298
- 提供一个独立工具,将“模板数据 + 打印数据”渲染为 html
305
+ 提供一个独立工具,将“模板数据 + 打印数据”渲染为 PDF 或 PNG,并返回 base64 或 dataURL
299
306
 
300
307
  ### 导入
301
308
 
@@ -324,17 +331,6 @@ async function renderTemplateToPdfBase64(
324
331
  ): Promise<string>
325
332
  ```
326
333
 
327
- ### 字段替换规则
328
-
329
- - 适用组件:text、barcode、qrcode。
330
- - FieldName 支持 string 或数组:
331
- - string:从 `printData[key]` 取值
332
- - array:从 `printData` 逐个取值,使用组件 `FieldConnector` 连接(默认空格)
333
- - 覆盖策略:导出前始终用打印数据覆盖组件测试值:
334
- - text → 覆盖 `Text`(并拼接 `Title`)
335
- - barcode → 覆盖 `BarcodeValue`
336
- - qrcode → 覆盖 `QRCodeValue`
337
-
338
334
  ### 基础用法(导出 PDF,返回 base64)
339
335
 
340
336
  ```js
@@ -351,29 +347,161 @@ const pdfBase64 = await renderTemplateToPdfBase64(template, printData, {
351
347
  })
352
348
  ```
353
349
 
354
- ### 在浏览器中预览与下载 PDF
350
+ ### 体积优化建议
351
+
352
+ - 不需要透明背景时,`transparent=false` + `quality≈0.8` + `scale≈2`,可显著减小体积仍保持清晰。
353
+ - 需要极致清晰时提高 `scale`;若体积过大,适当降低 `quality`。
354
+
355
+ ## 条形码和二维码生成工具
356
+
357
+ 提供独立的条形码和二维码生成函数,可在组件外部使用。
358
+
359
+ ### generateBarcode - 条形码生成
360
+
361
+ #### 导入
355
362
 
356
363
  ```js
357
- // 预览(推荐使用 blob URL,兼容性更好)
358
- const dataUrl = await renderTemplateToPdfBase64(template, printData, { returnType: 'baseUrl' })
359
- const base64 = dataUrl.split(',')[1]
360
- const blob = new Blob([Uint8Array.from(atob(base64), c => c.charCodeAt(0))], { type: 'application/pdf' })
361
- const blobUrl = URL.createObjectURL(blob)
362
- window.open(blobUrl)
363
-
364
- // 下载
365
- const a = document.createElement('a')
366
- a.href = blobUrl
367
- a.download = 'template.pdf'
368
- document.body.appendChild(a)
369
- a.click()
370
- document.body.removeChild(a)
364
+ import { generateBarcode } from 'template-designer'
371
365
  ```
372
366
 
373
- ### 体积优化建议
367
+ #### 函数签名
374
368
 
375
- - 不需要透明背景时,`transparent=false` + `quality≈0.8` + `scale≈2`,可显著减小体积仍保持清晰。
376
- - 需要极致清晰时提高 `scale`;若体积过大,适当降低 `quality`。
369
+ ```ts
370
+ async function generateBarcode(
371
+ text: string, // 要编码的文本内容
372
+ options?: {
373
+ CodeType?: string // 条形码格式,可选'CODE128'|'CODE39'|'EAN13'|'EAN8'|'UPC'|'ITF14'|'MSI'|'pharmacode'|'codabar',默认'CODE128'
374
+ width?: number // 条形码宽度(毫米),默认50
375
+ height?: number // 条形码高度(毫米),默认15
376
+ LineColor?: string // 条形码颜色,默认'#000000'
377
+ background?: string // 背景色,默认'#ffffff'
378
+ displayValue?: boolean // 是否显示文本,默认false
379
+ textAlign?: string // 文本对齐,可选'left'|'center'|'right',默认'center'
380
+ textPosition?: string // 文本位置,可选'bottom'|'top',默认'bottom'
381
+ textMargin?: number // 文本边距(毫米),默认2
382
+ font?: string // 字体,默认'monospace'
383
+ fontSize?: number // 字体大小(毫米),默认5
384
+ outputFormat?: string // 输出格式,可选'png'|'jpeg'|'svg',默认'png'
385
+ quality?: number // 图片质量(0-1),仅对jpeg有效,默认0.95
386
+ returnDataUrl?: boolean // 是否返回完整dataUrl,默认true
387
+ dpi?: number // DPI值,默认300
388
+ }
389
+ ): Promise<string> // 返回base64格式的图片数据或dataUrl
390
+ ```
391
+
392
+ #### 使用示例
393
+
394
+ ```js
395
+ // 基础用法
396
+ const barcodeDataUrl = await generateBarcode('1234567890', {
397
+ CodeType: 'CODE128',
398
+ width: 50,
399
+ height: 15,
400
+ displayValue: true
401
+ })
402
+
403
+ // 使用返回的dataUrl
404
+ const img = document.createElement('img')
405
+ img.src = barcodeDataUrl
406
+ document.body.appendChild(img)
407
+
408
+ // 自定义样式
409
+ const customBarcode = await generateBarcode('ABC123', {
410
+ CodeType: 'CODE39',
411
+ width: 60,
412
+ height: 20,
413
+ LineColor: '#FF0000',
414
+ background: '#FFFF00',
415
+ displayValue: true,
416
+ textAlign: 'center',
417
+ fontSize: 6,
418
+ outputFormat: 'png',
419
+ dpi: 300
420
+ })
421
+ ```
422
+
423
+ #### 支持的条形码格式
424
+
425
+ - `CODE128` - 支持ASCII字符,最大80个字符(默认)
426
+ - `CODE39` - 支持大写字母、数字、特殊字符,最大43个字符
427
+ - `EAN13` - 13位数字,商品条码
428
+ - `EAN8` - 8位数字,商品条码
429
+ - `UPC` - 12位数字,商品条码
430
+ - `ITF14` - 14位数字,物流条码
431
+ - `MSI` - 数字,最大20位
432
+ - `pharmacode` - 数字,最大6位,药品条码
433
+ - `codabar` - 支持数字、字母、特殊字符,最大20个字符
434
+
435
+ ### generateQRCode - 二维码生成
436
+
437
+ #### 导入
438
+
439
+ ```js
440
+ import { generateQRCode } from 'template-designer'
441
+ ```
442
+
443
+ #### 函数签名
444
+
445
+ ```ts
446
+ async function generateQRCode(
447
+ text: string, // 要编码的文本内容
448
+ options?: {
449
+ width?: number // 二维码宽度(毫米),默认50
450
+ height?: number // 二维码高度(毫米),默认50
451
+ QRCodeForeground?: string // 前景色(二维码颜色),默认'#000000'
452
+ QRCodeBackground?: string // 背景色,默认'#ffffff'
453
+ QRCodeErrorLevel?: string // 错误纠正级别,可选'L'|'M'|'Q'|'H',默认'M'
454
+ margin?: number // 边距(毫米),默认0
455
+ format?: string // 输出格式,可选'png'|'jpeg'|'svg',默认'png'
456
+ quality?: number // 图片质量(0-1),仅对jpeg有效,默认0.9
457
+ returnDataUrl?: boolean // 是否返回完整dataUrl,默认true
458
+ dpi?: number // DPI值,默认96
459
+ }
460
+ ): Promise<string> // 返回base64格式的图片数据或dataUrl
461
+ ```
462
+
463
+ #### 使用示例
464
+
465
+ ```js
466
+ // 基础用法
467
+ const qrcodeDataUrl = await generateQRCode('https://example.com', {
468
+ width: 50,
469
+ height: 50,
470
+ QRCodeErrorLevel: 'M'
471
+ })
472
+
473
+ // 使用返回的dataUrl
474
+ const img = document.createElement('img')
475
+ img.src = qrcodeDataUrl
476
+ document.body.appendChild(img)
477
+
478
+ // 自定义样式
479
+ const customQRCode = await generateQRCode('Hello World', {
480
+ width: 60,
481
+ height: 60,
482
+ QRCodeForeground: '#0000FF',
483
+ QRCodeBackground: '#FFFFFF',
484
+ QRCodeErrorLevel: 'H', // 高错误纠正级别
485
+ margin: 2,
486
+ format: 'png',
487
+ dpi: 300
488
+ })
489
+
490
+ // 生成SVG格式
491
+ const svgQRCode = await generateQRCode('SVG格式二维码', {
492
+ width: 50,
493
+ height: 50,
494
+ format: 'svg',
495
+ returnDataUrl: true
496
+ })
497
+ ```
498
+
499
+ #### 错误纠正级别说明
500
+
501
+ - `L` - 低(约7%的错误可被纠正)
502
+ - `M` - 中(约15%的错误可被纠正,默认)
503
+ - `Q` - 四分之一(约25%的错误可被纠正)
504
+ - `H` - 高(约30%的错误可被纠正)
377
505
 
378
506
  ## 开发
379
507
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "template-designer",
3
- "version": "0.10.7",
3
+ "version": "0.10.8",
4
4
  "description": "模板拖拽设计器组件库",
5
5
  "main": "dist/template-designer.umd.js",
6
6
  "module": "dist/template-designer.esm.js",