-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (50 loc) · 1.71 KB
/
Copy pathindex.js
File metadata and controls
57 lines (50 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* dynamic-handlebar-pdf
*
*
* Copyright (c) 2017 Navjot Dhanawat
* Licensed under the MIT license.
*/
/**
* Dynamic handlebars pdf is used to create pdf from handlebar templates.
* @param {document, options}
* @return {callback}
*/
var Handlebars = require('handlebars'),
pdf = require('html-pdf');
// Handlebar helper support
module.exports.registerHelper = (conditionName, callback) => {
Handlebars.registerHelper(conditionName, callback);
};
module.exports.create = (document, options) => {
// Compile handlebar template
return new Promise((resolve, reject) => {
if (!document || !document.template || !document.context) {
reject(new Error("Some, or all, options are missing."));
}
if (document.type !== 'buffer' && !document.path) {
reject(new Error("Please provide path parameter to save file or if you want buffer as output give parameter type = 'buffer'"));
}
var html = Handlebars.compile(document.template)(document.context);
var pdfPromise = pdf.create(html, options);
if (document.type === 'buffer') {
// Create PDF from html template generated by handlebars
//Output will be buffer
pdfPromise.toBuffer((err, buff) => {
if (!err)
resolve(buff);
else
reject(err);
});
} else {
// Create PDF from html template generated by handlebars
// Output will be PDF file
pdfPromise.toFile(document.path, (err, res) => {
if (!err)
resolve(res);
else
reject(err);
});
}
});
};