Vue中使用vue-plugin-hiprint插件进行打印的功能实现
引言
hiprint 是一个web 打印的js组件,无需安装软件。支持windows,macOS,linux 系统,支持移动端,PC端浏览器,angular,vue,react 等 分页预览,打印,操作简单,运行快速。预览界面为css+html 。支持数据分组,批量预览。生成pdf,图片更方便
附上该插件的官方文档 http://hiprint.io/,本文中未提及的功能基本上都能在官网中找到。Gitee链接
一、安装插件
1、安装
在项目入口文件index.html中引入样式文件,按需引入即可。
//在项目的入口文件中引入所需的CDN <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>hinnn-hiprint</title> <!-- hiprint --> <link href="hiprint/css/hiprint.css" rel="external nofollow" rel="stylesheet"> <link href="hiprint/css/print-lock.css" rel="external nofollow" rel="external nofollow" rel="stylesheet"> <link href="hiprint/css/print-lock.css" rel="external nofollow" rel="external nofollow" media="print" rel="stylesheet"> </head> <body> <!-- 加载 hiprint 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 --> <!-- polyfill.min.js解决浏览器兼容性问题v6.26.0--> <script src="hiprint/polyfill.min.js"></script> <!-- hiprint 核心js--> <script src="hiprint/hiprint.bundle.js"></script> <!-- 条形码生成组件--> <script src="hiprint/plugins/JsBarcode.all.min.js"></script> <!-- 二维码生成组件--> <script src="hiprint/plugins/qrcode.js"></script> <!-- 调用浏览器打印窗口helper类,可自行替换--> <script src="hiprint/plugins/jquery.hiwprint.js"></script> </body> </html>
2、页面中引入
全局引入
123 | // main.ts import { hiPrintPlugin } from 'vue-plugin-hiprint' app.use(hiPrintPlugin) |
12 | // 局部引入 import { hiPrintPlugin } from 'vue-plugin-hiprint' |
二、打印html元素
123456789101112131415 | <template> <div ref= "printRef" style= "height: 100%;width: 100%;" ></div> </template> <script lang= "ts" setup name= "mesFanTwins" > import {hiprint} from 'vue-plugin-hiprint' ; const printRef = ref(); const print()=()=>{ //初始化 hiprint.init() const hiprintTemplate = new hiprint.PrintTemplate() // printByHtml为预览打印 hiprintTemplate.printByHtml(printRef.value,{}); } </script> |
三、打印配置项
12 | //初始化 hiprint.init(); |
123 | //创建实例 var hiprintTemplate = new hiprint.PrintTemplate(); //默认添加A4大小的面板 var panel = hiprintTemplate.addPrintPanel({ width: 100, height: 130, paperFooter: 340, paperHeader: 10 }); //参数请参考下方列表 |
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
width | number | 宽度(毫米) | A4宽 |
height | number | 高度(毫米) | A4高 |
paperType | string | A3,A4等 自定义则为空 | A4 |
paperHeader | number | 页眉线top值(单位pt),默认0,可空 | 0 |
paperFooter | number | 页尾线top值(单位pt),默认等于纸张高度对应的pt值,可空 | 纸张高pt |
paperNumberLeft | number | 页码left(pt) 默认纸张宽pt-20 可空 | 张宽pt-20 |
paperNumberTop | number | 页码top(pt) 默认纸张高pt-20 可空 | 高pt-20 |
paperNumberDisabled | boolean | 禁用页码。默认false 可空 | false |
rotate | boolean | 旋转 比如A4纸旋转 上面宽和高需要对换 默认false可空 | false |
123456789101112131415161718192021 | //根据实际需要进行选择,其他配置见官网 //文本 panel.addPrintText({ options: { width: 140, height: 15, top: 20, left: 20, title: 'hiprint插件手动添加text' , textAlign: 'center' } }); //条形码 panel.addPrintText({ options: { width: 140, height: 35, top: 40, left: 20, title: '123456' , textType: 'barcode' } }); //二维码 panel.addPrintText({ options: { width: 35, height: 35, top: 40, left: 165, title: '123456' , textType: 'qrcode' } }); //长文本 panel.addPrintLongText({ options: { width: 180, height: 35, top: 90, left: 20, title: '长文本:hiprint是一个很好的webjs打印,浏览器在的地方他都可以运行' } }); //表格 panel.addPrintTable({ options: { width: 252, height: 35, top: 130, left: 20, content: $( '#testTable' ).html() } }); //Html panel.addPrintHtml({ options: { width: 140, height: 35, top: 180, left: 20, content: '' } }); //竖线//不设置宽度 panel.addPrintVline({ options: { height: 35, top: 230, left: 20 } }); //横线 //不设置高度 panel.addPrintHline({ options: { width: 140, top: 245, left: 120 } }); //矩形 panel.addPrintRect({ options: { width: 35, height: 35, top: 230, left: 60 } }); //打印设计 hiprintTemplate.design( '#hiprint-printTemplate' ); |
四、打印JSON模板
新建panel.js文件,该文件为JSON模板,后续需要见数据填入,示例内容如下,可以根据自己需要实现的效果进行修改。
123456789101112131415161718192021222324 | export const panel = { "panels" : [ { "index" :0, "height" :20, "width" :20, "paperHeader" :2, "paperFooter" :0, "paperNumberDisabled" : true , "rotate" : true , "printElements" :[{ "options" :{ "left" :4, "top" :30, "height" :40, "width" :40, "field" : "barcodeNumber" , "textType" : "qrcode" }, "printElementType" :{ "type" : "text" }, }, ], }] } |
123456789101112 | import { panel } from './panel.js' ; import {hiprint} from 'vue-plugin-hiprint' ; //将数据以数组方式存入,会根据模板定义的样式来打印 let printList=[] printList.push({ barcodeNumber: newfanframecode }) hiprint.init(); var hiprintTemplate = new hiprint.PrintTemplate({ template: panel }); hiprintTemplate.print(printList); |
栏 目:JavaScript
本文标题:Vue中使用vue-plugin-hiprint插件进行打印的功能实现
本文地址:https://www.fushidao.cc/wangluobiancheng/23701.html
您可能感兴趣的文章
- 07-21Vue中使用vue-plugin-hiprint插件进行打印的功能实现
- 07-21基于vue3与supabase系统认证机制详解
- 07-21Vue使用wavesurfer.js实现音频可视化的示例详解
- 07-21微信小程序事件绑定基本语法示例详解
- 07-21vue+d3js+fastapi实现天气柱状图折线图饼图的示例
- 07-21Vue.js 的计算属性和侦听器详解(提升数据处理与交互的关键工具)
- 07-21React useEffect、useLayoutEffect底层机制及区别介绍
- 07-21JavaScript 中问号的三种用法 ??和?.以及?:
- 07-21JavaScript检查变量类型的常用方法
- 07-21一文详解javascript语言中的类(class)


阅读排行
- 1Vue中使用vue-plugin-hiprint插件进行打印的功能实现
- 2基于vue3与supabase系统认证机制详解
- 3Vue使用wavesurfer.js实现音频可视化的示例详解
- 4微信小程序事件绑定基本语法示例详解
- 5vue+d3js+fastapi实现天气柱状图折线图饼图的示例
- 6Vue.js 的计算属性和侦听器详解(提升数据处理与交互的关键工具)
- 7React useEffect、useLayoutEffect底层机制及区别介绍
- 8JavaScript 中问号的三种用法 ??和?.以及?:
- 9JavaScript检查变量类型的常用方法
- 10一文详解javascript语言中的类(class)
推荐教程
- 04-23JavaScript Array实例方法flat的实现
- 04-23Vue3使用v-if指令进行条件渲染的实例代码
- 04-23THREE.JS使用TransformControls对模型拖拽的代码实例
- 04-23vue3+ts项目搭建的实现示例
- 07-21JavaScript检查变量类型的常用方法
- 07-21基于vue3与supabase系统认证机制详解
- 07-21Vue中使用vue-plugin-hiprint插件进行打印的功能实现
- 04-23JavaScript实现下载超大文件的方法详解
- 07-21微信小程序事件绑定基本语法示例详解
- 07-21JavaScript 中问号的三种用法 ??和?.以及?: