Last active
April 28, 2018 03:03
-
-
Save NikhilNanjappa-zz/abfc8ac8db92e7a17ed7f045e5bd855b to your computer and use it in GitHub Desktop.
PDF Store Server
This file contains 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 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 | |
}); | |
}); |
This file contains 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'); | |
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