Skip to content

Commit 8836312

Browse files
committed
update
1 parent 73e527e commit 8836312

File tree

8 files changed

+288
-49
lines changed

8 files changed

+288
-49
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,25 @@ import VXETablePluginExportPDF from 'vxe-table-plugin-export-pdf'
2222
VXETable.use(VXETablePluginExportPDF)
2323
```
2424

25+
## Font
26+
27+
```javascript
28+
// ...
29+
import VXETablePluginExportPDF from 'vxe-table-plugin-export-pdf'
30+
// ...
31+
32+
VXETablePluginExportPDF.setup({
33+
fontName: 'SourceHanSans-Bold', // 指定字体
34+
fontUrl: 'https://cdn.jsdelivr.net/npm/vxe-table-plugin-export-pdf/fonts/source-han-sans-bold.js' // 字体库下载路径,可以将包下载放到自己服务器中
35+
})
36+
```
37+
2538
## Demo
2639

2740
```html
2841
<vxe-toolbar>
2942
<template v-slot:buttons>
30-
<vxe-button @click="exportEvent">export.pdf</vxe-button>
43+
<vxe-button @click="exportEvent">MyExport.pdf</vxe-button>
3144
</template>
3245
</vxe-toolbar>
3346

@@ -60,7 +73,7 @@ export default {
6073
methods: {
6174
exportEvent() {
6275
this.$refs.xTable.exportData({
63-
filename: 'export',
76+
filename: 'MyExport',
6477
type: 'pdf'
6578
})
6679
}

dist/index.common.js

Lines changed: 95 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 94 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
/* eslint-disable no-unused-vars */
2626

2727
/* eslint-enable no-unused-vars */
28+
var isWin = typeof window !== 'undefined';
29+
var globalOptions = {};
30+
var globalFonts = {};
31+
2832
var _vxetable;
2933

3034
function getFooterCellValue($table, opts, rows, column) {
@@ -35,9 +39,13 @@
3539

3640
function exportPDF(params) {
3741
var colWidth = 0;
42+
var msgKey = 'pdf';
43+
var fontName = globalOptions.fontName,
44+
fontUrl = globalOptions.fontUrl;
3845
var options = params.options,
3946
columns = params.columns,
4047
datas = params.datas;
48+
var showMsg = options.message !== false;
4149
var $table = params.$table;
4250
var treeConfig = $table.treeConfig,
4351
treeOpts = $table.treeOpts;
@@ -46,7 +54,6 @@
4654
isHeader = options.isHeader,
4755
isFooter = options.isFooter,
4856
original = options.original,
49-
message = options.message,
5057
footerFilterMethod = options.footerFilterMethod;
5158
var footList = [];
5259
var headers = columns.map(function (column) {
@@ -88,27 +95,76 @@
8895
});
8996
footList.push(item);
9097
});
91-
} // 转换pdf
98+
}
9299

93-
/* eslint-disable new-cap */
100+
var exportMethod = function exportMethod() {
101+
/* eslint-disable new-cap */
102+
var doc = new _jspdf["default"]({
103+
putOnlyUsedFonts: true,
104+
orientation: 'landscape'
105+
});
94106

107+
if (fontName && fontUrl) {
108+
doc.addFont(fontName + '.ttf', fontName, 'normal');
109+
doc.setFont(fontName, 'normal');
110+
}
95111

96-
var doc = new _jspdf["default"]({
97-
putOnlyUsedFonts: true,
98-
orientation: 'landscape'
99-
});
100-
doc.table(1, 1, rowList.concat(footList), headers, {
101-
printHeaders: isHeader,
102-
autoSize: false
103-
});
104-
doc.save("".concat(filename, ".").concat(type));
112+
doc.table(1, 1, rowList.concat(footList), headers, {
113+
printHeaders: isHeader,
114+
autoSize: false
115+
});
116+
doc.save("".concat(filename, ".").concat(type));
105117

106-
if (message !== false) {
118+
if (showMsg) {
119+
_vxetable.modal.close(msgKey);
120+
121+
_vxetable.modal.message({
122+
message: _vxetable.t('vxe.table.expSuccess'),
123+
status: 'success'
124+
});
125+
}
126+
};
127+
128+
if (showMsg) {
107129
_vxetable.modal.message({
108-
message: _vxetable.t('vxe.table.expSuccess'),
109-
status: 'success'
130+
id: msgKey,
131+
message: _vxetable.t('vxe.table.expLoading'),
132+
status: 'loading',
133+
duration: -1
110134
});
135+
} // 转换pdf
136+
137+
138+
checkFont().then(function () {
139+
if (showMsg) {
140+
setTimeout(exportMethod, 1000);
141+
} else {
142+
exportMethod();
143+
}
144+
});
145+
}
146+
147+
function checkFont() {
148+
var fontName = globalOptions.fontName,
149+
fontUrl = globalOptions.fontUrl;
150+
151+
if (fontName && fontUrl) {
152+
if (!globalFonts[fontName]) {
153+
globalFonts[fontName] = new Promise(function (resolve, reject) {
154+
var fontScript = document.createElement('script');
155+
fontScript.src = fontUrl;
156+
fontScript.type = 'text/javascript';
157+
fontScript.onload = resolve;
158+
fontScript.onerror = reject;
159+
document.body.appendChild(fontScript);
160+
});
161+
return globalFonts[fontName];
162+
}
111163
}
164+
165+
return new Promise(function (resolve) {
166+
return setTimeout(resolve, 1000);
167+
});
112168
}
113169

114170
function handleExportEvent(params) {
@@ -117,13 +173,30 @@
117173
return false;
118174
}
119175
}
176+
177+
function setup(options) {
178+
var _Object$assign = Object.assign(globalOptions, options),
179+
fontName = _Object$assign.fontName,
180+
fontUrl = _Object$assign.fontUrl;
181+
182+
if (fontName) {
183+
if (!fontUrl) {
184+
throw new Error('The fontUrl cannot be empty.');
185+
}
186+
187+
if (isWin && !window.jsPDF) {
188+
window.jsPDF = _jspdf["default"];
189+
}
190+
}
191+
}
120192
/**
121193
* 基于 vxe-table 表格的增强插件,支持导出 pdf 格式
122194
*/
123195

124196

125197
var VXETablePluginExportPDF = {
126-
install: function install(xtable) {
198+
setup: setup,
199+
install: function install(xtable, options) {
127200
var interceptor = xtable.interceptor;
128201
_vxetable = xtable;
129202
Object.assign(xtable.types, {
@@ -132,11 +205,15 @@
132205
interceptor.mixin({
133206
'event.export': handleExportEvent
134207
});
208+
209+
if (options) {
210+
setup(options);
211+
}
135212
}
136213
};
137214
_exports.VXETablePluginExportPDF = VXETablePluginExportPDF;
138215

139-
if (typeof window !== 'undefined' && window.VXETable) {
216+
if (isWin && window.VXETable) {
140217
window.VXETable.use(VXETablePluginExportPDF);
141218
}
142219

dist/index.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fonts/source-han-sans-bold.js

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
declare module 'jspdf' {
22
export class jsPDF {
33
constructor(options: any)
4+
addFont(postScriptName: string, id: string, fontStyle: string, encoding?: any): any;
5+
setFont(fontName: string, fontStyle: string): any;
46
table(x: number, y: number, row: any[], headers: any[], options?: any): any;
57
save(filename: string): any;
68
}
79
export default jsPDF
8-
}
10+
}

index.ts

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import {
1010
import jsPDF from 'jspdf'
1111
/* eslint-enable no-unused-vars */
1212

13+
const isWin = typeof window !== 'undefined'
14+
const globalOptions: VXETablePluginExportPDFOptions = {}
15+
const globalFonts: { [key: string]: any } = {}
1316
let _vxetable: typeof VXETable
1417

1518
function getFooterCellValue ($table: Table, opts: ExportOptons, rows: any[], column: ColumnConfig) {
@@ -19,10 +22,13 @@ function getFooterCellValue ($table: Table, opts: ExportOptons, rows: any[], col
1922

2023
function exportPDF (params: InterceptorExportParams) {
2124
let colWidth = 0
25+
const msgKey = 'pdf'
26+
const { fontName, fontUrl } = globalOptions
2227
const { options, columns, datas } = params
28+
const showMsg = options.message !== false
2329
const $table: any = params.$table
2430
const { treeConfig, treeOpts } = $table
25-
const { type, filename, isHeader, isFooter, original, message, footerFilterMethod } = options
31+
const { type, filename, isHeader, isFooter, original, footerFilterMethod } = options
2632
const footList: { [key: string]: any }[] = []
2733
const headers: { [key: string]: any }[] = columns.map((column) => {
2834
const title = XEUtils.toString(original ? column.property : column.getTitle()) || ' '
@@ -60,14 +66,49 @@ function exportPDF (params: InterceptorExportParams) {
6066
footList.push(item)
6167
})
6268
}
69+
const exportMethod = () => {
70+
/* eslint-disable new-cap */
71+
const doc = new jsPDF({ putOnlyUsedFonts: true, orientation: 'landscape' })
72+
if (fontName && fontUrl) {
73+
doc.addFont(fontName + '.ttf', fontName, 'normal')
74+
doc.setFont(fontName, 'normal')
75+
}
76+
doc.table(1, 1, rowList.concat(footList), headers, { printHeaders: isHeader, autoSize: false })
77+
doc.save(`${filename}.${type}`)
78+
if (showMsg) {
79+
_vxetable.modal.close(msgKey)
80+
_vxetable.modal.message({ message: _vxetable.t('vxe.table.expSuccess'), status: 'success' })
81+
}
82+
}
83+
if (showMsg) {
84+
_vxetable.modal.message({ id: msgKey, message: _vxetable.t('vxe.table.expLoading'), status: 'loading', duration: -1 })
85+
}
6386
// 转换pdf
64-
/* eslint-disable new-cap */
65-
const doc = new jsPDF({ putOnlyUsedFonts: true, orientation: 'landscape' })
66-
doc.table(1, 1, rowList.concat(footList), headers, { printHeaders: isHeader, autoSize: false })
67-
doc.save(`${filename}.${type}`)
68-
if (message !== false) {
69-
_vxetable.modal.message({ message: _vxetable.t('vxe.table.expSuccess'), status: 'success' })
87+
checkFont().then(() => {
88+
if (showMsg) {
89+
setTimeout(exportMethod, 1000)
90+
} else {
91+
exportMethod()
92+
}
93+
})
94+
}
95+
96+
function checkFont () {
97+
const { fontName, fontUrl } = globalOptions
98+
if (fontName && fontUrl) {
99+
if (!globalFonts[fontName]) {
100+
globalFonts[fontName] = new Promise((resolve, reject) => {
101+
const fontScript = document.createElement('script')
102+
fontScript.src = fontUrl
103+
fontScript.type = 'text/javascript'
104+
fontScript.onload = resolve
105+
fontScript.onerror = reject
106+
document.body.appendChild(fontScript)
107+
})
108+
return globalFonts[fontName]
109+
}
70110
}
111+
return new Promise(resolve => setTimeout(resolve, 1000))
71112
}
72113

73114
function handleExportEvent (params: InterceptorExportParams) {
@@ -77,21 +118,48 @@ function handleExportEvent (params: InterceptorExportParams) {
77118
}
78119
}
79120

121+
interface VXETablePluginExportPDFOptions {
122+
fontName?: 'SourceHanSans-Bold';
123+
fontUrl?: string;
124+
}
125+
126+
function setup (options: VXETablePluginExportPDFOptions) {
127+
const { fontName, fontUrl } = Object.assign(globalOptions, options)
128+
if (fontName) {
129+
if (!fontUrl) {
130+
throw new Error('The fontUrl cannot be empty.')
131+
}
132+
if (isWin && !window.jsPDF) {
133+
window.jsPDF = jsPDF
134+
}
135+
}
136+
}
137+
138+
declare global {
139+
interface Window {
140+
jsPDF: any;
141+
}
142+
}
143+
80144
/**
81145
* 基于 vxe-table 表格的增强插件,支持导出 pdf 格式
82146
*/
83147
export const VXETablePluginExportPDF = {
84-
install (xtable: typeof VXETable) {
148+
setup,
149+
install (xtable: typeof VXETable, options?: VXETablePluginExportPDFOptions) {
85150
const { interceptor } = xtable
86151
_vxetable = xtable
87152
Object.assign(xtable.types, { pdf: 0 })
88153
interceptor.mixin({
89154
'event.export': handleExportEvent
90155
})
156+
if (options) {
157+
setup(options)
158+
}
91159
}
92160
}
93161

94-
if (typeof window !== 'undefined' && window.VXETable) {
162+
if (isWin && window.VXETable) {
95163
window.VXETable.use(VXETablePluginExportPDF)
96164
}
97165

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"name": "vxe-table-plugin-export-pdf",
3-
"version": "1.3.3",
3+
"version": "1.4.2",
44
"description": "基于 vxe-table 表格的增强插件,支持导出 pdf 格式",
55
"scripts": {
66
"lib": "gulp build"
77
},
88
"files": [
9+
"fonts",
910
"dist",
1011
"*.ts",
1112
"*.d.ts"

0 commit comments

Comments
 (0)