-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (52 loc) · 1.84 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
56
57
58
59
60
61
62
63
64
65
const excelToJson = require('convert-excel-to-json');
const fs = require('fs-extra');
const moment = require('moment');
const createReport = require('docx-templates').createReport;
const docxConverter = require('custom-soffice-to-pdf');
const result = excelToJson({
sourceFile: 'rapports.xlsx'
});
console.log('Liste des pages du Excel:');
keys = Object.keys(result);
for (i in keys) {
console.log('-' + keys[i]);
}
console.log('\n');
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
async function generate() {
fs.removeSync('result/');
fs.ensureDirSync('result/');
// Première page
const firstPageTemplate = fs.readFileSync('template-RP.docx');
const firstPage = result.RP;
for (i in firstPage) {
if (i != 0) {
const real = firstPage[i];
for (i in letters) {
const letter = letters[i]
if (typeof real[letter] == 'undefined') {
real[letter] = '';
}
}
real.formattedDate = moment(new Date(real.A)).format("DD/MM/YYYY");
const filename = `result/${real.B} ${real.C}`;
const buffer = await createReport({
template: firstPageTemplate,
data: real,
cmdDelimiter: ['{', '}']
});
var isWin = process.platform === "win32";
var customCommand;
if (isWin) {
customCommand = '"C:\\Program Files\\LibreOffice\\program\\soffice"';
} else {
customCommand = 'soffice';
}
const pdfBuffer = await docxConverter(buffer, customCommand)
fs.writeFileSync(filename + '.pdf', pdfBuffer);
// ou remplacer les deux dernières lignes par
// fs.writeFileSync(filename + '.docx', buffer);
}
}
}
generate();