node js pdf code example

Example 1: 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();

Example 2: node js serve pdf file

app.use('/pdf', express.static(__dirname + '/pathToPDF'));

Example 3: node js serve pdf file

res.setHeader('Content-Disposition', 'inline;filename=yolo.pdf')

Example 4: node js serve pdf file

res.setHeader('Content-Type', 'application/pdf')