-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
230 lines (213 loc) · 7.02 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import XEUtils from 'xe-utils'
import type { VXETableCore, VxeTableConstructor, VxeTablePropTypes, VxeTableDefines, VxeGlobalInterceptorHandles } from 'vxe-table'
import type jsPDF from 'jspdf'
let globalVxetable: VXETableCore
let globalJsPDF: any
declare module 'vxe-table' {
export namespace VxeTablePropTypes {
export interface ExportConfig {
fontName?: string;
}
}
}
interface VXETablePluginExportPDFFonts {
fontName: string;
fontStyle?: 'normal';
fontUrl: string;
}
interface VXETablePluginExportPDFOptions {
jsPDF?: any
fontName?: string;
fonts?: VXETablePluginExportPDFFonts[];
beforeMethod?: Function;
}
const globalOptions: VXETablePluginExportPDFOptions = {}
const globalFonts: { [key: string]: any } = {}
function getCellText (cellValue: any) {
return cellValue || ' '
}
function getFooterCellValue ($xeTable: VxeTableConstructor, opts: VxeTablePropTypes.ExportConfig, row: any, column: VxeTableDefines.ColumnInfo) {
const _columnIndex = $xeTable.getVTColumnIndex(column)
// 兼容老模式
if (XEUtils.isArray(row)) {
return getCellText(row[_columnIndex])
}
return getCellText(XEUtils.get(row, column.field))
}
function getFooterData (opts: VxeTablePropTypes.ExportConfig, footerData: any[][]) {
const { footerFilterMethod } = opts
return footerFilterMethod ? footerData.filter((items, index) => footerFilterMethod({ items, $rowIndex: index })) : footerData
}
function exportPDF (params: VxeGlobalInterceptorHandles.InterceptorExportParams) {
const { modal, t } = globalVxetable
const { fonts, beforeMethod } = globalOptions
const { $table, options, columns, datas } = params
const { props } = $table
const { treeConfig } = props
const { computeColumnOpts, computeTreeOpts } = $table.getComputeMaps()
const treeOpts = computeTreeOpts.value
const columnOpts = computeColumnOpts.value
const dX = 7
const dY = 15.8
const ratio = 3.78
const pdfWidth = 210
let colWidth = 0
const msgKey = 'pdf'
const showMsg = options.message !== false
const { type, filename, isHeader, isFooter, original } = options
const footList: { [key: string]: any }[] = []
const headers: any[] = columns.map((column) => {
const { id, field, renderWidth } = column
const headExportMethod = (column as any).headerExportMethod || (columnOpts as any).headerExportMethod
const title = headExportMethod ? headExportMethod({ column, options, $table }) : (XEUtils.toValueString(original ? field : column.getTitle()))
const width = renderWidth / ratio
colWidth += width
return {
name: id,
prompt: getCellText(title),
width
}
})
const offsetWidth = (colWidth - Math.floor(pdfWidth + dX * 2 * ratio)) / headers.length
headers.forEach((column) => {
column.width = column.width - offsetWidth
})
const rowList: any[] = datas.map((row) => {
const item: any = {}
columns.forEach((column) => {
item[column.id] = getCellText(treeConfig && column.treeNode ? (' '.repeat(row._level * treeOpts.indent / 8) + row[column.id]) : row[column.id])
})
return item
})
if (isFooter) {
const { footerData } = $table.getTableData()
const footers = getFooterData(options, footerData)
footers.forEach(rows => {
const item: any = {}
columns.forEach((column) => {
item[column.id] = getFooterCellValue($table, options, rows, column)
})
footList.push(item)
})
}
let fontConf: VXETablePluginExportPDFFonts | null | undefined
const fontName = options.fontName || globalOptions.fontName
if (fonts) {
if (fontName) {
fontConf = fonts.find(item => item.fontName === fontName)
}
if (!fontConf) {
fontConf = fonts[0]
}
}
const exportMethod = () => {
/* eslint-disable new-cap */
const doc: jsPDF = new (globalJsPDF || ((window as any).jspdf ? (window as any).jspdf.jsPDF : (window as any).jsPDF))({ orientation: 'landscape' })
// 设置字体
doc.setFontSize(10)
doc.internal.pageSize.width = pdfWidth
if (fontConf) {
const { fontName, fontStyle = 'normal' } = fontConf
if (globalFonts[fontName]) {
doc.addFont(fontName + '.ttf', fontName, fontStyle)
doc.setFont(fontName, fontStyle)
}
}
if (beforeMethod && beforeMethod({ $pdf: doc, $table, options, columns, datas }) === false) {
return
}
if (options.sheetName) {
const title = XEUtils.toValueString(options.sheetName)
const textWidth = doc.getTextWidth(title)
doc.text(title, (pdfWidth - textWidth) / 2, dY / 2 + 2)
}
// 转换数据
doc.table(dX, dY, rowList.concat(footList), headers, {
printHeaders: isHeader,
autoSize: false,
fontSize: 6
})
// 导出 pdf
doc.save(`${filename}.${type}`)
if (showMsg && modal) {
modal.close(msgKey)
modal.message({ content: t('vxe.table.expSuccess'), status: 'success' })
}
}
if (showMsg && modal) {
modal.message({ id: msgKey, content: t('vxe.table.expLoading'), status: 'loading', duration: -1 })
}
checkFont(fontConf).then(() => {
if (showMsg) {
setTimeout(exportMethod, 1500)
} else {
exportMethod()
}
})
}
function checkFont (fontConf?: VXETablePluginExportPDFFonts | null | undefined) {
if (fontConf) {
const { fontName, fontUrl } = fontConf
if (fontUrl && !globalFonts[fontName]) {
globalFonts[fontName] = new Promise((resolve, reject) => {
const fontScript = document.createElement('script')
fontScript.src = fontUrl
fontScript.type = 'text/javascript'
fontScript.onload = resolve
fontScript.onerror = reject
document.body.appendChild(fontScript)
})
return globalFonts[fontName]
}
}
return Promise.resolve()
}
function handleExportEvent (params: VxeGlobalInterceptorHandles.InterceptorExportParams) {
if (params.options.type === 'pdf') {
exportPDF(params)
return false
}
}
function pluginSetup (options: VXETablePluginExportPDFOptions) {
Object.assign(globalOptions, options)
}
/**
* 基于 vxe-table 表格的扩展插件,支持导出 pdf 格式
*/
export const VXETablePluginExportPDF = {
config: pluginSetup,
install (vxetable: VXETableCore, options?: VXETablePluginExportPDFOptions) {
// 检查版本
if (!/^(4)\./.test(vxetable.version) && !/v4/i.test((vxetable as any).v)) {
console.error('[vxe-table-plugin-export-pdf 4.x] Version vxe-table 4.x is required')
}
globalVxetable = vxetable
globalJsPDF = options ? options.jsPDF : null
const setConfig = vxetable.setConfig || vxetable.config
setConfig({
table: {
exportConfig: {
_typeMaps: {
pdf: 1
}
} as any
},
// 兼容老版本
export: {
types: {
pdf: 1
}
} as any
})
vxetable.interceptor.mixin({
'event.export': handleExportEvent
})
if (options) {
pluginSetup(options)
}
}
}
if (typeof window !== 'undefined' && window.VXETable && window.VXETable.use) {
window.VXETable.use(VXETablePluginExportPDF)
}
export default VXETablePluginExportPDF