NodeJS nestjs中的pdfMake库给我一个字体错误

hvvq6cgz  于 2023-05-28  发布在  Node.js
关注(0)|答案(1)|浏览(186)

我想用pdfmake在nestjs中创建一个pdf。由于某种原因,我一直得到这个错误“UnhandledPromiseRejection:这个错误可能是由于抛出了一个没有catch块的async函数,或者是拒绝了一个没有用.catch()处理的promise。promise被拒绝,原因是“File 'Roboto-Medium.ttf' not found in virtual file system.”我试图安装字体,但仍然不工作。

const documentDefinition: any = {
                header: { text: payload.plan_name, alignment: 'center', style: 'header' },
                content: [

                ],
                styles: {
                    header: {
                        fontSize: 18,
                        bold: true,
                        margin: [10, 10, 10, 10]
                    },
                    subheader: {
                        fontSize: 16,
                        bold: true,
                        margin: [0, 10, 0, 5]
                    },
                    tableExample: {
                        margin: [5, 5, 0, 15],
                        padding: [2, 2, 2, 2],
                    },
                    tableHeader: {
                        bold: true,
                        fontSize: 13,
                        color: 'black'
                    }
                },
            };
            payload.all_exercises.forEach((item) => {
                const { day, exercises } = item;
                const bodyData: any = [
                    [{ text: 'Name', color: 'white' }, { text: 'Sets', color: 'white' }, { text: 'Reps', color: 'white' }, { text: 'Rest', color: 'white' }],
                ];
                exercises.map((exercise) => {
                    const { exercise_name, sets, reps, rest = '1min', video_link } = exercise;
                    bodyData.push(
                        [{ text: exercise_name, link: video_link, color: 'blue' }, sets, reps, rest]
                    )
                })
                documentDefinition.content.push(
                    { text: day, style: 'header' },
                    {
                        style: 'tableExample',

                        table: {
                            widths: [200, 70, 70, 70],
                            body: bodyData,
                        },
                        layout: layout,
                    })
            });
            const pdf = pdfMake.createPdf(documentDefinition);
            this.logger.log('Initialized pdfMake')

            new Promise((resolve, reject) => {

                pdf.getBase64(async (data) => {
                    const newPlan = await this.planRepository.save({
                        ...payload,
                        buffer64: data,
                    });
                    return newPlan;
                });
            });
hpcdzsge

hpcdzsge1#

我不得不将Roboto字体添加到应用程序中,并将这些字体导入代码中并传递它们,就像这样:

const fonts = {
      Roboto: {
        normal: 'src/modules/pdf/fonts/Roboto-Regular.ttf',
        bold: 'src/modules/pdf/fonts/Roboto-Medium.ttf',
        italics: 'src/modules/pdf/fonts/Roboto-Italic.ttf',
        bolditalics: 'src/modules/pdf/fonts/Roboto-MediumItalic.ttf',
      },
    };

    const PdfPrinter = require('pdfmake');
    const printer = new PdfPrinter(fonts);

相关问题