Skip to content

Instantly share code, notes, and snippets.

@NikhilNanjappa-zz
Last active April 28, 2018 03:03
Show Gist options
  • Save NikhilNanjappa-zz/abfc8ac8db92e7a17ed7f045e5bd855b to your computer and use it in GitHub Desktop.
Save NikhilNanjappa-zz/abfc8ac8db92e7a17ed7f045e5bd855b to your computer and use it in GitHub Desktop.
PDF Store Server
const docDefinition = {
content: ['This will show up in the file created']
};
generatePdf(docDefinition, (response) => {
// doc successfully created
res.json({
status: 200,
data: response
});
}, (error) => {
// doc creation error
res.json({
status: 400,
data: error
});
});
const pdfMakePrinter = require('pdfmake/src/printer');
const fs = require('fs');
function generatePdf(docDefinition, successCallback, errorCallback) => {
try {
const fontDescriptors = { ... };
const printer = new pdfMakePrinter(fontDescriptors);
const doc = printer.createPdfKitDocument(docDefinition);
doc.pipe(
fs.createWriteStream('docs/filename.pdf').on("error", (err) => {
errorCallback(err.message);
})
);
doc.on('end', () => {
successCallback("PDF successfully created and stored");
});
doc.end();
} catch(err) {
throw(err);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment