Created
February 12, 2022 14:12
-
-
Save NikhilNanjappa/f08e0cef53cd077132daf94351b83f17 to your computer and use it in GitHub Desktop.
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(result); | |
}); | |
doc.end(); | |
} catch(err) { | |
throw(err); | |
} | |
}; | |
app.get("/pdf", function(req, res) { | |
const docDefinition = { content: "Dummy content" }; | |
generatePdf( | |
docDefinition, | |
function(binary) { | |
res.contentType("application/pdf"); | |
res.send(binary); | |
}, | |
function(error) { | |
res.send("ERROR:" + error); | |
} | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment