Last active
February 12, 2022 14:09
-
-
Save NikhilNanjappa/4b75b5f73ec49b089664c9e9c319ce2e to your computer and use it in GitHub Desktop.
generatePdfClientResponse.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pdfMakePrinter = require('pdfmake/src/printer'); | |
function generatePdf(docDefinition, callback) => { | |
try { | |
const fontDescriptors = { ... }; | |
const printer = new pdfMakePrinter(fontDescriptors); | |
const doc = printer.createPdfKitDocument(docDefinition); | |
let chunks = []; | |
const result; | |
doc.on('data', (chunk) => { | |
chunks.push(chunk); | |
}); | |
doc.on('end', () => { | |
result = Buffer.concat(chunks); | |
callback('data:application/pdf;base64,' + result.toString('base64')); | |
}); | |
doc.end(); | |
} catch(err) { | |
throw(err); | |
} | |
}; | |
app.get("/pdf", function(req, res) { | |
const docDefinition = { content: "Dummy content" }; | |
generatePdf( | |
docDefinition, | |
function(base64String) { | |
res.send(base64String); | |
}, | |
function(error) { | |
res.send("ERROR:" + error); | |
} | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment