-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.js
63 lines (57 loc) · 1.54 KB
/
generator.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
56
57
58
59
60
61
62
63
const handlebars = require('handlebars')
const html2pdf = require('html2pdf.js')
const { jsPDF } = require("jspdf")
/**
* New class declaration that is supported by WebPack.
*
* Generator class that takes care of filling data into template and
* generating a client-side PDF.
*
* TO-DO: make support for server-side.
*
* @param template
* @param data
* @constructor
*/
function Generator(template, data) {
this.template = template
this.renderedTemplate = null
this.data = data
}
/**
* Simple getter for template property.
* @returns {*}
*/
Generator.prototype.getTemplate = function () {
return this.template
}
/**
* Simple getter for data property.
* @returns {*}
*/
Generator.prototype.getData = function () {
return this.data
}
/**
* Simple function that fills HTML (handlebars) template with provided data.
*/
Generator.prototype.renderTemplate = function () {
let templateHandlebars = handlebars.compile(this.template)
this.renderedTemplate = templateHandlebars(this.data)
}
/**
* Function that generates and downloads PDF created from a HTML template.
*
* generateRenderedPdf is a switch that generates rendered template or non-filled template.
*
* @returns {Promise<void>}
*/
Generator.prototype.downloadPDF = async function (generateRenderedPdf = true) {
// function that prompts user to download a pdf
// must be ran in a browser
if (generateRenderedPdf)
html2pdf().from(this.renderedTemplate).save()
else
html2pdf().from(this.template).save()
}
module.exports = Generator;