vue实现静默打印pdf
浏览器中想要打印文件,不依靠浏览器自带的打印窗口,想要实现静默打印(也就是不弹出打印对话框),同时控制打印份数的功能,一种方式是使用
vue-plugin-hiprint
和本地安装客户端electron-hiprint
本来是浏览器去调用打印控件,现在直接在对应电脑上安装程序,然后浏览器去调用该程序。
浏览器 -> electron-hiprint -> 打印
-
vue-plugin-hiprint:https://gitee.com/CcSimple/vue-plugin-hiprint
-
electron-hiprint: https://gitee.com/CcSimple/electron-hiprint
- 下载地址:https://gitee.com/CcSimple/electron-hiprint/releases
- 找到指定系统版本,进行下载并安装
- 【安装注意:安装客户端时请 以管理员身份运行 ,才能成功添加 URLScheme】
- URLScheme
hiprint://
实现步骤
安装 electron-hiprint
上面有安装地址,比如windows 64位的选择这个安装包下载,并使用管理员身份安装
安装完成并启动后
在vue项目中使用vue-plugin-hiprint
安装包
npm install vue-plugin-hiprint
# 或者
yarn add vue-plugin-hiprint
项目中引入包
// 全局引入
// main.js
import { hiPrintPlugin } from 'vue-plugin-hiprint'
Vue.use(hiPrintPlugin, '$pluginName')// 局部引入(推荐)
import { hiPrintPlugin } from 'vue-plugin-hiprint'
页面中使用
<button @click="print">打印</YsButton>import { hiPrintPlugin } from 'vue-plugin-hiprint'async print() {const printer = hiprint.hiwebSocket.getPrinterList().find(i => i.isDefault); // 获取当前默认的打印机console.log(printer)// hiprint.hiwebSocket.send({ client: 'TEST_PRINT', printer: printer?.name, type: 'url_pdf', pdf_path: 'http://xx.xxx.xx.xx:1123/preview/xxx.pdf' }) // 指定打印机,同时配置客户端的tokenhiprint.hiwebSocket.send({ client: '', type: 'url_pdf', pdf_path: 'http://xx.xxx.xx.xx:1123/preview/xxx.pdf', pages: 1, copies: 2 }) // 设置打印的页数和打印的份数},
如果需要设置客户端的token的话在此处,应用即可。
有打印成功的状态返回