Skip to content

Instantly share code, notes, and snippets.

@NikhilNanjappa
Created February 12, 2022 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikhilNanjappa/f08e0cef53cd077132daf94351b83f17 to your computer and use it in GitHub Desktop.
Save NikhilNanjappa/f08e0cef53cd077132daf94351b83f17 to your computer and use it in GitHub Desktop.
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