前端实现 生成条形码并调用打印机打印

本文档使用开发语言vue3+vite+setup语法糖

实现功能,批量选择数据-->生成条形码--->调用打印机-->打印输出

一、生成条形码:  

1.安装所需要插件

npm i jsbarcode

2. 引入

import JsBarcode from 'jsbarcode'

3. 使用 

 // html 部分
<svg ref="barcodeRef"></svg>
 
// js部分
 
import{ref, onMounted} from 'vue'
import JsBarcode from 'jsbarcode'
 
const barcodeRef=ref(null)
const text = '123456789'
const options= {
    // format: 'EAN13', // 格式
    height: 50,
    // text: "覆盖显示的文本",
    fontSize: 16,
    // background: '#ccc',
     lineColor: 'black'
}
JsBarcode(barcodeRef.value, text , options)

具体相关条形码配置options,请参考jsbarcode - npm

二,调用打印机打印条形码

 1.安装所需要插件

 npm i vue-print-nb

2. 引入

main.js文件

import print from 'vue3-print-nb'
 
const app = createApp(App)
app.use(print)
...
app.mount('#app')

3. 使用 

 // html 部分
 <div ref="printTest" id="printTest">
    <div> 这里就是你所要打印的内用</div>
    <el-button v-print="printObj">打印</el-button>
  </div>
// js部分
 
import{ref, onMounted} from 'vue'
 
const printObj = reactive({
  id: 'printTest', // 绑定打印区域的id
  beforeOpenCallback(vue) {
    vue.printLoading = true
    console.log('打开之前')
  },
  openCallback(vue) {
    vue.printLoading = false
    console.log('执行了打印')
  },
  closeCallback(vue) {
    console.log('关闭了打印工具')
  }
})

具体相关条形码配置printObj,请参考vue-print-nb - npm

在打印预览的时候发现,条形码并不是一码一页,以及存在打印预览中会看到在纸张的上下页眉和页脚中有日期及其他的文字内用,该怎么去掉;

别着急!!!

 // 1.实现一码一页打印
 <div ref="printTest" id="printTest">
    <div> 
        这里就是你所要打印的内用
 
         <!-- 换页打印 -->
        <p style="page-break-after: always"></p>
    </div>
    <el-button v-print="printObj">打印</el-button>
  </div>
 
 
// 去掉页眉和页脚
通过媒体查询,css来解决
<style lang="less">
@media print {
  @page {
    size: auto; /* 重置页面大小,避免出现空白页 */
    margin-top: 0; /* 取消页眉 */
    margin-bottom: 0; /* 取消页脚 */
    margin-left:0; margin-right:0;  /* 取消默认的左右页边距 */
  }
}
</style>

三,完整代码实现批量打印条形码

<template>
  <div>
    <div id="printTest">
      <div v-for="(item, index) in data" :key="index">
        <svg ref="barcodeList"></svg>
        <!-- 换页打印 -->
        <p style="page-break-after: always"></p>
      </div>
    </div>
    <el-button v-print="printObj">打印</el-button>
  </div>
</template>
 
<script setup>
import JsBarcode from 'jsbarcode'
import { ref, reactive, onMounted } from 'vue'
 
const printObj = reactive({
  id: 'printTest', // 绑定打印区域的id
  beforeOpenCallback(vue) {
    vue.printLoading = true
    console.log('打开之前')
  },
  openCallback(vue) {
    vue.printLoading = false
    console.log('执行了打印')
  },
  closeCallback(vue) {
    console.log('关闭了打印工具')
  }
})
 
// 批量要打印的数据
const data = ref(['123456789', '987654321'])
 
const options = {
  // format: 'EAN13', // 格式
  height: 50,
  // text: "覆盖显示的文本",
  fontSize: 16,
  // background: '#ccc',
  lineColor: 'black'
}
 
const barcodeList = ref([])
onMounted(() => {
  data.value.forEach((item, index) => {
    JsBarcode(barcodeList.value[index], item, options)
  })
})
</script>
 
<style lang="less">
@media print {
  @page {
    size: auto; /* 重置页面大小,避免出现空白页 */
    margin-top: 0; /* 取消页眉 */
    margin-bottom: 0; /* 取消页脚 */
     margin-left: 0;  margin-right: 0; /* 取消默认左右页边距 */
  }
}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值