Node.js Express Example to Generate PDF on Server Using PDFMake.js Library and Send PDF to Client For Download as Attachment in Javascript Full Project For Beginners



npm i pdfmake



And after this inside your node.js project install express library as well



npm i express



And then make a index.js file and copy paste the code which is shown below






'use script'
var pdfmake = require('pdfmake');
const express = require('express')
const app = express()

app.get('/generate-pdf', (req, res) => {

  const doc = new pdfmake({
    Roboto: { normal: new Buffer(require('pdfmake/build/vfs_fonts.js').pdfMake.vfs['Roboto-Regular.ttf'], 'base64') }
  }).createPdfKitDocument({ content: 'test' })
  var chunks = [];
  var result;
  doc.on('readable', function () {
    var chunk;
    while ((chunk = !== null) {
  doc.on('end', function () {
    result = Buffer.concat(chunks);
    res.setHeader('Content-Type', 'application/pdf');
    res.setHeader('Content-disposition', 'attachment; filename=test.pdf');


app.listen(3000, () => console.log('Example app listening on port 3000!'))

Leave a Reply