-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (48 loc) · 1.74 KB
/
index.js
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
//handlebars-to-pdf
var Handlebars = require('handlebars');
var PDF = require('html-pdf');
module.exports = {
create: function(options, callback){
//validate callback
if(typeof callback !== "function"){
throw new Error("No callback function supplied.");
return;
}
//check options have been supplied correctly. Required template_html and template_data, optional layout_settings
if(!options || typeof options !== "object"){
callback(new Error("`options` is malformatted or doesn't exist."), null);
return;
}
if(!options.template_html || typeof options.template_html !== "string"){
callback(new Error("`options.template_html` is malformatted or doesn't exist."), null)
return;
}
if(!options.template_data || typeof options.template_data !== "object"){
callback(new Error("`options.template_data` is malformatted or doesn't exist."), null)
return;
}
var output_layout = options.layout_settings || {
"format": "A4",
"orientation": "portrait",
"border": {
"top": "0.5in",
"right": "0.5in",
"bottom": "0.5in",
"left": "0.5in"
},
};
//compile handlebars template and data into raw HTML
var output_html = Handlebars.compile(options.template_html)(options.template_data);
//output final PDF
PDF.create(
output_html,
output_layout
).toBuffer(function(error, buffer){
if(error){
callback(error);
} else {
callback(null, buffer);
}
});
}
}