-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·67 lines (58 loc) · 1.89 KB
/
app.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
var _ = require('lodash');
var async = require('asyncawait/async');
var await = require('asyncawait/await');
var Promise = require('bluebird');
var fs = require('fs');
var csvtojson = require("csvtojson").Converter;
var handlebars = require('handlebars');
var pdf = require('html-pdf');
const commandLineArgs = require('command-line-args')
/* all requires above this line */
const argOptions = [
{name: 'jobs', type: String}
];
const args = commandLineArgs(argOptions);
var html = fs.readFileSync('./templates/card.html', 'utf8');
var csvpath = './csvdata/Jobs_Done_TEST.csv';
var converter = Promise.promisifyAll(new csvtojson({delimiter: [',', ';']}));
var pdfOptions = { /*format: 'A4'*/ };
function generatePDF(csv) {
// Filter by job numbers provided in command line args
if (!_.isUndefined(args.jobs)) {
csv = _.filter(csv, function(row) {
return row.Job.indexOf(args.jobs) > -1;
});
}
_.each(csv, function(row) {
// Do some processing
row['Job'] = row['Job'].replace('#', '');
row['Hours'] = row['Bid (Hours)'];
row['TotalAmount'] = row['Full Amount'];
var address = row['Address'].split(',');
row['Address1'] = address[0];
row['Address2'] = address[1];
row['Address3'] = address[2] + ',' + address[3];
row['Address4'] = '';
for (i = 4; i < address.length; i++) {
row['Address4'] += address[i];
if (i !== address.length - 1) {
row['Address4'] += ',';
}
}
//console.log(row);
var chtml = handlebars.compile(html)(row);
var filename = 'STO-' + row.Job + '.pdf';
pdf.create(chtml, pdfOptions).toFile('./generated/' + filename, function(err, res) {
if (err) return console.log(err);
console.log('Generated Invoice..', res.filename);
});
});
}
converter.fromFile(csvpath)
.then(function(csv) {
//console.log(csv);
generatePDF(csv);
})
.catch(function(err) {
console.log(err);
});