Install PDFKit
npm install pdfkitIn app.js Or Controller
// import pdfkit
const PDFDocument = require('pdfkit');
// create an instance
const pdfDoc = new PDFDocument();
// set it to keep stream writing
pdfDoc.pipe(fs.createWriteStream(invoicePath));
// set it to keep streaming response
pdfDoc.pipe(res);
// Design the page
pdfDoc.fontSize(26).text("Invoice");
pdfDoc.fontSize(12).text("------------------------------------------------------------------------------------------------------------");
let grandTotal = 0;
let total = 0;
order.products.forEach(prod => {
total = prod.quantity*prod.product.price;
grandTotal = grandTotal + total;
pdfDoc.fontSize(12).text(`${prod.product.title}`);
pdfDoc.fontSize(16).text(`${prod.quantity} x Rs. ${prod.product.price} = Rs. ${total}`);
});
pdfDoc.fontSize(12).text("------------------------------------------------------------------------------------------------------------");
pdfDoc.fontSize(16).text(`Total: Rs. ${grandTotal}`, { underline: true });
// close it
pdfDoc.end();
Comments
Post a Comment