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' }) // 指定打印机,同时配置客户端的token
      hiprint.hiwebSocket.send({ client: '', type: 'url_pdf', pdf_path: 'http://xx.xxx.xx.xx:1123/preview/xxx.pdf', pages: 1, copies: 2 }) // 设置打印的页数和打印的份数
    },

如果需要设置客户端的token的话在此处,应用即可。
在这里插入图片描述
有打印成功的状态返回
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值