feat: complete day 10
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const pdf = require("html-pdf-node");
|
||||
|
||||
router.get("/:code", async (req, res) => {
|
||||
const { amount = 1, service = "software service" } = req.query;
|
||||
|
||||
// Read the invoice template
|
||||
const templatePath = path.join(__dirname, "../invoice.html");
|
||||
let html = fs.readFileSync(templatePath, "utf8");
|
||||
|
||||
// Replace placeholders in the template
|
||||
html = html
|
||||
.replace("Website design", service)
|
||||
.replace("$300.00", `$${amount}.00`)
|
||||
.replace("Total: $385.00", `Total: $${amount}.00`);
|
||||
|
||||
// Generate PDF
|
||||
let file = { content: html };
|
||||
pdf.generatePdf(file, { format: "A4" }).then((pdfBuffer) => {
|
||||
res.setHeader("Content-Type", "application/pdf");
|
||||
res.setHeader("Content-Disposition", "attachment; filename=invoice.pdf");
|
||||
res.send(pdfBuffer);
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user