@nestjsforge/pdfme

Configuration (forRootAsync)

Async configuration for PdfmeModule using useFactory, useClass, or useExisting.

Use PdfmeModule.forRootAsync() when configuration depends on async providers like ConfigService.

useFactory

app.module.ts
PdfmeModule.forRootAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: async (config: ConfigService) => {
    const fontPath = config.get<string>('FONT_DIR', './fonts');
    return {
      fonts: {
        NotoSans: {
          data: await readFile(`${fontPath}/NotoSans-Regular.ttf`),
          fallback: true,
        },
      },
    };
  },
})

useClass

@Injectable()
class PdfConfigService implements PdfmeOptionsFactory {
  createPdfmeOptions(): PdfmeModuleOptions {
    return {
      fonts: {
        NotoSans: {
          data: readFileSync('./fonts/NotoSans-Regular.ttf'),
          fallback: true,
        },
      },
    };
  }
}

// In AppModule:
PdfmeModule.forRootAsync({ useClass: PdfConfigService })

useExisting

PdfmeModule.forRootAsync({
  imports: [SharedModule],
  useExisting: AppConfigService,
})