-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendSubmission.js
115 lines (106 loc) · 3.16 KB
/
endSubmission.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const pdfkit = require("pdfkit")
const pdfmake = require('pdfmake')
const fs = require("fs")
const dateformat = require('dateformat')
const sanitize = require('sanitize-filename')
const nodemailer = require('nodemailer')
let transporter = nodemailer.createTransport({
type: 'smtp',
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT || 587,
secure: false,
tls: {rejectUnauthorized:false}
})
const fonts = {
Roboto: {
normal: 'fonts/Roboto-Regular.ttf',
bold: 'fonts/Roboto-Medium.ttf',
italics: 'fonts/Roboto-Italic.ttf',
bolditalics: 'fonts/Roboto-MediumItalic.ttf'
}
}
const pdfPrinter = new pdfmake(fonts)
const endSubmission = (user, questionList, bot) => {
fileName = getFileName(user)
makePDF(user, fileName, questionList, bot)
}
const getFileName = (user) => {
date = new Date()
return sanitize(user.id + '_' + user.quest + '_' + dateformat(date, 'dd.mm (HH-MM)') + '.pdf')
}
const makePDF = (user, filename, questionList, bot) => {
stream = fs.createWriteStream("./output/" + fileName)
docDefinition = getDocDefinition(user, questionList)
pdfDoc = pdfPrinter.createPdfKitDocument(docDefinition)
pdfDoc.pipe(stream)
pdfDoc.end()
stream.on('finish', () => {
sendFile(user, fileName, bot)
})
}
const getDocDefinition = (user, questionList) => {
return {
footer: function(currentPage, pageCount) { return {
text : "СТРАНИЦА: " + currentPage.toString(),
alignment: 'right',
fontSize: 6,
margin: [0, 0, 60, 30],
color: '#444'
}
},
content: [{
image: 'img/logo.png',
width: 90,
alignment: 'center'
},
{
text: "JUST FRY DESIGN ",
fontSize: 18,
alignment: 'center',
margin: [0, 30, 0, 30]
},
{
text: [
"Это основной текст для ответов на вопросы."
],
bold: false,
fontSize: 10,
margin: [45, 5, 0, 30]
},
{
fontSize: 10,
style: 'tableExample',
table: {
widths: [150,285],
body: getBodyTable(user,questionList)
}, fillColor: '#eeeeee', layout: {
hLineWidth: function (i, node) {
return 0.5;
},
vLineWidth: function (i, node) {
return 0.1;
},
hLineColor: '#dbdbdb',
vLineColor: '#dbdbdb',
}
}],
styles: {
tableExample: {
margin: [45, 5, 0, 60]
}
}
}
}
const getBodyTable = (user, questionList) => {
return user.ans.map((el,i) => {
question = questionList[i]
return [[{text:question.main, bold: true}, question.type == 'text'? question.extend : ""], user.ans[i]]
})
}
const sendFile = (user, fileName, bot) => {
//stream = fs.createReadStream("./output/" + fileName)
bot.sendDocument(user.id, "./output/" + fileName).then(() => {
fs.unlinkSync("./output/" + fileName)
})
}
module.exports = endSubmission