nodejs generate pdf and download code example
Example 1: How to Create PDFs in Node.js using PDF Kit
const fs = require("fs");
const PDFDocument = require("./pdfkit-tables");
const patients = require("./patients.json");
const doc = new PDFDocument();
doc.pipe(fs.createWriteStream(`patients.pdf`));
doc
.image("logo.png", 50, 45, { width: 50 })
.fillColor("#444444")
.fontSize(20)
.text("Patient Information.", 110, 57)
.fontSize(10)
.text("725 Fowler Avenue", 200, 65, { align: "right" })
.text("Chamblee, GA 30341", 200, 80, { align: "right" })
.moveDown();
const table = {
headers: ["Name", "Address", "Phone", "Birthday", "Email Address", "Blood Type", "Height", "Weight"],
rows: []
};
for (const patient of patients) {
table.rows.push([patient.name, patient.address, patient.phone, patient.birthday, patient.emailAddress, patient.bloodType, patient.height, patient.weight])
}
doc.moveDown().table(table, 10, 125, { width: 590 });
doc.end();
Example 2: nodejs create pdf
$ npm install pdfkit
const PDFDocument = require('pdfkit');
const fs = require('fs');
var pdfDoc = new PDFDocument;
pdfDoc.pipe(fs.createWriteStream('SampleDocument.pdf'));
pdfDoc.text("From Mon-Sat we will have a 10% discount on selected items!", 150, 150);
pdfDoc
.fillColor('red')
.fontSize(17)
.text("20%", 305, 150);
pdfDoc.end();