-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (61 loc) · 1.77 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
const pdfreader = require('pdfreader')
function main() {
try {
let cursor = null
let currentBank = null
let banks = []
new pdfreader.PdfReader().parseFileItems('Tabela.pdf', function (err, item) {
if (err) throw err
if (!item) { // EOF
console.log('banks', JSON.stringify(banks, null, 4))
console.log('Finished!')
process.exit(0)
} else if (item.page) { // Page start
cursor = null
currentBank = null
} else if (item.text) { // Some page content
if (item.text === 'SEGMENTO') { // Next item will be the first bank code for this page
cursor = 'code'
currentBank = null
} else if (cursor) {
switch (cursor) {
case 'code':
// Invalid bank code. Some bank segment rows are split into two items or the table is over
if (!Number.isInteger(parseInt(item.text))) {
if (currentBank && item.text !== 'FONTE UNICAD.') currentBank.segment += item.text
return
}
// Adds latest bank object to array and start building a new one
if (currentBank) banks.push(currentBank)
currentBank = {}
currentBank[cursor] = item.text
cursor = 'cnpj'
break
case 'cnpj':
currentBank[cursor] = item.text
cursor = 'name'
break
case 'name':
currentBank[cursor] = item.text
cursor = 'segment'
break
case 'segment':
// Some bank names are split into two items. If it is all uppercase, that's probably the bank name remainings
if (item.text === item.text.toUpperCase()) {
currentBank.name += item.text
return
}
currentBank[cursor] = item.text
cursor = 'code'
break
default:
return
}
}
}
})
} catch (err) {
console.log('err:', err)
}
}
main()