Vue.js使用pdfMake时出现字体找不到错误

报错问题

通过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

解决方法

通过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 = {...}

然后就解决了。

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值