vue实现打印条码功能

需求:打印条码

分为两步:一个是打印功能,一个是生成条码功能

一、打印功能 vue-print-nb:这个插件使用起来便捷,用法如下:

1、安装

npm install vue-print-nb --save

2、在main.js文件中注册使用

import Print from 'vue-print-nb'
Vue.use(Print);

3、使用

<!--绑定id方法-->
<div id='box'>
   <p>打印内容</p>
</div>
<div v-print='#box'>打印</div>

<!--绑定对象方法-->
<div id='box'>
   <p>打印内容</p>
</div>
<div v-print='printObj'>打印</div>

export default{
   data(){
      return {
         printObj:{
            id: "printMe", // 打印的区域
            preview: false, // 预览工具是否启用
            previewTitle: '打印条码', // 预览页面的标题
            popTitle: '', // 打印页面的页眉
            previewBeforeOpenCallback(vue) {
              console.log('正在加载预览窗口')
            },
            previewOpenCallback(vue) {
              console.log('已经加载完预览窗口')
            },
            clickMounted: (vue) => {
              console.log("触发点击打印回调");
              vue.isShowPrint = true  //弹框显示条码
            },
            beforeOpenCallback(vue) {
              console.log('打开之前',vue.barcodeNum)
            },
            openCallback (vue) {
              vue.isShowPrint = false  // 关闭条码显示弹框
              console.log('执行了打印',vue.barcodeNum)
            },
         }
      }
   }
}

 二、条码实现,通过vue-barcode插件,用法如下:

1、安装插件

npm install vue-barcode --save

2、引用注册

import VueBarcode from 'vue-barcode';

3、使用

<vue-barcode value="value-to-render" :width="1.5" :height="50">
  Show this if the rendering fails.
</vue-barcode>

三、vue 打印条码功能实现完整代码 

<Modal
    v-model="isShowPrint"
    title="打印条码"
    footer-hide>
        <vue-barcode :value="barCode" :width="1.5" :height="50">
          // 显示绑定的barCode值
        </vue-barcode>
        <Button  size="small" class="mar8" @click="printBarcode(row)">条码打印</Button>
      </Modal>
export default{
   data(){
      return {
         printContent:{
            id: "printMe", // 打印的区域
            preview: false, // 预览工具是否启用
            previewTitle: '打印条码', // 预览页面的标题
            popTitle: '', // 打印页面的页眉
            previewBeforeOpenCallback(vue) {
              console.log('正在加载预览窗口')
            },
            previewOpenCallback(vue) {
              console.log('已经加载完预览窗口')
            },
            clickMounted: (vue) => {
              console.log("触发点击打印回调");
              vue.isShowPrint = true  //弹框显示条码
            },
            beforeOpenCallback(vue) {
              console.log('打开之前',vue.barcodeNum)
            },
            openCallback (vue) {
              vue.isShowPrint = false  // 关闭条码显示弹框
              console.log('执行了打印',vue.barcodeNum)
            },
         }
      }
   }
}

有问题欢迎指出~

参考链接:

https://github.com/lindell/vue-barcode
Options · lindell/JsBarcode Wiki · GitHub

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现pdf打印功能需要以下步骤: 1. 在Spring Boot中使用PDFBox或iText等库生成PDF文件。 2. 将生成的PDF文件保存到服务器上。 3. 在Vue中使用pdf.js或其他库加载服务器上的PDF文件。 4. 在Vue中使用window.print()方法打印PDF文件。 以下是具体的实现步骤: 1. 在Spring Boot中使用PDFBox或iText等库生成PDF文件 以PDFBox为例,可以使用以下代码生成PDF文件: ```java PDDocument document = new PDDocument(); PDPage page = new PDPage(); document.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(document, page); // 在页面上添加内容 contentStream.beginText(); contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12); contentStream.newLineAtOffset(100, 700); contentStream.showText("Hello, World!"); contentStream.endText(); contentStream.close(); document.save("example.pdf"); document.close(); ``` 2. 将生成的PDF文件保存到服务器上 可以使用Java的文件操作方法将生成的PDF文件保存到服务器上,例如: ```java File file = new File("example.pdf"); OutputStream outputStream = new FileOutputStream(file); document.save(outputStream); outputStream.close(); ``` 3. 在Vue中使用pdf.js或其他库加载服务器上的PDF文件 可以使用pdf.js库加载服务器上的PDF文件,并在Vue组件中显示PDF文件。 ```html <template> <div> <pdf :src="pdfFile" :page="1"></pdf> </div> </template> <script> import pdf from 'vue-pdf' export default { components: { pdf }, data () { return { pdfFile: 'http://localhost:8080/example.pdf' } } } </script> ``` 4. 在Vue中使用window.print()方法打印PDF文件 可以在Vue组件中添加一个打印按钮,并在点击时调用window.print()方法打印PDF文件。 ```html <template> <div> <pdf :src="pdfFile" :page="1"></pdf> <button @click="printPDF">Print</button> </div> </template> <script> import pdf from 'vue-pdf' export default { components: { pdf }, data () { return { pdfFile: 'http://localhost:8080/example.pdf' } }, methods: { printPDF () { window.print() } } } </script> ``` 这样就可以实现Spring Boot和Vue中的PDF打印功能了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值