import { Injectable , Inject } from "@nestjs/common" ; import * as fs from "fs" ; import * as pdfGenerator from "html-pdf" ; import { promisify } from "bluebird" ; import { BaseService } from "../../../base.service" ; import { SharedDiToken } from "../../shared/shared.di" ; import { TemplateRenderer } from "../../shared/template-renderer" ; import { PdfFileGenerationException } from "../exceptions/pdf-file-generation.exception" ; import { LoggerDiToken , LoggerService } from "../../logger" ; import { PdfFileTemplateNotResolvedException } from "../exceptions" ; import { FileService , FileDiToken , FileDto , FileParams } from ".." ; /**
 *  Provides API for generating PDF files.
 *  Supports generating as internal storing (in-system FileDto class representation) or outputing generated pdf Buffer data
 *  Provided template names must be kept under `templates` folder.
 */ @ Injectable ( ) export class PdfFileGeneratorService extends BaseService { public constructor ( @ Inject ( FileDiToken . FILE_SERVICE ) private readonly fileService , @ Inject ( SharedDiToken . TEMPLATE_RENDERER ) private readonly renderer , @ Inject ( LoggerDiToken . LOGGER ) private readonly logger ) { super ( ) ; } param { { templateName : string , data : any } } request returns { Buffer } description example public async generateBuffer ( { templateName , data } ) { try { const template = await this . getTemplate ( templateName ) ; const renderedTemplate = this . renderer . render ( template , data ) ; const context = pdfGenerator . create ( renderedTemplate , this . getBasePdfOptions ( ) ) ; return await promisify ( context . toBuffer , { context } ) ( ) ; } catch ( err ) { this . logger . error ( err ) ; throw new PdfFileGenerationException ( ) ; } } param { { templateName : string , data : any } } request returns { FileDto } description example public async generate ( { templateName , data , params } ) { try { const template = await this . getTemplate ( templateName ) ; const renderedTemplate = this . renderer . render ( template , data ) ; const context = pdfGenerator . create ( renderedTemplate , this . getBasePdfOptions ( ) ) ; const readStream = await promisify ( context . toStream , { context } ) ( ) ; const file = await this . fileService . multipartUpload ( readStream , params ) ; return file ; } catch ( err ) { this . logger . error ( err ) ; throw new PdfFileGenerationException ( ) ; } } ignore private async getTemplate ( name ) { try { return ( promisify ( fs . readFile , { multiArgs : true } ) as any ) ( `${ __dirname }/../templates/${ name }.html` , "utf-8" ) ; } catch ( err ) { this . logger . error ( err ) ; throw new PdfFileTemplateNotResolvedException ( name ) ; } } ignore private getBasePdfOptions ( ) { return { format : "A4" , type : "pdf" , border : { top : "20px" , right : "10px" , bottom : "20px" , left : "10px" , } , } ; } }