-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
341 lines (307 loc) · 9.54 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env node
const { PDFDocument, StandardFonts} = require('pdf-lib');
const fontkit = require('@pdf-lib/fontkit');
const fs = require('fs');
const parse = require('csv-parse/lib/sync');
const open = require('open');
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
var bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer();
const express = require('express')
var path = require('path');
var appDir = path.dirname(require.main.filename);
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
}
const namePoint = new Point(153, 720);
const birthPoint = new Point(294, 720);
const milStartPoint = new Point(440, 720);
const companyPoint = new Point(230, 680);
const reasonPoint = new Point(147, 643);
const phonePoint = new Point(282, 643);
const durationPoint = new Point(455, 643);
const workPlacePoint = new Point(200, 603);
const workStartDatePoint = new Point(101, 534);
const workStartTimePoint = new Point(201, 542);
const workEndTimePoint = new Point(201, 515);
const workMemoPoint = new Point(316, 539);
const chairmanPoint = new Point(327, 100);
const writerPoint = new Point(327, 120);
const signYearPoint = new Point(410, 155);
const signMonthPoint = new Point(455, 155);
const signDatePoint = new Point(489, 155);
const signSignaturePoint = new Point(410, 95);
class UserInfo {
constructor(name, birth, milStartDate, phoneNumber, workPlace, companyName, chairmanName, reason) {
this.name = name;
this.birth = birth;
this.milStartDate = milStartDate;
this.phoneNumber = phoneNumber;
this.workPlace = workPlace;
this.companyName = companyName;
this.chairmanName = chairmanName;
this.reason = reason;
}
}
class WorkInfo {
constructor(date, workMemo, workStartTime = "10:00", workEndTime="19:00") {
this.date = date;
this.workMemo = workMemo;
this.workStartTime = workStartTime;
this.workEndTime = workEndTime;
}
}
class WorkInfos {
constructor(startDate, endDate, workInfoList) {
this.startDate = startDate;
this.endDate = endDate;
this.workInfoList = workInfoList;
}
}
const DefaultPdfTemplatePath = appDir + "/assets/work.pdf"
const pdfTemplate = fs.readFileSync(appDir + "/assets/work.pdf")
const fontFile = fs.readFileSync(appDir + "/assets/NanumBarunGothic.otf")
async function drawUserSignature(page, signature, adjustX, adjustY) {
const scale = signature.scale(0.2);
page.drawImage(signature, {
x: signSignaturePoint.x + adjustX, // firstPage.getWidth() / 2 - 150,
y: signSignaturePoint.y + adjustY, // - pngDims.height,
width: scale.width,
height: scale.height,
})
return page
}
async function drawUserInfo(page, userInfo, adjustX, adjustY) {
var fontSize = 10
page.drawText(userInfo.name, {
x: namePoint.x + adjustX,
y: namePoint.y - fontSize + adjustY,
size: fontSize,
})
page.drawText(userInfo.name, {
x: writerPoint.x + adjustX,
y: writerPoint.y - fontSize + adjustY,
size: fontSize,
})
page.drawText(userInfo.companyName, {
x: companyPoint.x + adjustX,
y: companyPoint.y - fontSize + adjustY,
size: fontSize,
})
page.drawText(userInfo.birth, {
x: birthPoint.x + adjustX,
y: birthPoint.y - fontSize + adjustY,
size: fontSize,
})
page.drawText(userInfo.milStartDate, {
x: milStartPoint.x + adjustX,
y: milStartPoint.y - fontSize + adjustY,
size: fontSize,
})
page.drawText(userInfo.phoneNumber, {
x: phonePoint.x + adjustX,
y: phonePoint.y - fontSize + adjustY,
size: fontSize,
})
page.drawText(userInfo.workPlace, {
x: workPlacePoint.x + adjustX,
y: workPlacePoint.y - fontSize + adjustY,
size: fontSize,
})
page.drawText(userInfo.chairmanName, {
x: chairmanPoint.x + adjustX,
y: chairmanPoint.y - fontSize + adjustY,
size: fontSize,
})
fontSize = 8
page.drawText(userInfo.reason, {
x: reasonPoint.x + adjustX,
y: reasonPoint.y - fontSize + adjustY,
size: fontSize,
})
return page;
}
async function createSheet(pdfTemplatePath, fileName, userInfo, workInfos, year, imgData, adjustX, adjustY) {
const pdfDoc = await PDFDocument.load(fs.readFileSync(pdfTemplatePath))
pdfDoc.registerFontkit(fontkit);
const font = await pdfDoc.embedFont(fontFile)
const signature = await pdfDoc.embedPng(imgData)
var page = pdfDoc.getPage(0)
page.setFont(font);
page = await drawUserSignature(page, signature, adjustX, adjustY);
page = await drawUserInfo(page, userInfo, adjustX, adjustY);
const workInfoLen = workInfos.workInfoList.length
const workDuration = workInfos.workInfoList[0].date + "~" + workInfos.workInfoList[workInfoLen-1].date
console.log("Generating " + workDuration + " pdf result")
var fontSize = 8
page.drawText(workDuration, {
x: durationPoint.x + adjustX,
y: durationPoint.y - fontSize + adjustY,
size: fontSize,
})
const splited = workInfos.workInfoList[workInfoLen-1].date.split('/')
page.drawText(year, {
x: signYearPoint.x + adjustX,
y: signYearPoint.y - fontSize + adjustY,
size: fontSize,
})
page.drawText(splited[0], {
x: signMonthPoint.x + adjustX,
y: signMonthPoint.y - fontSize + adjustY,
size: fontSize,
})
page.drawText(splited[1], {
x: signDatePoint.x + adjustX,
y: signDatePoint.y - fontSize + adjustY,
size: fontSize,
})
for (var i = 0; i < workInfos.workInfoList.length; i++) {
fontSize = 8
const height = 63
var workInfo = workInfos.workInfoList[i]
page.drawText(workInfo.date, {
x: workStartDatePoint.x + adjustX,
y: workStartDatePoint.y - fontSize - height*i + adjustY,
size: fontSize,
})
page.drawText(workInfo.workMemo, {
x: workMemoPoint.x + adjustX,
y: workMemoPoint.y - fontSize - height*i + adjustY,
size: fontSize,
})
fontSize = 6
page.drawText(workInfo.workStartTime, {
x: workStartTimePoint.x + adjustX,
y: workStartTimePoint.y - fontSize - height*i + adjustY,
size: fontSize,
})
page.drawText(workInfo.workEndTime, {
x: workEndTimePoint.x + adjustX,
y: workEndTimePoint.y - fontSize - height*i + adjustY,
size: fontSize,
})
}
fs.writeFileSync(fileName, await pdfDoc.save());
}
async function parseCSV(csvFileName) {
const file = fs.readFileSync(csvFileName)
records = parse(file.toString(), {columns: false, skip_empty_lines:false})
const userInfo = new UserInfo(
name=records[0][1],
birth=records[1][1],
milStartDate=records[2][1],
phoneNumber=records[3][1],
workPlace=records[4][1],
companyName=records[5][1],
chairmanName=records[6][1],
reason=records[7][1],
)
var i = 0;
var workInfos = [];
for(i = 8; i < records.length; i+=5) {
const workWindow = records.slice(i, i+5)
var works = []
for(var workIdx = 0; workIdx < workWindow.length; workIdx++) {
var work = workWindow[workIdx]
works.push(new WorkInfo(work[0], work[1]))
}
workInfos.push(
new WorkInfos(
startDate=workWindow[0][0],
endDate=workWindow[workWindow.length-1][0],
workInfoList=works
));
}
return [userInfo, workInfos]
}
const sleep = (ms) => {
return new Promise(resolve=>{
setTimeout(resolve,ms)
})
}
async function getSignature() {
const app = express()
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(upload.array()); // for parsing multipart/form-data
app.use(express.static(appDir+'/public'))
var data = ""
app.post('/', (req, res) =>{
res.send("Hello")
data = req.body.imgData
})
const server = app.listen(8080, () => {
console.log(`http://localhost:8080 에 접속해 서명부를 위한 싸인을 해주세요`)
})
open("http://127.0.0.1:8080/index.html")
for(;;){
if (data !== "") {
break;
}
await sleep(1000)
}
server.close()
return data
}
function parseArgv() {
const argv = yargs(hideBin(process.argv))
.option('csv', {
alias: 'c',
type: 'string',
description: 'file path of csv file',
default: undefined
})
.option('year', {
alias: 'y',
type: 'string',
description: 'change year in document (default is current year)',
default: new Date().getFullYear().toString()
})
.option('template', {
alias: 't',
type: 'string',
description: 'set custom template of document',
default: DefaultPdfTemplatePath
})
.option('dx', {
type: 'number',
number: true,
description: 'custom template option to adjust cordinate',
default: 0
})
.option('dy', {
type: 'number',
number: true,
description: 'custom template option to adjust cordinate',
default: 0
})
return argv;
}
async function main(){
const signature = await getSignature()
const myArgs = parseArgv()
if (myArgs.argv.csv === undefined) {
console.log("-c csv file path needed")
myArgs.showHelp()
process.exit(1)
}
const csvFileName = myArgs.argv.csv
var year = myArgs.argv.year
const [userInfo, workInfos] = await parseCSV(csvFileName);
for (var i = 0; i < workInfos.length; i++) {
var work = workInfos[i];
const startDate = work.startDate.replace('/', '_')
const endDate = work.endDate.replace('/', '_')
const fileName = [userInfo.name, startDate, endDate].join('_') + ".pdf"
await createSheet(myArgs.argv.template,"./"+fileName, userInfo, work, year, signature, myArgs.argv.dx * 1, myArgs.argv.dy * 1)
}
}
main().then(()=>{
console.log("success")
process.exit(0)
})