template-designer 0.5.0 → 0.5.2
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.
- package/README.md +83 -1
- package/dist/index-6bec0af6.mjs +23327 -0
- package/dist/index.es-71cdb4be.mjs +5771 -0
- package/dist/{lodop-0993f903.mjs → lodop-d02120ea.mjs} +9 -0
- package/dist/purify.es-35a15df8.mjs +480 -0
- package/dist/{rfid-2e73e7ce.mjs → rfid-0c29f3f0.mjs} +9 -0
- package/dist/style.css +1 -1
- package/dist/template-designer.es.js +4 -14269
- package/dist/template-designer.umd.js +278 -17
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -262,7 +262,89 @@ const config = {
|
|
|
262
262
|
|
|
263
263
|
### 1、控件中的字段(FieldName)只选中一个值为字符串,如果选中多个则为字符串数组,使用时注意兼容
|
|
264
264
|
|
|
265
|
-
### 2、当前模板中控件的旋转中心为控件的中心,每个组件的数据都包含x1,y1
|
|
265
|
+
### 2、当前模板中控件的旋转中心为控件的中心,每个组件的数据都包含x1,y1,为左上角旋转之后的实时坐标,旋转角度为顺时针
|
|
266
|
+
|
|
267
|
+
## 渲染导出工具:renderTemplateToPdfBase64
|
|
268
|
+
|
|
269
|
+
提供一个独立工具,将“模板数据 + 打印数据”渲染为 PDF 或 PNG,并返回 base64 或 dataURL
|
|
270
|
+
|
|
271
|
+
### 导入
|
|
272
|
+
|
|
273
|
+
```js
|
|
274
|
+
import { renderTemplateToPdfBase64 } from 'template-designer'
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
### 函数签名
|
|
278
|
+
|
|
279
|
+
```ts
|
|
280
|
+
async function renderTemplateToPdfBase64(
|
|
281
|
+
template: object, // 模板数据(同设计器使用的数据结构)
|
|
282
|
+
printData: Record<string, any>, // 打印数据(key -> value)
|
|
283
|
+
options?: {
|
|
284
|
+
page?: { widthMm?: number; heightMm?: number; marginMm?: number } // (可选)默认打印宽高
|
|
285
|
+
returnType?: 'base64' | 'dataUrl' // (可选)默认 'base64'
|
|
286
|
+
format?: 'pdf' | 'png' // (可选)默认 'pdf';png 可保留透明背景
|
|
287
|
+
pdf?: {
|
|
288
|
+
orientation?: 'portrait' | 'landscape' // (可选)默认自动
|
|
289
|
+
transparent?: boolean // (可选)是否保留透明背景(仅对 pdf 内嵌图像生效)
|
|
290
|
+
quality?: number // (可选)0~1(jpeg质量,透明=false 时生效,默认 0.85)
|
|
291
|
+
scale?: number // (可选)html2canvas 放大倍数,建议 1.5~2.5
|
|
292
|
+
compress?: boolean // (可选)jsPDF 压缩,默认 true
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
): Promise<string>
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
### 字段替换规则
|
|
299
|
+
|
|
300
|
+
- 适用组件:text、barcode、qrcode。
|
|
301
|
+
- FieldName 支持 string 或数组:
|
|
302
|
+
- string:从 `printData[key]` 取值
|
|
303
|
+
- array:从 `printData` 逐个取值,使用组件 `FieldConnector` 连接(默认空格)
|
|
304
|
+
- 覆盖策略:导出前始终用打印数据覆盖组件测试值:
|
|
305
|
+
- text → 覆盖 `Text`(并拼接 `Title`)
|
|
306
|
+
- barcode → 覆盖 `BarcodeValue`
|
|
307
|
+
- qrcode → 覆盖 `QRCodeValue`
|
|
308
|
+
|
|
309
|
+
### 基础用法(导出 PDF,返回 base64)
|
|
310
|
+
|
|
311
|
+
```js
|
|
312
|
+
const pdfBase64 = await renderTemplateToPdfBase64(template, printData, {
|
|
313
|
+
page: { widthMm: 103, heightMm: 40 },
|
|
314
|
+
returnType: 'base64',
|
|
315
|
+
pdf: {
|
|
316
|
+
orientation: 'landscape',
|
|
317
|
+
transparent: false, // 若不需要透明,可关闭以减小体积
|
|
318
|
+
quality: 0.8, // 仅非透明时生效(jpeg质量)
|
|
319
|
+
scale: 2, // 1.5~2.5 间调节清晰度与体积
|
|
320
|
+
compress: true
|
|
321
|
+
}
|
|
322
|
+
})
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### 在浏览器中预览与下载 PDF
|
|
326
|
+
|
|
327
|
+
```js
|
|
328
|
+
// 预览(推荐使用 blob URL,兼容性更好)
|
|
329
|
+
const dataUrl = await renderTemplateToPdfBase64(template, printData, { returnType: 'baseUrl' })
|
|
330
|
+
const base64 = dataUrl.split(',')[1]
|
|
331
|
+
const blob = new Blob([Uint8Array.from(atob(base64), c => c.charCodeAt(0))], { type: 'application/pdf' })
|
|
332
|
+
const blobUrl = URL.createObjectURL(blob)
|
|
333
|
+
window.open(blobUrl)
|
|
334
|
+
|
|
335
|
+
// 下载
|
|
336
|
+
const a = document.createElement('a')
|
|
337
|
+
a.href = blobUrl
|
|
338
|
+
a.download = 'template.pdf'
|
|
339
|
+
document.body.appendChild(a)
|
|
340
|
+
a.click()
|
|
341
|
+
document.body.removeChild(a)
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
### 体积优化建议
|
|
345
|
+
|
|
346
|
+
- 不需要透明背景时,`transparent=false` + `quality≈0.8` + `scale≈2`,可显著减小体积仍保持清晰。
|
|
347
|
+
- 需要极致清晰时提高 `scale`;若体积过大,适当降低 `quality`。
|
|
266
348
|
|
|
267
349
|
## 开发
|
|
268
350
|
|