欢迎来到科站长!

JavaScript

当前位置: 主页 > 网络编程 > JavaScript

Vue中使用vue-plugin-hiprint插件进行打印的功能实现

时间:2025-07-21 09:16:09|栏目:JavaScript|点击:

引言

hiprint 是一个web 打印的js组件,无需安装软件。支持windows,macOS,linux 系统,支持移动端,PC端浏览器,angular,vue,react 等 分页预览,打印,操作简单,运行快速。预览界面为css+html 。支持数据分组,批量预览。生成pdf,图片更方便

附上该插件的官方文档 http://hiprint.io/,本文中未提及的功能基本上都能在官网中找到。Gitee链接

一、安装插件

1、安装

1
2
3
npm install vue-plugin-hi-print
# 或者
yarn add vue-plugin-hi-print

在项目入口文件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.tsimport { 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 });//参数请参考下方列表
名称类型描述默认值
widthnumber宽度(毫米)A4宽
heightnumber高度(毫米)A4高
paperTypestringA3,A4等 自定义则为空A4
paperHeadernumber页眉线top值(单位pt),默认0,可空0
paperFooternumber页尾线top值(单位pt),默认等于纸张高度对应的pt值,可空纸张高pt
paperNumberLeftnumber页码left(pt) 默认纸张宽pt-20 可空张宽pt-20
paperNumberTopnumber页码top(pt) 默认纸张高pt-20 可空高pt-20
paperNumberDisabledboolean禁用页码。默认false 可空false
rotateboolean旋转 比如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() } });//Htmlpanel.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模板,后续需要见数据填入,示例内容如下,可以根据自己需要实现的效果进行修改。

123456789101112131415161718192021222324export 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"},          },        ],      }]  }
123456789101112import { 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);


上一篇:基于vue3与supabase系统认证机制详解

栏    目:JavaScript

下一篇:暂无

本文标题:Vue中使用vue-plugin-hiprint插件进行打印的功能实现

本文地址:https://www.fushidao.cc/wangluobiancheng/23701.html

广告投放 | 联系我们 | 版权申明

申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:257218569 | 邮箱:257218569@qq.com

Copyright © 2018-2025 科站长 版权所有冀ICP备14023439号