Skip to content

Instantly share code, notes, and snippets.

@NikhilNanjappa
Last active February 12, 2022 14:09
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/4b75b5f73ec49b089664c9e9c319ce2e to your computer and use it in GitHub Desktop.
Save NikhilNanjappa/4b75b5f73ec49b089664c9e9c319ce2e to your computer and use it in GitHub Desktop.
generatePdfClientResponse.js
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