引言
hiprint 是一个web 打印的js组件,无需安装软件。支持windows,macOS,linux 系统,支持移动端,PC端浏览器,angular,vue,react 等 分页预览,打印,操作简单,运行快速。预览界面为css+html 。支持数据分组,批量预览。生成pdf,图片更方便
附上该插件的官方文档 http://hiprint.io/,本文中未提及的功能基本上都能在官网中找到。Gitee链接
一、安装插件
1、安装
npm install vue-plugin-hi-print # 或者 yarn add vue-plugin-hi-print
在项目入口文件index.html中引入样式文件,按需引入即可。
//在项目的入口文件中引入所需的CDN <!DOCTYPE html> <html lang="zh-CN"> <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、页面中引入
全局引入
// main.ts import { hiPrintPlugin } from 'vue-plugin-hiprint' app.use(hiPrintPlugin)
// 局部引入 import { hiPrintPlugin } from 'vue-plugin-hiprint'
二、打印html元素
<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>
三、打印配置项
//初始化 hiprint.init();
//创建实例 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 |
//根据实际需要进行选择,其他配置见官网 //文本 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模板,后续需要见数据填入,示例内容如下,可以根据自己需要实现的效果进行修改。
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"}, }, ], }] }
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);
五、总结
以上就是Vue中使用vue-plugin-hiprint插件进行打印的实现步骤的详细内容,更多关于Vue vue-plugin-hiprint打印的资料请关注脚本之家其它相关文章!
来源链接:https://www.jb51.net/javascript/339429nfk.htm
© 版权声明
本站所有资源来自于网络,仅供学习与参考,请勿用于商业用途,否则产生的一切后果将由您(转载者)自己承担!
如有侵犯您的版权,请及时联系3500663466#qq.com(#换@),我们将第一时间删除本站数据。
如有侵犯您的版权,请及时联系3500663466#qq.com(#换@),我们将第一时间删除本站数据。
THE END
暂无评论内容