Vue中使用vue-plugin-hiprint插件進行打印的功能實現(xiàn)
引言
hiprint 是一個web 打印的js組件,無需安裝軟件。支持windows,macOS,linux 系統(tǒng),支持移動端,PC端瀏覽器,angular,vue,react 等 分頁預(yù)覽,打印,操作簡單,運行快速。預(yù)覽界面為css+html 。支持?jǐn)?shù)據(jù)分組,批量預(yù)覽。生成pdf,圖片更方便
附上該插件的官方文檔 http://hiprint.io/,本文中未提及的功能基本上都能在官網(wǎng)中找到。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 插件。你也可以根據(jù)需要只加載單個插件。 -->
<!-- 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>
<!-- 調(diào)用瀏覽器打印窗口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為預(yù)覽打印
hiprintTemplate.printByHtml(printRef.value,{});
}
</script>
三、打印配置項
//初始化 hiprint.init();
//創(chuàng)建實例
var hiprintTemplate = new hiprint.PrintTemplate();//默認(rèn)添加A4大小的面板
var panel = hiprintTemplate.addPrintPanel({ width: 100, height: 130, paperFooter: 340, paperHeader: 10 });//參數(shù)請參考下方列表
| 名稱 | 類型 | 描述 | 默認(rèn)值 |
|---|---|---|---|
| width | number | 寬度(毫米) | A4寬 |
| height | number | 高度(毫米) | A4高 |
| paperType | string | A3,A4等 自定義則為空 | A4 |
| paperHeader | number | 頁眉線top值(單位pt),默認(rèn)0,可空 | 0 |
| paperFooter | number | 頁尾線top值(單位pt),默認(rèn)等于紙張高度對應(yīng)的pt值,可空 | 紙張高pt |
| paperNumberLeft | number | 頁碼left(pt) 默認(rèn)紙張寬pt-20 可空 | 張寬pt-20 |
| paperNumberTop | number | 頁碼top(pt) 默認(rèn)紙張高pt-20 可空 | 高pt-20 |
| paperNumberDisabled | boolean | 禁用頁碼。默認(rèn)false 可空 | false |
| rotate | boolean | 旋轉(zhuǎn) 比如A4紙旋轉(zhuǎn) 上面寬和高需要對換 默認(rèn)false可空 | false |
//根據(jù)實際需要進行選擇,其他配置見官網(wǎng)
//文本
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:'' } });
//豎線//不設(shè)置寬度
panel.addPrintVline({ options: { height: 35, top: 230, left: 20 } });
//橫線 //不設(shè)置高度
panel.addPrintHline({ options: { width: 140, top: 245, left: 120 } });
//矩形
panel.addPrintRect({ options: { width: 35, height: 35, top: 230, left: 60 } });
//打印設(shè)計
hiprintTemplate.design('#hiprint-printTemplate');
四、打印JSON模板
新建panel.js文件,該文件為JSON模板,后續(xù)需要見數(shù)據(jù)填入,示例內(nèi)容如下,可以根據(jù)自己需要實現(xiàn)的效果進行修改。
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';
//將數(shù)據(jù)以數(shù)組方式存入,會根據(jù)模板定義的樣式來打印
let printList=[]
printList.push({
barcodeNumber: newfanframecode
})
hiprint.init();
var hiprintTemplate = new hiprint.PrintTemplate({
template: panel
});
hiprintTemplate.print(printList);
五、總結(jié)
以上就是Vue中使用vue-plugin-hiprint插件進行打印的實現(xiàn)步驟的詳細(xì)內(nèi)容,更多關(guān)于Vue vue-plugin-hiprint打印的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue中el-select中多選回顯數(shù)據(jù)后沒法重新選擇和更改的解決
本文主要介紹了vue中el-select中多選回顯數(shù)據(jù)后沒法重新選擇和更改解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01
關(guān)于element-ui的隱藏組件el-scrollbar的使用
這篇文章主要介紹了關(guān)于element-ui的隱藏組件el-scrollbar的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
詳解Vue的數(shù)據(jù)及事件綁定和filter過濾器
這篇文章主要為大家介紹了Vue的數(shù)據(jù)及事件綁定和filter過濾器,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01
在vue項目中集成graphql(vue-ApolloClient)
這篇文章主要介紹了在vue項目中集成graphql(vue-ApolloClient),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09

