-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (108 loc) · 4.23 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
//Setup
const express = require('express');
const multer = require('multer');
const { PdfReader } = require("pdfreader");
const { TableParser } = require("pdfreader");
const app = express();
const port = 3000;
//Log port
app.listen(port, function () {
console.log("App using port " + port);
});
//Store PDF in memory
const upload = multer({ storage: multer.memoryStorage() });
//Base page with form (/)
app.get("/", function (req, res) {
res.sendFile(__dirname + "/form.html");
});
//Take form input, store as a variable "schedule," parse the pdf, and go to upload page (/app)
var schedule;
var currentLine = [];
var currentCell = [];
var previousCharacterX = 0;
const tableParser = new TableParser();
var table;
var pageNumber = 0;
app.post("/app", upload.single("pdfUpload"), function (req, res) {
schedule = req.file;
console.log(schedule.originalname);
//Get PDF data
new PdfReader().parseBuffer(schedule.buffer, function (err, item) {
if (!item) {
if (currentCell.length) { //(Have to get last cell)
var cellString = currentCell.map(function (character) {
return character.text;
}).join("");
var cellAsItem = {
x: currentCell[0].x,
y: currentCell[0].y + (pageNumber * 1000),
text: cellString,
};
tableParser.processItem(cellAsItem);
table = tableParser.getMatrix();
table.forEach(function (row) {
row.forEach(function (cellArray) {
var cellText1 = cellArray[0].text;
var cellText2 = cellArray[1].text;
var cellText3 = cellArray[2].text;
var cellText4 = cellArray[3].text;
var cellText5 = cellArray[4].text;
console.log(cellText1 + " | " + cellText2 + " | " + cellText3 + " | " + cellText4 + " | " + cellText5);
});
});
}
} else if (item.text) {
if (currentLine.length && currentLine[0].y == item.y) { //Same row
if (item.x - previousCharacterX <= 1) { //Same cell
currentCell.push(item);
} else { //New cell
var cellString = currentCell.map(function (character) {
return character.text;
}).join("");
var cellAsItem = {
x: currentCell[0].x,
y: currentCell[0].y + (pageNumber * 1000),
text: cellString,
};
tableParser.processItem(cellAsItem);
currentCell = [];
currentCell.push(item);
}
} else { //New row
if (currentCell.length) { //(Have to get first cell of row)
var cellString = currentCell.map(function (character) {
return character.text;
}).join("");
var cellAsItem = {
x: currentCell[0].x,
y: currentCell[0].y + (pageNumber * 1000),
text: cellString,
};
tableParser.processItem(cellAsItem);
}
currentCell = [];
currentCell.push(item);
currentLine = [];
}
previousCharacterX = item.x;
currentLine.push(item);
} else if (item.page) {
if (currentCell.length) { //(Have to last cell of page)
var cellString = currentCell.map(function (character) {
return character.text;
}).join("");
var cellAsItem = {
x: currentCell[0].x,
y: currentCell[0].y + (pageNumber * 1000),
text: cellString,
};
tableParser.processItem(cellAsItem);
}
currentCell = [];
currentCell.push(item);
currentLine = [];
pageNumber = pageNumber + 1;
}
});
res.sendFile(__dirname + "/app.html");
});