@nestjsforge/pdfme

Serving via HTTP

Patterns for serving generated PDFs via NestJS HTTP controllers.

Inline viewing

Serve the PDF to be displayed inline in the browser:

@Get('invoice/:id')
async getInvoice(
  @Param('id') id: string,
  @Res() res: Response,
): Promise<void> {
  const pdf = await this.invoices.generate(id);
  res.setHeader('Content-Type', 'application/pdf');
  res.setHeader('Content-Disposition', `inline; filename="invoice-${id}.pdf"`);
  res.setHeader('Content-Length', pdf.byteLength);
  res.end(Buffer.from(pdf));
}

Force download

res.setHeader('Content-Disposition', `attachment; filename="invoice-${id}.pdf"`);

StreamableFile (NestJS v10+)

import { StreamableFile } from '@nestjs/common';
import { Readable } from 'stream';

@Get('invoice/:id')
async getInvoice(@Param('id') id: string): Promise<StreamableFile> {
  const pdf = await this.invoices.generate(id);
  const stream = Readable.from(Buffer.from(pdf));
  return new StreamableFile(stream, {
    type: 'application/pdf',
    disposition: `attachment; filename="invoice-${id}.pdf"`,
  });
}