Installation
npm install @gboutte/nestjs-pdf
Configuration
@Module({
imports: [
HandlebarsModule.forRoot({
pdfOptions: {},
hbsOptions: {}
)
],
controllers: [],
providers: [],
})
export class AppModule {}
Description | |
---|---|
pdfOptions |
The pdf options can be found on: https://pptr.dev/api/puppeteer.pdfoptions |
hbsOptions |
The handlebars can be found on @gboutte/nestjs-hbs |
chromiumRevision |
The chromium revision that will be downloaded and used by puppeteer to generate the pdf. By default it will be 1095492 |
headless |
Define if you want to use chromium headless or not, or the new version. By default it can be true . Allowed values: true ,false ,"new" |
Usage
You can use the PdfService
, there is currently two methods
Description | Parameters | |
---|---|---|
generatePdfFromHtml |
- html : The html to use to generate the pdf |
|
generatePdfFromTemplateString |
- template : The template string to give to the handlebars service.- parameters : The parameters to use inside the template |
|
generatePdfFromTemplateFile |
- file :The template file to give to the handlebars service. - parameters : The parameters to use inside the template |
Here is an example
@Controller()
export class AppController {
constructor(private readonly pdfService: PdfService) {}
@Get()
@Header('Cache-Control', 'none')
@Header('Content-Type', 'application/pdf')
async getPdf() {
const product = new Product();
product.amount = 53;
const pdf = await this.pdfService.generatePdfFromTemplateString(
'<h1>Title</h1> Product price: {{product.amount}}€',
{ product },
);
return pdf;
}
}
Known issues
- Currently, the pdf only works with fastify adapter