Skip to content

Commit

Permalink
uncomment document preview
Browse files Browse the repository at this point in the history
  • Loading branch information
mgtennant committed Apr 11, 2024
1 parent f257d81 commit 335eb9f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 49 deletions.
36 changes: 18 additions & 18 deletions backend/src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
62 changes: 31 additions & 31 deletions backend/src/admin/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,35 +363,35 @@ export class AdminService {
return header + csvRows.join('\n');
}

// async convertDocxToPdfBuffer(base64Data: string): Promise<Buffer> {
// // 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<Buffer>((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<Buffer> {
// 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<Buffer>((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);
}
});
});
}
}

0 comments on commit 335eb9f

Please sign in to comment.