报错问题
通过npm安装pdfMake后,在.vue文件里面调用如下:
import "pdfmake/build/pdfmake";
import "pdfmake/build/vfs_fonts";
export default {
data() {
return {
};
},
created(){
this.pdfTest();
},
methods: {
pdfTest(){
var docDefinition = { content: 'This is an sample PDF printed with pdfMake' };
console.log(pdfMake.vfs);
pdfMake.createPdf(docDefinition).open();
}
}
}
发现无法生成PDF,并且报错:
[Vue warn]: Error in created hook: “File ‘Roboto-Regular.ttf’ not found in virtual file system”
File ‘Roboto-Regular.ttf’ not found in virtual file system
解决方法
通过console.log(pdfMake.vfs);打印log来看,vfs为空,应该是vfs_fonts.js里面的方法没有执行,打开vfs_fonts.js文件,将this.pdfMake修改为pdfMake:
this.pdfMake = this.pdfMake || {}; this.pdfMake.vfs = {...}
改为:
pdfMake = pdfMake || {}; pdfMake.vfs = {...}
然后就解决了。