Skip to content

How to use custom font family in table cell? #1667

@MichelaCucchiSpot

Description

@MichelaCucchiSpot

Hi,
I'd like to create tables with bold or italic text only in some cells.
Is this possible?

This is my code. It seems that

font: {
family: 'OpenSans-Italic',
src: `${fontsPath}OpenSans-Italic.ttf`,
},

has no effect.

What am I doing wrong?

const doc = new PDFDocument({
        size: 'A4',
        bufferPages: true,
        margins: { top: margin, right: margin, bottom: margin, left: margin },
        layout: 'portrait',
      });
      doc.registerFont('OpenSans', `${fontsPath}OpenSans-Regular.ttf`);
      doc.registerFont('OpenSans-Bold', `${fontsPath}OpenSans-Bold.ttf`);
      doc.registerFont('OpenSans-Italic', `${fontsPath}OpenSans-Italic.ttf`);
      doc.registerFont('OpenSans-Medium', `${fontsPath}OpenSans-Medium.ttf`);
      doc.fontSize(8);


      doc.font('OpenSans-Bold').fontSize(12);
      doc.table({
        defaultStyle: {
          border: false,
        },
        columnStyles: ['*', 200],
        data: [
          [
            {
              align: { x: 'left', y: 'top' },
              text: `${marker('E-BIKE REPORT')} `,
            },
            {
              font: {
                family: 'OpenSans-Italic',
                src: `${fontsPath}OpenSans-Italic.ttf`,
              },
              align: { x: 'right', y: 'top' },
              text: moment().format('DD/MM/YYYY'),
            },
          ],
        ],
      });
      doc.moveDown(1.0);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions