-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcertificate.js
213 lines (172 loc) · 5.6 KB
/
certificate.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env node
doc = `
Usage:
certificate.js <reasons> [--time=<time>] [--date=<date>] [--profile=<path>] [--output=<path>]
certificate.js -h | --help | --version
Options:
-h --help Show this screen.
--version Show version.
--time=<time> Going out time, foramt: HHhMM
--date=<date> Going out date, format: dd/mm/yyyy
--profile=<path> The path to the profile file.
--output=<path> The output path of the certificate.
Possible reasons:
- travail
- courses
- sante
- famille
- sport
- judiciaire
- missions
`
'use strict'
const QRCode = require('qrcode')
const fs = require('fs')
const PDFLib = require('pdf-lib')
const {docopt} = require('docopt')
const PDFDocument = PDFLib.PDFDocument
const attestationInputPath = './data/certificate.pdf'
const generateQR = async text => {
try {
var opts = {
errorCorrectionLevel: 'M',
type: 'image/png',
quality: 0.92,
margin: 1,
}
return await QRCode.toDataURL(text, opts)
} catch (err) {
console.error(err)
}
}
function pad(str) {
return String(str).padStart(2, '0')
}
function formatDate(date) {
year = date.getFullYear()
month = pad(date.getMonth() + 1) // Les mois commencent à 0
day = pad(date.getDate())
return `${day}/${month}/${year}`
}
function formatTime(date) {
const hour = pad(date.getHours())
const minute = pad(date.getMinutes())
return `${hour}h${minute}`
}
function idealFontSize(font, text, maxWidth, minSize, defaultSize) {
let currentSize = defaultSize
let textWidth = font.widthOfTextAtSize(text, defaultSize)
while (textWidth > maxWidth && currentSize > minSize) {
textWidth = font.widthOfTextAtSize(text, --currentSize)
}
return (textWidth > maxWidth) ? null : currentSize
}
async function generatePdf(existingPdfBytes, profile, reasons) {
const generatedDate = new Date()
const creationDate = formatDate(generatedDate)
const creationHour = formatTime(generatedDate)
const { lastname, firstname, birthday, lieunaissance, address, zipcode, town, datesortie, heuresortie } = profile
const releaseHours = String(heuresortie).substring(0, 2)
const releaseMinutes = String(heuresortie).substring(3, 5)
const data = [
`Cree le: ${creationDate} a ${creationHour}`,
`Nom: ${lastname}`,
`Prenom: ${firstname}`,
`Naissance: ${birthday} a ${lieunaissance}`,
`Adresse: ${address} ${zipcode} ${town}`,
`Sortie: ${datesortie} a ${releaseHours}h${releaseMinutes}`,
`Motifs: ${reasons}`,
].join(' ')
const pdfDoc = await PDFDocument.load(existingPdfBytes)
const page1 = pdfDoc.getPages()[0]
const font = await pdfDoc.embedFont(PDFLib.StandardFonts.Helvetica)
const drawText = (text, x, y, size = 11) => {
page1.drawText(text, { x, y, size, font })
}
drawText(`${firstname} ${lastname}`, 123, 686)
drawText(birthday, 123, 661)
drawText(lieunaissance, 92, 638)
drawText(`${address} ${zipcode} ${town}`, 134, 613)
if (reasons.includes('travail')) {
drawText('x', 76, 527, 19)
}
if (reasons.includes('courses')) {
drawText('x', 76, 478, 19)
}
if (reasons.includes('sante')) {
drawText('x', 76, 436, 19)
}
if (reasons.includes('famille')) {
drawText('x', 76, 400, 19)
}
if (reasons.includes('sport')) {
drawText('x', 76, 345, 19)
}
if (reasons.includes('judiciaire')) {
drawText('x', 76, 298, 19)
}
if (reasons.includes('missions')) {
drawText('x', 76, 260, 19)
}
let locationSize = idealFontSize(font, profile.town, 83, 7, 11)
if (!locationSize) {
console.warn('Le nom de la ville risque de ne pas être affiché correctement en raison de sa longueur. ' +
'Essayez d\'utiliser des abréviations ("Saint" en "St." par exemple) quand cela est possible.')
locationSize = 7
}
drawText(profile.town, 111, 226, locationSize)
if (reasons !== '') {
// Date sortie
drawText(`${profile.datesortie}`, 92, 200)
drawText(releaseHours, 200, 201)
drawText(releaseMinutes, 220, 201)
}
// Date création
drawText('Date de création:', 464, 150, 7)
drawText(`${creationDate} à ${creationHour}`, 455, 144, 7)
const generatedQR = await generateQR(data)
const qrImage = await pdfDoc.embedPng(generatedQR)
page1.drawImage(qrImage, {
x: page1.getWidth() - 170,
y: 155,
width: 100,
height: 100,
})
pdfDoc.addPage()
const page2 = await pdfDoc.getPages()[1]
page2.drawImage(qrImage, {
x: 50,
y: page2.getHeight() - 350,
width: 300,
height: 300,
})
return await pdfDoc.save()
}
async function main(arguments) {
try {
const now = new Date()
const todayDate = formatDate(now)
const nowNow = formatTime(now)
const profileFilePath = arguments['--profile'] || 'profile.json'
const profileData = fs.readFileSync(profileFilePath)
const profile = JSON.parse(profileData)
const reasons = arguments['<reasons>']
const goingOutDate = arguments['--date'] || todayDate
const goingOutHour = arguments['--time'] || nowNow
profile['datesortie'] = goingOutDate
profile['heuresortie'] = goingOutHour
const defaultOutputPath = `certificate-${profile['lastname']}-${goingOutHour}-${reasons}.pdf`
const outputPath = arguments['--output'] || defaultOutputPath
const existingPdfBytes = fs.readFileSync(attestationInputPath)
const pdfBytes = fs.readFileSync(attestationInputPath)
const pdfBlob = await generatePdf(existingPdfBytes, profile, reasons)
fs.writeFileSync(outputPath, pdfBlob)
console.log(`The certificate is ready: ${outputPath}`)
} catch (err) {
console.error('Error', err)
}
}
var arguments = docopt(doc, {
version: '0.1.1rc'
})
main(arguments)