1. 效果图
2. 步骤
2.1. 安装插件
vue3版本 文档地址
npm install vue3-print-nb --save
vue2版本 文档上同
npm install vue-print-nb --save
2.2. 注册组件 - 以Vue3为例
- 全局注册 -
main.js
import print from 'vue3-print-nb'
const app = createApp(App)
app.use(print)
- 按需注册
<script setup>
import vPrint from 'vue3-print-nb'
</script>
2.3. 代码展示
<template>
<div id="main">
<el-table
:data="tableData"
:span-method="arraySpanMethod"
border
style="width: 100%"
>
<el-table-column prop="id" label="ID" width="180" />
<el-table-column prop="name" label="Name" />
<el-table-column prop="amount1" sortable label="Amount 1" />
<el-table-column prop="amount2" sortable label="Amount 2" />
<el-table-column prop="amount3" sortable label="Amount 3" />
</el-table>
<el-table
:data="tableData"
:span-method="objectSpanMethod"
border
style="width: 100%; margin-top: 20px"
>
<el-table-column prop="id" label="ID" width="180" />
<el-table-column prop="name" label="Name" />
<el-table-column prop="amount1" label="Amount 1" />
<el-table-column prop="amount2" label="Amount 2" />
<el-table-column prop="amount3" label="Amount 3" />
</el-table>
</div>
<el-button type="primary" v-print="{ id: '#main', popTitle: '打印标题标题' }"
>打印页面</el-button
> // 重点代码1
</template>
<script setup>
import vPrint from 'vue3-print-nb'; // 重点代码2
const arraySpanMethod = ({ row, column, rowIndex, columnIndex }) => {
if (rowIndex % 2 === 0) {
if (columnIndex === 0) {
return [1, 2];
} else if (columnIndex === 1) {
return [0, 0];
}
}
};
const objectSpanMethod = ({ row, column, rowIndex, columnIndex }) => {
if (columnIndex === 0) {
if (rowIndex % 2 === 0) {
return {
rowspan: 2,
colspan: 1,
};
} else {
return {
rowspan: 0,
colspan: 0,
};
}
}
};
const tableData = [
{
id: '12987122',
name: 'Tom',
amount1: '234',
amount2: '3.2',
amount3: 10,
},
{
id: '12987123',
name: 'Tom',
amount1: '165',
amount2: '4.43',
amount3: 12,
},
{
id: '12987124',
name: 'Tom',
amount1: '324',
amount2: '1.9',
amount3: 9,
},
{
id: '12987125',
name: 'Tom',
amount1: '621',
amount2: '2.2',
amount3: 17,
},
{
id: '12987126',
name: 'Tom',
amount1: '539',
amount2: '4.1',
amount3: 15,
},
];
</script>
<style lang="scss" scoped></style>
注意:该插件有非常多的api,通过v-print以object形式交互,官网api参考如下:
<el-button v-print="printObj" class="mt-lg" type="primary">打印成PDF观看</el-button>
<div id="print">需要打印的内容</div>
const printObj = {
id: '#print',
};
| Parame | Explain | Type | OptionalValue | DefaultValue |
|---|---|---|---|---|
| id | Range print ID, required value | String | — | — |
| standard | Document type (Print local range only) | String | html5/loose/strict | html5 |
| extraHead |
<head></head>Add DOM nodes in the node, and separate multiple nodes with , (Print local range only) |
String | — | — |
| extraCss |
<link> New CSS style sheet , and separate multiple nodes with ,(Print local range only) |
String | — | - |
| popTitle |
<title></title> Content of label (Print local range only) |
String | — | - |
| openCallback | Call the successful callback function of the printing tool | Function | Returns the instance of Vue called at that time |
- |
| closeCallback | Close the callback function of printing tool success | Function | Returns the instance of Vue called at that time |
- |
| beforeOpenCallback | Callback function before calling printing tool | Function | Returns the instance of Vue called at that time |
- |
| url | Print the specified URL. (It is not allowed to set the ID at the same time) | string | - | - |
| asyncUrl | Return URL through 'resolve()' and Vue | Function | - | - |
| preview | Preview tool | Boolean | - | false |
| previewTitle | Preview tool Title | String | - | '打印预览' |
| previewPrintBtnLabel | The name of the preview tool button | String | - | '打印' |
| zIndex | CSS of preview tool: z-index | String,Number | - | 20002 |
| previewBeforeOpenCallback | Callback function before starting preview tool | Function | Returns the instance of Vue
|
- |
| previewOpenCallback | Callback function after fully opening preview tool | Function | Returns the instance of Vue
|
- |
| clickMounted | Click the callback function of the print button | Function | Returns the instance of Vue
|
- |