@nestjsforge/pdfme

Working with Fonts

Configure and use custom fonts in PDF generation.

pdfme requires fonts to render text. Register fonts globally in module configuration or override per-call.

Global font registration

import { readFileSync } from 'fs';
import { join } from 'path';

PdfmeModule.forRoot({
  fonts: {
    NotoSans: {
      data: readFileSync(join(__dirname, '../assets/fonts/NotoSans-Regular.ttf')),
      fallback: true,   // Required: at least one font must be fallback: true
    },
    NotoSansBold: {
      data: readFileSync(join(__dirname, '../assets/fonts/NotoSans-Bold.ttf')),
      fallback: false,
    },
    NotoSansItalic: {
      data: readFileSync(join(__dirname, '../assets/fonts/NotoSans-Italic.ttf')),
      fallback: false,
    },
  },
})

You must have exactly one font with fallback: true. This font is used when a schema field does not specify a font name.

Per-call font override

Add extra fonts for specific generations without changing the global config:

const pdf = await this.pdfme.generateWithFonts(
  { template, inputs },
  {
    BrandFont: {
      data: await this.fontsService.getFontBuffer('brand'),
      fallback: false,
    },
  }
);