import * as fs from 'fs' import moment from 'moment' import * as path from 'path' import pdf from 'pdf-creator-node' import { uploadLocalFile } from './storage' import { logger } from './logger' import GeneralUtils from './general' export const generate = async ({ data, fileName = String(moment().unix()), templateName, }: { data: Record fileName: string templateName: string }): Promise => { const html = fs.readFileSync(path.join(__dirname, `/../templates/${templateName}`), 'utf8') const formattedFileName = GeneralUtils.cleanString(fileName) const options = { format: 'A4', orientation: 'portrait', border: '7mm', } const localPath = path.join(__dirname, `/../tmp/${formattedFileName}.pdf`) const document = { html, data, path: localPath, } await pdf.create(document, options) const uploadedData = await uploadLocalFile(localPath) fs.unlink(localPath, logger.error) return Promise.resolve(uploadedData.metadata.mediaLink) }