Skip to content

Instantly share code, notes, and snippets.

@fitsum
Forked from InsilicoSoft/stream-pdf-from-nodejs.js
Created January 25, 2024 10:42
Show Gist options
  • Save fitsum/affc7fecc36b9e3b5c14d15fc3f722a7 to your computer and use it in GitHub Desktop.
Save fitsum/affc7fecc36b9e3b5c14d15fc3f722a7 to your computer and use it in GitHub Desktop.
Stream local pdf file from nodejs (restify) server to client
server.get('/downloadPdf/:fileData', function (req, res) {
// config
var fileData = Buffer.from(req.params.fileData, 'base64');
var menuData = JSON.parse(fileData.toString());
var userName = menuData.userName;
var menuName = slug(menuData.menuName);
var fileName = userName + "-" + menuName + PDF_EXT;
var filePath = PDF_PATH + fileName;
// process headers
res.writeHead(200, {
"Content-Type": 'application/octet-stream',
"Content-Disposition": "attachment; filename=" + fileName
});
// process Data
var readStream = fileSystem.createReadStream(filePath);
readStream.pipe(res);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment