Skip to content

Instantly share code, notes, and snippets.

@officer-rosmarino
Last active April 19, 2018 14:45
Show Gist options
  • Save officer-rosmarino/1f223fd8e5ec55ba51a4cb81c4c0a602 to your computer and use it in GitHub Desktop.
Save officer-rosmarino/1f223fd8e5ec55ba51a4cb81c4c0a602 to your computer and use it in GitHub Desktop.
Generate a PDF on the server and send it to the client to be downloaded
'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 = doc.read(9007199254740991)) !== null) {
chunks.push(chunk);
}
});
doc.on('end', function () {
result = Buffer.concat(chunks);
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-disposition', 'attachment; filename=test.pdf');
res.send(result);
});
doc.end();
});
app.listen(3000, () => console.log('Example app listening on port 3000!'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment