From 335eb9fcf720bab488414b56900831946b32c715 Mon Sep 17 00:00:00 2001 From: mgtennant <100305096+mgtennant@users.noreply.github.com> Date: Thu, 11 Apr 2024 11:26:11 -0700 Subject: [PATCH] uncomment document preview --- backend/src/admin/admin.controller.ts | 36 ++++++++-------- backend/src/admin/admin.service.ts | 62 +++++++++++++-------------- 2 files changed, 49 insertions(+), 49 deletions(-) diff --git a/backend/src/admin/admin.controller.ts b/backend/src/admin/admin.controller.ts index 67a8ac6b..5d931c08 100644 --- a/backend/src/admin/admin.controller.ts +++ b/backend/src/admin/admin.controller.ts @@ -61,24 +61,24 @@ export class AdminController { }); } - // @Get('preview-template/:id') - // async previewTemplate(@Param('id') id: number, @Res() res) { - // try { - // const dtObject = await this.adminService.downloadTemplate(id); - // const base64Data = dtObject.the_file; - // const pdfBuffer = await this.adminService.convertDocxToPdfBuffer(base64Data); - // const streamableFile = new stream.PassThrough(); - // streamableFile.end(pdfBuffer); - // res.set({ - // 'Content-Type': 'application/pdf', - // 'Content-Disposition': 'attachment; filename=file.pdf', - // }); - // streamableFile.pipe(res); - // } catch (error) { - // console.error('Error:', error); - // res.status(500).send('Internal Server Error'); - // } - // } + @Get('preview-template/:id') + async previewTemplate(@Param('id') id: number, @Res() res) { + try { + const dtObject = await this.adminService.downloadTemplate(id); + const base64Data = dtObject.the_file; + const pdfBuffer = await this.adminService.convertDocxToPdfBuffer(base64Data); + const streamableFile = new stream.PassThrough(); + streamableFile.end(pdfBuffer); + res.set({ + 'Content-Type': 'application/pdf', + 'Content-Disposition': 'attachment; filename=file.pdf', + }); + streamableFile.pipe(res); + } catch (error) { + console.error('Error:', error); + res.status(500).send('Internal Server Error'); + } + } @Get('download-template/:id') async downloadTemplate(@Param('id') id: number, @Res() res) { diff --git a/backend/src/admin/admin.service.ts b/backend/src/admin/admin.service.ts index 4c040287..00a0d3aa 100644 --- a/backend/src/admin/admin.service.ts +++ b/backend/src/admin/admin.service.ts @@ -363,35 +363,35 @@ export class AdminService { return header + csvRows.join('\n'); } - // async convertDocxToPdfBuffer(base64Data: string): Promise { - // // Decode base64 string - // const buffer = Buffer.from(base64Data, 'base64'); - - // // Write buffer to temporary file - // const tempFilePath = './temp.docx'; - // fs.writeFileSync(tempFilePath, buffer); - - // // Convert DOCX to HTML - // const { value } = await mammoth.convertToHtml({ path: tempFilePath }); - // const htmlContent = value; - - // // Set options for html-pdf - // const options: pdf.CreateOptions = { - // format: 'Letter', // Set the PDF format (e.g., 'A4', 'Letter', etc.) - // base: `file://${__dirname}/`, // Set the base path for local file references - // }; - - // return new Promise((resolve, reject) => { - // // Convert HTML to PDF - // pdf.create(htmlContent, options).toBuffer((err, buffer) => { - // if (err) { - // reject(err); - // } else { - // // Remove temporary file - // fs.unlinkSync(tempFilePath); - // resolve(buffer); - // } - // }); - // }); - // } + async convertDocxToPdfBuffer(base64Data: string): Promise { + // Decode base64 string + const buffer = Buffer.from(base64Data, 'base64'); + + // Write buffer to temporary file + const tempFilePath = './temp.docx'; + fs.writeFileSync(tempFilePath, buffer); + + // Convert DOCX to HTML + const { value } = await mammoth.convertToHtml({ path: tempFilePath }); + const htmlContent = value; + + // Set options for html-pdf + const options: pdf.CreateOptions = { + format: 'Letter', // Set the PDF format (e.g., 'A4', 'Letter', etc.) + base: `file://${__dirname}/`, // Set the base path for local file references + }; + + return new Promise((resolve, reject) => { + // Convert HTML to PDF + pdf.create(htmlContent, options).toBuffer((err, buffer) => { + if (err) { + reject(err); + } else { + // Remove temporary file + fs.unlinkSync(tempFilePath); + resolve(buffer); + } + }); + }); + } }