我发现以下解决方案使用 require+import 组合,并且在 tsconfig.json 中没有任何特殊更改。我在我的例子中使用了 pdfmake-unicode,但我想 vfs_fonts 应该类似地工作。
// 在 pdfmake 上:0.1.38,pdfmake-unicode:0.0.1
Install:
npm install pdfmake --save
npm install pdfmake-unicode --save
npm install @types/pdfmake --save-dev
Import:
const pdfMakeX = require('pdfmake/build/pdfmake.js');
const pdfFontsX = require('pdfmake-unicode/dist/pdfmake-unicode.js');
pdfMakeX.vfs = pdfFontsX.pdfMake.vfs;
import * as pdfMake from 'pdfmake/build/pdfmake';
产生:
downloadTest = () => {
const docDefinition = {
content: [{
table: {
headerRows: 1,
widths: ['*', 'auto', 100, '*'],
body: [
['First', 'Second', 'Third', 'Последняя'],
['Value 1', 'Value 2', 'Value 3', 'Value 4'],
[{ text: 'Bold value', bold: true }, 'Val 2', 'Val 3', 'Чё']
]
}
}]
};
pdfMake.createPdf(docDefinition).download('pdfmake.pdf');
}
Use:
<button onClick={this.downloadTest}>Download</button>