vue3简单使用打印插件vue-plugin-hiprint

9 篇文章 0 订阅

插件git地址:https://github.com/CcSimple/vue-plugin-hiprint

安装

npm install vue-plugin-hiprint
在public里面新建一个print-lock.css,用来存放相关css代码
css代码来源https://cdn.jsdelivr.net/npm/vue-plugin-hiprint@latest/dist/print-lock.css
然后在index.html里面加入如下代码(href后面跟上的是上面新建的css文件地址)

<link rel="stylesheet" type="text/css" media="print" href="<%= BASE_URL %>print-lock.css">

使用插件

<template>
	<div>
			<el-button @click="print('test')">测试打印</el-button>
	</div>
</template>
<script setup>
//定义一些打印相关的常量 可以通用
//打印左右边距
const printMinLeft = 5
//打印A4宽度px
const a4Width = 595
//A4的panel设置
const a4Panel = {width: 210, height: 297, paperFooter: 340, paperHeader: 10}


const headerColNum = 3//总列数
const titleHeight = 20//标题行高
const contentWidth = a4Width - printMinLeft * 2//内容宽度


//定义两个新建打印文本和打印表格列的方法,方便复用代码
//新字段元素:列索引,列数,行索引(从1开始) 中文名 字段名
const newPrintText = (col, colNum, row, title, field) => {
    return {
        options: {
            width: contentWidth / headerColNum * colNum,
            height: 20,
            fontSize: 10,
            top: titleHeight + 20 * row,
            left: contentWidth / headerColNum * (col - 1) + printMinLeft,
            type: 'text',
            title: title,
            field: field,
        }
    }
}
// 新列元素:中文名,字段名。相对宽度(建议1~10)
const newPrintColumn = (title, field, width) => {
    return {
        title: title,
        field: field,
        width: width,
        colspan: 1,
        rowspan: 1,
        align: 'center',
    }
}



import {autoConnect, disAutoConnect, hiprint, defaultElementTypeProvider} from 'vue-plugin-hiprint'
disAutoConnect()// 取消自动连接直接打印客户端
hiprint.init()//初始化
const print=title=>{
	//定义需要打印的数据
	let printData={
		formNo:'F0001',
		operator:'张三',
		addDate:'2023-01-01',
		details:[]
	}
	for(let i=0;i<5;i++){
		printData.details.push(
		{
			productNo:'P000'+i,
			productName:'测试产品'+i,
			num:0,
			unit:'个'
		})
	}
    //定义模板和面板,所有打印元素都在面板上,默认A4纸大小	
	let hiprintTemplate = new hiprint.PrintTemplate();
    let panel = hiprintTemplate.addPrintPanel(a4Panel);
    //文本标题
    panel.addPrintText({
        options: {
            width: 150,
            height: 20,
            top: 10,
            left: contentWidth / 2 - 30,
            fontSize: 20,
            title: '测试单据',
            type: 'text'
        }
    })
    //头部内容
    panel.addPrintText(newPrintText(1, 1, 1, '表单编号', 'formNo'))
    panel.addPrintText(newPrintText(2, 1, 1, '操作人', 'operator'))
    panel.addPrintText(newPrintText(3, 1, 1, '操作日期', 'addDate'))
    let tmpColumns = []
    tmpColumns.push(newPrintColumn('产品编号', 'productNo', 3))
    tmpColumns.push(newPrintColumn('产品名称', 'productName', 5))
    tmpColumns.push(newPrintColumn('数量', 'num', 1))
    tmpColumns.push(newPrintColumn('单位', 'unit', 1))
    panel.addPrintTable({
        options: {
            width: a4Width - printMinLeft * 2,
            top: 80,
            left: printMinLeft,
            field: 'details',
            columns: [tmpColumns]
        }
    })
//打印
    hiprintTemplate.print(printData);
}
</script>

title属性一般都是默认文本
field属性一般都是绑定数据的字段名称

如果需要打印条形码或者二维码,也是用addPrintText方法,只是options的参数textType使用“barcode”(条形码)或者“qrcode”(二维码)即可。
如果需要打印html则可以使用addPrintHtml方法。
其他特殊打印形状参考http://hiprint.io/docs/text

简单的自定义打印,也可以通过存储元素的top和left属性,确认元素位置,实现简单的自定义打印。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用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插件实现标签打印功能了。在调用打印方法时,可以根据需要自定义标签大小和打印的数量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值