当前位置:   article > 正文

vue 打印(模板套打)_print-template

print-template

vue 使用  print-template 生成pdf 打印 。支持生成二维码、条形码、文本、图片、线 

安装

  1. yarn add print-template
  2. npm install print-template

内容类型

line线条
barcode条形码
qrcode二维码
image图片
text文本

内容属性

通用

x必须 所有类型 
y必须 所有类型 
default固定内容默认值

线条 【line】

length线条长度number
orientation线条方向   p 竖线 / l 横线string 

文本 【text】

fontSize字体大小   number
fontWeight字体宽度number

fontFamily

字体类型  默认 微软雅黑string

maxWidth

最大宽度 换行使用(目前英文和数字计算有问题) number

 图片 【image】

width图片宽度number

height

图片高度number

条形码 【brcode】

format

条形码格式  CODE128A/B/C .......string
width生成线条的宽度 内容越多 宽度约大 生成的条形码约长number
height 高度   条形码加字体高度number
margin条形码白边边距number

displayValue

是否显示文字boolean

fontSize

字体大小number

fontOptions

字体样式  粗体  bold / 斜体 italic /  粗斜体 bold  italicstring

 二维码【brcode】

 

width二维码宽度number

使用

 

  1. import printTemplate from 'print-template'
  2. // 创建打印模板
  3. let template = new printTemplate()
  4. // 模板数据
  5. let yto= {
  6. name:'yto' // 模板名称
  7. unit:'mm', // 尺寸 默认mm mm / px
  8. size: [76, 130], // 模板大小 宽 76mm /130mm
  9. fixed:[ // 固定内容 比如:线条 、logo 广告、固定字体
  10. // 个人觉得 制作一个透明的底图 不需要一条线一条线设置
  11. { type: 'line', x: 2, y: 12, length: 72 },
  12. { type: 'line', x: 2, y: 12, orientation: 'p', length: 116 },
  13. { type: 'line', x: 74, y: 12, orientation: 'p', length: 116 },
  14. { type: 'line', x: 2, y: 27, length: 72 },
  15. { type: 'line', x: 2, y: 35, length: 72 },
  16. { type: 'line', x: 2, y: 41, length: 52 },
  17. { type: 'line', x: 54, y: 35, orientation: 'p', length: 32 },
  18. { type: 'line', x: 54, y: 49, length: 20 },
  19. { type: 'line', x: 2, y: 59, length: 72 },
  20. { type: 'line', x: 2, y: 67, length: 72 },
  21. { type: 'line', x: 2, y: 77, length: 72 },
  22. { type: 'line', x: 2, y: 110, length: 72 },
  23. { type: 'line', x: 2, y: 128, length: 72 },
  24. { type: 'text', fontSize: 3.8, fontWeight: 700, x: 66, y: 2, default: '货到\n付款' }, // 固定文字 \n 换行 也可以设置 maxWidth:3.8*2 自动换行
  25. ],
  26. data:{ // 动态数据
  27. name: { type: 'text', x: 8, y: 45, fontSize: 3.5 },
  28. code: { type: 'barcode', x: 7, y: 13, format: 'CODE128A', width: 4, margin: 0, fontSize: 3.3, fontOptions: 'bold', displayValue: true, height: 13 },
  29. }
  30. }
  31. // 添加模板
  32. template.push(yto)
  33. // 传入数据
  34. let data = [{name:'张三1',code:'YT100011111'},{name:'张三2',code:'YT100011112'}]
  35. // 打印
  36. template.print('yto', data).then(pdf=>{
  37. // 返回 jspdf
  38. // blob 地址
  39. let uri = pdf.output('bloburi', { filename: '打印文件' });
  40. // 下载保存
  41. pdf.save( '打印文件' );
  42. })

 

生成的文件截图

 

问题

最近有人私信问我一些问题,在这里回复一下

1、底图

 模板中 logo 图标 ,提示文字,边框线或二维码等在固定内容可以合成一张透明图片 在 fixed 中只需设置一次图片类型内容即可

 

注意:width  和 height 必须是数字类型

  1. {
  2. size: [76, 130], //模板大小
  3. fixed: [
  4. {
  5. type: 'image', // 内容类型 图片
  6. width: 76, // 图片宽度一般和模板宽度相同 数字类型
  7. height: 130, // 图片高度一般和模板高度度相同 数字类型
  8. default:'/files/bg.png' // 图片地址 不支持跨域
  9. },
  10. ]
  11. }

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/113084
推荐阅读
相关标签
  

闽ICP备14008679号