manniru
9/3/2017 - 3:13 PM

Generate PDF File from Express NodeJS Server

Generate PDF File from Express NodeJS Server

// DEPENDANCIES "express": "^4.15.4", "pdfmake": "^0.1.32"

const express = require('express')
const app = express()

app.get('/', function (req, res) {



var fs = require('fs');

var fonts = {
	Roboto: {
		normal: 'fonts/Roboto-Regular.ttf',
		bold: 'fonts/Roboto-Medium.ttf',
		italics: 'fonts/Roboto-Italic.ttf',
		bolditalics: 'fonts/Roboto-MediumItalic.ttf'
	}
};

var PdfPrinter = require('pdfmake/src/printer');
var printer = new PdfPrinter(fonts);

//var printer = new pdfMakePrinter(fonts)
//var doc = printer.createPdfKitDocument(definition)


var dd = {
content: [
    'Alhamdulillah',
    'Bismillahir Rahamanir Rahim'
]
}
var pdfDoc = printer.createPdfKitDocument(dd);

//pdfDoc.pipe(fs.createWriteStream('basics.pdf')).on('finish',function(){ });
//pdfDoc.end();


var chunks = []
var result

pdfDoc.on('data', function (chunk) { chunks.push(chunk) });
pdfDoc.on('end', function () {
      result = Buffer.concat(chunks)
      res.contentType('application/pdf')
      res.send(result)
    });
    pdfDoc.end()



  //res.send('Hello World 5')
})

app.listen(3000, function () {
  console.log('Example app listening on port 3000!')
})