Vue套用hiprint插件实现打印分页

在这里插入图片描述
一、Vue下依赖包

npm i easy-print

二、代码

1.引入:
import hiprint from 'easy-print'//引入hiprint
import panel from './panel.js'//引入打印样式

2.data里声明模板变量:
hiprintTemplate: new hiprint.PrintTemplate({ template: panel }),

3.在打印按钮调用方法:
hiprintContent(){
    //给模板字段赋值
    var printData = {
        supp: this.print_data.supp,//表头-供应商
        order: this.print_data.order,//表头-单号
        address: this.print_data.SUPPADDR,//表头-地址
        sendDate: this.print_data.date,//表头-送货日期
        tell: this.print_data.SUPPTEL,//表头-电话
		qrcode:this.print_data.order,//表头-二维码
        detailList:this.print_tatle_data,//表格数据
    };
	this.hiprintTemplate.print(printData);
},

三、panel.js

const panel = {
  panels: [
    {'height': 140,//纸张的高
      'width': 215,//纸张的宽
      'paperHeader': 115,//页头的高度,调整高度可设置分页的页眉内容
      'paperFooter': 330,//页尾的高度,调整高度可设置分页的页尾内容
      'printElements': [{'options': {'left': 262.5, 'top': 22.5, 'height': 9.75, 'width': 82.5, 'title': '送货单', 'fontSize': 19.5, 'fontWeight': 600, 'color': '#000'},
        'printElementType': {'type': 'text'}}, {'options': {'left': 19.5, 'top': 60, 'height': 9.75, 'width': 300, 'title': '供应商', 'field': 'supp', 'fontSize': 14, 'color': '#000'},
        'printElementType': {'type': 'text'}}, {'options': {'left': 300, 'top': 60, 'height': 9.75, 'width': 200, 'title': '单号', 'field': 'order', 'fontSize': 14, 'color': '#000'},
        'printElementType': {'type': 'text'}}, {'options': {'left': 19.5, 'top': 80, 'height': 9.75, 'width': 400, 'title': '地址', 'field': 'address', 'fontSize': 14, 'color': '#000'},
        'printElementType': {'type': 'text'}}, {'options': {'left': 19.5, 'top': 100, 'height': 9.75, 'width': 200, 'title': '送货日期', 'field': 'sendDate', 'fontSize': 14, 'color': '#000'},
        'printElementType': {'type': 'text'}}, {'options': {'left': 300, 'top': 100, 'height': 9.75, 'width': 200, 'title': '电话', 'field': 'tell', 'fontSize': 14, 'color': '#000'},
        'printElementType': {'type': 'text'}}, {'options': {'left': 470, 'top': 50, 'height': 60, 'width': 60, 'field': 'qrcode', 'textType': 'qrcode'},
        'printElementType': {'type': 'text'}}, 
		//到这里为止是页眉的内容,下面是表格会自动分页
		{'options': {'left': 19.5,
        'top': 120,//表格高度
        'height': 39,
        'width': 549,
        'textAlign': 'center',
        'field': 'detailList',//表格数据
        'columns': [[//表格列
          {'title': '序号', 'field': 'SHOWSERIAL', 'width': 28.675373869095594, 'colspan': 1, 'rowspan': 1, 'checked': true, 'columnId': 'SHOWSERIAL'},
          {'title': '产品代码', 'field': 'PROD', 'width': 83.85182567889859, 'colspan': 1, 'rowspan': 1, 'checked': true, 'columnId': 'PROD'},
          {'title': '品名', 'field': 'NAME', 'width': 109.20163436367484, 'colspan': 1, 'rowspan': 1, 'checked': true, 'columnId': 'NAME'},
          {'title': '规格', 'field': 'SPEC', 'width': 54.34142950693378, 'colspan': 1, 'rowspan': 1, 'checked': true},
          {'title': '送货数量', 'field': 'QTY', 'width': 44.082548951480675, 'colspan': 1, 'rowspan': 1, 'checked': true, 'columnId': 'QTY'},
          {'title': '单位', 'field': 'UNITNAME', 'width': 43.366939065981505, 'colspan': 1, 'rowspan': 1, 'checked': true, 'columnId': 'UNITNAME'},
          {'title': '含税单价', 'field': 'PRICETAX', 'width': 43.06711654562698, 'colspan': 1, 'rowspan': 1, 'checked': true, 'columnId': 'PRICETAX'},
          {'title': '合计', 'field': 'ALL', 'width': 45.989780649905455, 'colspan': 1, 'rowspan': 1, 'checked': true, 'columnId': 'ALL'},
          {'title': '备注', 'field': 'REMARK', 'width': 43.878609887169446, 'colspan': 1, 'rowspan': 1, 'checked': true, 'columnId': 'REMARK'}
        ]]
      },
      'printElementType': {'title': '表格', 'type': 'tableCustom'}}, 
      {'options': {'left': 19.5, 'top': 330, 'height': 9.75, 'width': 270, 'title': '购货单位:金华有限公司', 'fontSize': 13, 'color': '#000'},'printElementType': {'type': 'text'}}, 
      {'options': {'left': 220, 'top': 330, 'height': 9.75, 'width': 250, 'title': '地址:浙江省金华市', 'fontSize': 13, 'color': '#000'},'printElementType': {'type': 'text'}},
      {'options': {'left': 450, 'top': 330, 'height': 9.75, 'width': 250, 'title': '电话:12345', 'fontSize': 13, 'color': '#000'},'printElementType': {'type': 'text'}},
      {'options': {'left': 19.5, 'top': 350, 'height': 9.75, 'width': 250, 'title': '购货单位签收:', 'fontSize': 13, 'color': '#000'}, 'printElementType': {'title': '自定义文本', 'type': 'text'}
      }],
      'paperNumberLeft': 565.5,
      'paperNumberTop': 380,//页码位置
      'paperNumberFormat': 'paperNo/paperCount'}
  ]
}
export default panel

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
要使用vue-hiprint完成标签打印功能,可以按照以下步骤进行操作: 1. 安装vue-hiprint插件 可以通过npm安装vue-hiprint插件,命令如下: ```npm install vue-hiprint``` 2. 引入vue-hiprint插件Vue项目的main.js中引入vue-hiprint插件,代码如下: ``` import Vue from 'vue' import HiPrint from 'vue-hiprint' Vue.use(HiPrint) ``` 3. 编写标签模板 创建标签模板,可以使用HTML和CSS编写模板。例如,以下是一个2×4的标签模板: ``` <div class="label-wrapper"> <div class="label"> <div class="name">Name 1</div> <div class="address">Address 1</div> </div> <div class="label"> <div class="name">Name 2</div> <div class="address">Address 2</div> </div> <div class="label"> <div class="name">Name 3</div> <div class="address">Address 3</div> </div> <div class="label"> <div class="name">Name 4</div> <div class="address">Address 4</div> </div> </div> <style> .label-wrapper { display: flex; flex-wrap: wrap; } .label { width: 50%; height: 50%; border: 1px solid #000; padding: 10px; } .name { font-weight: bold; } .address { margin-top: 10px; } </style> ``` 4. 使用vue-hiprint组件 在Vue组件中使用vue-hiprint组件,并传入标签模板,代码如下: ``` <template> <hi-print ref="print" :debug="true"> <div class="label-wrapper"> <div class="label"> <div class="name">Name 1</div> <div class="address">Address 1</div> </div> <div class="label"> <div class="name">Name 2</div> <div class="address">Address 2</div> </div> <div class="label"> <div class="name">Name 3</div> <div class="address">Address 3</div> </div> <div class="label"> <div class="name">Name 4</div> <div class="address">Address 4</div> </div> </div> </hi-print> </template> ``` 5. 调用打印方法 在Vue组件中调用vue-hiprint打印方法,并指定打印的标签大小和数量,代码如下: ``` this.$refs.print.print({ mediaSize: { width: '2.5in', height: '1.5in' }, copies: 8 }) ``` 这样就可以完成使用vue-hiprint插件实现标签打印功能了。在调用打印方法时,可以根据需要自定义标签大小和打印的数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值