-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdfgenerator.js
72 lines (56 loc) · 1.65 KB
/
pdfgenerator.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
64
65
66
67
68
69
70
71
72
const fs = require('fs');
const path = require('path');
const util = require('util')
const Handlebars = require('handlebars');
const puppeteer = require('puppeteer');
const data = require('./output.json')
function commaFormat(number) {
return (number/100).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function creditCommaFormat(number) {
return (number/-100).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
(async function() {
try {
const browser = await puppeteer.launch();
const page = await browser.newPage();
const templatePath = path.resolve('financials.html')
var contents = fs.readFileSync(templatePath, 'utf8');
Handlebars.registerHelper('commaFormat', function(number) {
return commaFormat(number);
});
Handlebars.registerHelper('creditCommaFormat', function(number) {
return creditCommaFormat(number);
});
Handlebars.registerHelper('if_eq', function(a, b, opts) {
if (a == b) {
return opts.fn(this);
} else {
return opts.inverse(this);
}
});
Handlebars.registerHelper("debug", function(optionalValue) {
console.log("Current Context");
console.log("====================");
console.log(this);
if (optionalValue) {
console.log("Value");
console.log("====================");
console.log(optionalValue);
}
});
const template = Handlebars.compile(contents)
await page.setContent(template({data}))
await page.emulateMedia('screen');
await page.pdf({
path: 'mypdf.pdf',
format: 'a4',
printBackground: true
});
console.log('done');
browser.close();
process.exit();
} catch (e) {
console.log(e);
}
})();