hiprint结合vue2项目实现静默打印详细使用步骤

代码地址是:vue-plugin-hiprint: hiprint for Vue2/Vue3 ⚡打印、打印设计、可视化设计器、报表设计、元素编辑、可视化打印编辑

本地安装包地址:electron-hiprint 发行版 - Gitee.com

1、先安装hipint安装包在本地

2、项目运行npm(socket.io是为了实现自动打印下载的)

复制代码
npm install vue-plugin-hiprint
npm i jquery --save-d
npm install socket.io

3、在main.js文件中

复制代码
import {hiPrintPlugin} from 'vue-plugin-hiprint'
Vue.use(hiPrintPlugin, '$pluginName')

import $ from "jquery";
window.jquery = window.$ = $;
Vue.prototype.$ = jquery

4、在node_modules包中找到print-lock.css样式文件复制到项目中

找到后复制到public文件夹

在public文件夹的index.html文件引入这个css文件(说是防止样式重叠使用的)

复制代码
<!-- 引入vue-plugin-hiprint的样式 -->
<link rel="stylesheet" type="text/css" media="print" href="<%= BASE_URL %>print-lock.css"></link>
<link rel="stylesheet" type="text/css" media="print" href="https://cdn.jsdelivr.net/npm/vue-plugin-hiprint@latest/dist/print-lock.css">

5、在页面的具体使用

复制代码
print(){
  const styleStr = `<style>样式代码 </style>`;

  let hiprintTemplate = new this.$pluginName.PrintTemplate()
  Array.from(this.$refs.print).forEach(v=>{
    let panel = hiprintTemplate.addPrintPanel({ paperType:'A4',paperNumberDisabled:true})
    panel.addPrintHtml({options: {"left":15,"top":5, "content":`<html><head>${styleStr}</head><body>${v.innerHTML}</body> </html>` } })
  }) 

  this.$message({
    message: '正在执行打印操作,请稍等......',
    type: 'success',
    offset:350
  });
        
  hiprintTemplate.print2()//直接静默打印
  //hiprintTemplate.print()  预览打印
}

6、在App.vue加上隐藏的样式,这样不影响页面样式

复制代码
#hiwprint_iframe{
  display: none !important;
}

最后:一定要在启动了客户端的软件情况下点击打印按钮,不然会报错未连接客户端!

相关推荐
gnip1 分钟前
http缓存
前端·javascript
我不只是切图仔36 分钟前
我只是想给网站加个注册验证码,咋就那么难!
前端·后端
JohnYan1 小时前
工作笔记 - 微信消息发送和处理
javascript·后端·微信
该用户已不存在1 小时前
macOS是开发的终极进化版吗?
前端·后端
小豆包api1 小时前
小豆包AI API × Nano Banana:3D手办 + AI视频生成,「动起来」的神级玩法!
前端·api
陈陈爱java2 小时前
Spring八股文
开发语言·javascript·数据库
布列瑟农的星空2 小时前
大话设计模式——观察者模式和发布/订阅模式的区别
前端·后端·架构
龙在天2 小时前
Vue3 实现 B站 视差 动画
前端
KenXu2 小时前
F2C Prompt to Design、AI 驱动的设计革命
前端
小鱼儿亮亮2 小时前
canvas中画线条,线条效果比预期宽1像素且模糊问题分析及解决方案
前端·react.js