Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xuliangzhan committed Jan 21, 2024
1 parent 13632e8 commit 4dced8f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@
## Installing

```shell
npm install xe-utils vxe-table@next vxe-table-plugin-export-pdf@next jspdf
npm install vxe-table@next vxe-table-plugin-export-pdf@next jspdf
```

```javascript
// ...
import VXETable from 'vxe-table'
import VXETablePluginExportPDF from 'vxe-table-plugin-export-pdf'
import jspdf from 'jspdf'
// ...

VXETable.use(VXETablePluginExportPDF)
// 方式1:NPM 安装,注入 jspdf 对象
VXETable.use(VXETablePluginExportPDF, {
jspdf
})

// 方式2:CDN 安装,只要确保 window.jspdf 存在即可
// VXETable.use(VXETablePluginExportPDF)
```

## Options
Expand All @@ -50,10 +57,11 @@ VXETable.use(VXETablePluginExportPDF)

```javascript
// ...
import VXETable from 'vxe-table'
import VXETablePluginExportPDF from 'vxe-table-plugin-export-pdf'
// ...

VXETablePluginExportPDF.setup({
VXETable.use(VXETablePluginExportPDF, {
// Set default font
fontName: 'SourceHanSans-Normal',
// Font maps
Expand Down Expand Up @@ -102,9 +110,9 @@ export default {
methods: {
exportEvent() {
this.$refs.xTable.exportData({
// fontName: 'SourceHanSans-Normal', // Set the font, default first
filename: 'MyExport',
type: 'pdf',
fontName: 'SourceHanSans-Normal' // Set font
type: 'pdf'
})
}
}
Expand Down
21 changes: 15 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import XEUtils from 'xe-utils'
import { VXETableCore, VxeTableConstructor, VxeTablePropTypes, VxeTableDefines, VxeGlobalInterceptorHandles } from 'vxe-table'
import jsPDF from 'jspdf'
import type jsPDF from 'jspdf'

let vxetable: VXETableCore
let globalJsPDF: any

declare module 'vxe-table' {
export namespace VxeTablePropTypes {
Expand All @@ -19,6 +20,7 @@ interface VXETablePluginExportPDFFonts {
}

interface VXETablePluginExportPDFOptions {
jspdf?: any
fontName?: string;
fonts?: VXETablePluginExportPDFFonts[];
beforeMethod?: Function;
Expand Down Expand Up @@ -95,12 +97,17 @@ function exportPDF (params: VxeGlobalInterceptorHandles.InterceptorExportParams)
}
let fontConf: VXETablePluginExportPDFFonts | null | undefined
const fontName = options.fontName || globalOptions.fontName
if (fonts && fontName) {
fontConf = fonts.find(item => item.fontName === 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 = new jsPDF({ orientation: 'landscape' })
const doc: jsPDF = new (globalJsPDF || (window as any).jspdf)({ orientation: 'landscape' })
// 设置字体
doc.setFontSize(10)
doc.internal.pageSize.width = pdfWidth
Expand Down Expand Up @@ -177,14 +184,16 @@ function pluginSetup (options: VXETablePluginExportPDFOptions) {
* 基于 vxe-table 表格的扩展插件,支持导出 pdf 格式
*/
export const VXETablePluginExportPDF = {
setup: pluginSetup,
config: pluginSetup,
install (vxetable: VXETableCore, options?: VXETablePluginExportPDFOptions) {
// 检查版本
if (!/^(4)\./.test(vxetable.version)) {
console.error('[vxe-table-plugin-export-pdf] Version vxe-table 4.x is required')
}

vxetable.setup({
globalJsPDF = options ? options.jspdf : null

vxetable.config({
export: {
types: {
pdf: 1
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vxe-table-plugin-export-pdf",
"version": "3.1.0",
"version": "4.0.0",
"description": "基于 vxe-table 表格的扩展插件,支持导出 pdf 格式",
"scripts": {
"lib": "gulp build"
Expand Down Expand Up @@ -53,8 +53,8 @@
"sass": "^1.55.0",
"typescript": "^4.6.4",
"vue": "^3.3.4",
"vxe-table": "^4.5.10",
"xe-utils": "^3.5.13"
"vxe-table": "^4.5.18",
"xe-utils": "^3.5.18"
},
"peerDependencies": {
"vxe-table": "^4.5.0"
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface VXETablePluginExportPDFFonts {
}

interface VXETablePluginExportPDFOptions {
jspdf?: any
fontName?: string;
fonts?: VXETablePluginExportPDFFonts[];
beforeMethod?: Function;
Expand Down

0 comments on commit 4dced8f

Please sign in to comment.