我想在Ionic/Angular中使用pdfMake生成一个中英文pdf。我已经使用gulp创建了一个新的vfs_fonts.js,并在Build文件夹中替换了原来的文件,还将所有.ttf文件放在了同一Build文件夹中。如果我不定义,它可以输出英文(但不能输出中文(
pdfMake.fonts = {
Roboto: {
normal: XXXXX
..........
},
cwTeXQMing: {
normal: XXXXX
..........
}
};
但一旦我把这些代码放在适当的位置,它就会警告我错误"在虚拟文件系统中找不到文件"pdfmake/build/Roboto Regular.ttf"或在虚拟文件体系中找不到此文件"pdfmake/build/cwTeXOMing.ttf"。我谷歌和搜索SO,但似乎这些解决方案不适合我。请帮助。
import * as pdfMake from 'pdfmake/build/pdfmake.js'; // Could not find a declaration file for
// module 'pdfmake/build/pdfmake.js'
import * as pdfFonts from 'pdfmake/build/vfs_fonts';
pdfMake.vfs = pdfFonts.pdfMake.vfs;
pdfMake.fonts = {
Roboto: {
normal: 'pdfmake/build/Roboto-Regular.ttf',
bold: 'pdfmake/build/Roboto-Regular.ttf',
italics: 'pdfmake/build/Roboto-Regular.ttf',
bolditalics: 'pdfmake/build/Roboto-Regular.ttf'
},
cwTeXQMing: {
normal: 'pdfmake/build/cwTeXQMingZH-Medium.ttf',
bold: 'pdfmake/build/cwTeXQMingZH-Medium.ttf',
italics: 'pdfmake/build/cwTeXQMingZH-Medium.ttf',
bolditalics: 'pdfmake/build/cwTeXQMingZH-Medium.ttf'
}
};
@Component({
selector: 'app-export',
templateUrl: './export.page.html',
styleUrls: ['./export.page.scss'],
})
export class ExportPage {
selectVersion = '';
pdfObj = null;
constructor() {}
async createPdf() {
if (this.selectVersion === 'Chinese') {
const docDefinition = {
pageSize: 'A4',
font: 'cwTeXQMing',
content: [
{
text: '通知我'
, style: 'firstParagraph'
},
],
defaultStyle: {
font: 'cwTeXQMing',
},
styles: {
font: 'cwTeXQMing',
firstParagraph: {
fontSize: 11,
},
}
};
this.pdfObj = pdfMake.createPdf(docDefinition);
} else { // TO OUTPUT ENGLISH
const docDefinition = {
pageSize: 'A4',
content: [
{
text: 'THIS IS ENGLISH PDF'
, style: 'firstParagraph'
},
],
styles: {
firstParagraph: {
fontSize: 11,
},
}
};
this.pdfObj = pdfMake.createPdf(docDefinition);
}
已解决。只需键入文件名"normal:'Roboto Regular.ttf'",而不是"normal:'pdfmake/build/Roboto Regular.ttf"。
我遇到了类似的问题,我只需更新vfs_fonts.js
的CDN链接即可解决。
您可以在查看链接https://www.cdnpkg.com/pdfmake