-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·214 lines (163 loc) · 5.82 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
const pptr = require('puppeteer-core');
const fs = require('fs');
const fse = require('fs-extra');
const pdfMerge = require('pdf-merge');
const Progress = require('progress');
const tempy = require('tempy');
const globby = require('globby');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const { customAlphabet } = require('nanoid');
const { numbers, uppercase } = require('nanoid-dictionary');
const nanoid = customAlphabet(numbers + uppercase, 12);
// globals
const BATCH_SIZE = 90; // batch size to make
const TOTAL_NUMBER_OF_TICKETS = 25000;
// const TOTAL_NUMBER_OF_TICKETS = 90;
const PAGE_SIZE = 30; // size of a page.
const TEMP_DIR = tempy.directory();
const normalizeCSS = fs.readFileSync('./node_modules/normalize.css/normalize.css', 'utf8');
const paperCSS = fs.readFileSync('./node_modules/paper-css/paper.min.css', 'utf8');
const jsBarcode = fs.readFileSync('./node_modules/jsbarcode/dist/JsBarcode.all.min.js', 'utf8');
const template = fs.readFileSync('template.html', 'utf8');
const LEN = Number(TOTAL_NUMBER_OF_TICKETS).toString().length;
// base64 logos
/*
const imaLogo = fs.readFileSync('./lib/ima-logo.jpg').toString('base64');
const corusLogo = fs.readFileSync('./lib/corus-logo.png').toString('base64');
*/
const sanity = [];
function genId() {
let id = nanoid();
while (sanity.includes(id)) {
id = nanoid();
}
sanity.push(id);
return id;
}
function generateIdentifiers() {
console.log('generating identifiers');
let i = TOTAL_NUMBER_OF_TICKETS;
while (i--) {
genId();
}
console.log(`done generating ${sanity.length} identifiers. Sorting...`);
sanity.sort();
console.log('done sorting identifiers');
}
async function makeItem(ident) {
// make a data URL out of the QR code.
// const url = await QR.toDataURL(String(id));
// const ima = `data:image/jpeg;base64,${imaLogo}`;
// const corus = `data:image/png;base64,${corusLogo}`;
return `
<div class="label">
<svg
class="barcode"
jsbarcode-format="auto"
jsbarcode-width="1"
jsbarcode-height="40"
jsbarcode-fontsize="12"
jsbarcode-value="${ident}"
jsbarcode-textmargin="0">
</svg>
</div>
`;
}
const headless = true;
function extractNumberFromFileName(fname) {
const last = fname.split('-').pop().replace('.txt', '');
return parseInt(last, 10);
}
async function createTemplatesFromRange(start, stop, statusbar) {
console.log('Creating text templates');
const items = [];
for (let i = start; i <= stop; i += 1) {
items.push(makeItem(sanity[i]));
// after chunk is done, let's write it to disk
if ((i % PAGE_SIZE) === 0) {
const chunks = await Promise.all(items);
// write to temp file
await fse.writeFile(`${TEMP_DIR}/chunks-temp-${i}.txt`, chunks.join(''), 'utf8');
// reset the items
items.length = 0;
}
statusbar.tick();
}
}
function leftPad(num, len) {
const { length } = Number(num).toString();
if (length < len) {
return '0'.repeat(len - length).concat(num);
}
return num;
}
async function renderTemplatesFromRange(i, j, statusbar) {
const files = [];
const globs = await globby(`${TEMP_DIR}/chunks*.txt`);
console.log('Sorting templates into sensible ordering');
// order paths in a sensible order
globs.sort((a, b) => ((extractNumberFromFileName(a) > extractNumberFromFileName(b)) ? 1 : -1));
const browser = await pptr.launch({ executablePath: '/usr/bin/chromium-browser', headless });
let index = 0;
for (const glob of globs) {
const sheet = await fse.readFile(glob, 'utf8');
const content = template
.replace('INJECT_NORMALIZE', normalizeCSS)
.replace('INJECT_PAPER_CSS', paperCSS)
.replace('INJECT_JSBARCODE', jsBarcode)
.replace('INJECT_CONTENT', `<div class="sheet">${sheet}</div>`);
const page = await browser.newPage();
await page.setContent(content);
await page.addScriptTag({ content: 'JsBarcode(".barcode").init();' });
const path = `${TEMP_DIR}/tickets-${index++}.pdf`;
files.push(path);
await page.pdf({ path, format: 'letter' });
await page.close();
statusbar.tick();
}
await browser.close();
console.log('Consolidating all paths into a single path.');
const fname = `output/${leftPad(i, LEN)}-${leftPad(j, LEN)}-tickets.pdf`;
await pdfMerge(files, { output: fname });
console.log(`Zipping PDFs into ${fname}.xz`);
// await xzPDF(fname);
// delete all .txt files
console.log(`Deleting ${globs.length} txt files`);
await Promise.all(globs.map((path) => fse.unlink(path)));
// delete all .pdf files
console.log(`Deleting ${files.length} pdf files`);
await Promise.all(files.map((path) => fse.unlink(path)));
}
async function makeBatchOfTickets(i, j, statusbar) {
console.log(`Making a batch of tickets (${i} - ${j})`);
await createTemplatesFromRange(i, j, statusbar);
await renderTemplatesFromRange(i, j, statusbar);
}
(async () => {
console.log('Starting PDF generation...');
try {
// generate sanity ids
generateIdentifiers();
console.log('Starting creating text templates...');
const statusbar = new Progress('[:bar] :percent :etas', { total: (TOTAL_NUMBER_OF_TICKETS * 1.9) });
let i = 1;
let j = i + BATCH_SIZE;
while (i <= TOTAL_NUMBER_OF_TICKETS) {
await makeBatchOfTickets(i, j, statusbar);
i += BATCH_SIZE;
j = i + BATCH_SIZE;
}
console.log('merging files with pdftk');
const { stdout, stderr } = await exec('pdftk output/*.pdf cat output tickets.pdf');
if (stderr) {
console.error(`error: ${stderr}`);
}
console.log(`Number of files ${stdout}`);
console.log('Writing identifiers out to a CSV file..');
await fs.promises.writeFile('identifiers.csv', 'uuid\n'.concat(sanity.join('\n')), 'utf8');
console.log('Done!');
} catch (e) {
console.log('e:', e);
}
})();