-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocxGenerator.js
407 lines (364 loc) · 9.47 KB
/
docxGenerator.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import * as docx from 'docx';
import { fetchImageAsBase64 } from './imageHelpers'; // Placeholder for image fetching helper function
const sealImageUrl =
'https://raw.githubusercontent.com/Disfactory/Disfactory/master/backend/doc_resources/seal.png';
const lowerCaseNumber = '〇一二三四五六七八九';
const toLowerCaseNumber = (number) => lowerCaseNumber[number];
const STAFF_EMAIL = {
賴沛蓮: '[email protected]',
};
function getSenderParagraphs(sender = '賴沛蓮') {
const email = STAFF_EMAIL[sender] || '[email protected]';
const context = [
'',
'地址:10049台北市北平東路28號9樓之2',
'電話:02-23920371',
'傳真:02-23920381',
`連絡人:${sender}`,
`電子信箱:${email}`,
'',
'',
];
return context.map(
(text) =>
new docx.Paragraph({
style: 'Normal',
spacing: {
line: 240,
lineRule: docx.LineRuleType.EXACT,
},
children: [
new docx.TextRun({
text,
size: 20,
}),
],
alignment: docx.AlignmentType.RIGHT,
}),
);
}
function getReceiverParagraphs(serealNumber = '00000000') {
const taipeiDateString = new Date().toLocaleDateString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
timeZone: 'Asia/Taipei',
});
const [month, day, year] = taipeiDateString.split('/');
const twYear = parseInt(year, 10) - 1911;
const context = [
'',
'受文者:如正、副本行文單位',
`發文日期:中華民國${twYear}年${month}月${day}日`,
`發文字號:地球公民違字第 ${serealNumber} 號`,
'速別:普通件',
'附件:舉證照片',
'',
];
return context.map(
(text) =>
new docx.Paragraph({
style: 'Normal',
spacing: {
after: 150,
line: 200,
},
children: [
new docx.TextRun({
text,
size: 28,
}),
],
alignment: docx.AlignmentType.LEFT,
}),
);
}
function getSubjectParagraphs(location = '') {
const context = [
`主旨:舉報 ${location} 地號土地疑有違法新增鐵皮廠房情事。`,
'',
'說明:',
];
return context.map(
(text) =>
new docx.Paragraph({
style: 'Normal',
spacing: {
line: 420,
after: 100,
lineRule: docx.LineRuleType.EXACT,
},
children: [
new docx.TextRun({
text,
size: 28,
}),
],
alignment: docx.AlignmentType.LEFT,
}),
);
}
function getContextParagraphs(location = '') {
const context = [
'一、 依工廠管理輔導法第28-1、28-12條辦理。',
// eslint-disable-next-line no-irregular-whitespace
`二、 ${location} 地號土地新發現新增建鐵皮廠房情形,經地球公民基金會志工拍攝存證,如附件一。因懷疑係屬非法建築行為,函請貴府調查處理。若有不法情事,並應依法裁處,請貴府將查處情形,惠知本會。`,
];
return context.map(
(text) =>
new docx.Paragraph({
style: 'Normal',
spacing: {
line: 420,
lineRule: docx.LineRuleType.EXACT,
},
children: [
new docx.TextRun({
text,
size: 28,
}),
],
alignment: docx.AlignmentType.LEFT,
}),
);
}
function getCCParagraphs(legislator = 'XXX', _townName = null) {
let townName;
if (_townName) {
townName = _townName
.replace('臺灣省', '')
.replace('台灣省', '')
.slice(0, 3);
} else {
townName = 'UNKNOWN';
}
const context = [
'',
`正本:${townName}政府`,
`副本:內政部、行政院農委會、經濟部工業局、經濟部中部辦公室、立法委員${legislator}國會辦公室`,
];
return context.map(
(text) =>
new docx.Paragraph({
style: 'Normal',
spacing: {
line: 360,
lineRule: docx.LineRuleType.EXACT,
},
children: [
new docx.TextRun({
text,
size: 24,
}),
],
alignment: docx.AlignmentType.LEFT,
}),
);
}
async function createAttachmentParagraphs(imageUrls) {
/** @type {docx.Paragraph[]} */
const imageParagraphs = await Promise.all(
imageUrls.map((url) => {
return createImageParagraph(url, docx.convertInchesToTwip(0.45));
}),
);
const descriptionParagraphs = new Array(imageUrls.length)
.fill(0)
.map((_, index) => {
return new docx.Paragraph({
style: 'Normal',
spacing: {
line: 120,
},
children: [
new docx.TextRun({
text: `附件 ${toLowerCaseNumber(index + 1)}`,
size: 24,
}),
],
alignment: docx.AlignmentType.LEFT,
});
});
// one description paragraph for each image
const mappedParagraphs = imageParagraphs
.map((paragraph, index) => {
return [descriptionParagraphs[index], paragraph];
})
.flat();
return mappedParagraphs;
}
async function createImageParagraph(imageURL, _width, _height) {
return fetchImageAsBase64(imageURL)
.then((data) => {
const {
base64,
dimension: { width, height },
} = data;
const ratio = width / height;
// calculate image width and height base on given width and height
// if one of them is not given, use the other one to calculate
// if both are not given, use default width and height
let transformWidth, transformHeight;
if (!_width && !_height) {
transformWidth = width;
transformHeight = height;
} else if (!_width) {
transformHeight = _height;
transformWidth = transformHeight * ratio;
} else if (!_height) {
transformWidth = _width;
transformHeight = transformWidth / ratio;
}
console.log(transformWidth, transformHeight, width, height);
const paragraph = new docx.Paragraph({
style: 'Normal',
children: [
new docx.ImageRun({
data: base64,
transformation: {
width: transformWidth,
height: transformHeight,
},
}),
],
});
return paragraph;
})
.catch((err) => {
console.log(err);
});
}
async function generateDocumentParagraphs({
sender = '賴沛蓮',
serialNumber = '00000000',
location = '台北市中山區中山北路一段',
legislator = 'XXX',
townName = '台北市中山區',
imageUrls = [],
}) {
return [
// original
new docx.Paragraph({
style: 'Normal',
children: [
new docx.TextRun({
text: '正本',
}),
],
spacing: {
after: 300,
},
}),
// title
new docx.Paragraph({
style: 'Normal',
children: [
new docx.TextRun({
text: '地球公民基金會 函',
size: 40,
}),
],
spacing: {
after: 300,
},
alignment: docx.AlignmentType.CENTER,
}),
// sender
...getSenderParagraphs(sender),
// receiver
...getReceiverParagraphs(serialNumber),
// subject
...getSubjectParagraphs(location),
// context
...getContextParagraphs(location),
// cc
...getCCParagraphs(legislator, townName),
// seal
await createImageParagraph(sealImageUrl, docx.convertInchesToTwip(0.45)),
// attachments
...(await createAttachmentParagraphs(imageUrls)),
];
}
// Main document generation function
/**
* @param {{
* sender: string,
* serialNumber: string,
* location: string,
* legislator: string,
* townName: string,
* imageUrls: string[],
* }[]} docsData
*/
export async function generate(docsData) {
const groupsOfParagraphs = await Promise.all(
docsData.map(generateDocumentParagraphs),
);
// for each group of paragraphs, add a page break paragraph after it
const breakParagraphs = groupsOfParagraphs.map(() => {
return new docx.Paragraph({
style: 'Normal',
children: [
new docx.TextRun({
text: '',
}),
],
pageBreakBefore: true,
});
});
const paragraphsWithBreaks = groupsOfParagraphs
.map((paragraphs, index) => {
return [...paragraphs, breakParagraphs[index]];
})
.flat();
// remove the end page break
paragraphsWithBreaks.pop();
const doc = new docx.Document({
// creator: 'Your Creator Name',
// title: 'Your Document Title',
// description: 'Your Document Description',
styles: {
default: {
document: {
run: {
font: '標楷體',
size: 12,
},
},
},
paragraphStyles: [
{
id: 'Normal',
name: 'Normal',
basedOn: 'Normal',
next: 'Normal',
run: {
font: '標楷體',
size: 24,
},
},
],
},
sections: [],
});
doc.addSection({
properties: {
type: docx.SectionType.NEXT_PAGE,
page: {
margin: {
top: docx.convertInchesToTwip(1),
right: docx.convertInchesToTwip(1),
bottom: docx.convertInchesToTwip(1),
left: docx.convertInchesToTwip(1),
},
size: {
// A4
width: docx.convertMillimetersToTwip(210),
height: docx.convertMillimetersToTwip(297),
},
},
},
children: paragraphsWithBreaks,
});
return doc;
}