-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
117 lines (102 loc) · 2.85 KB
/
server.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
// Declaring
const express = require("express");
const multer = require("multer");
const app = express();
const fs = require("fs");
const pdf = require("pdfkit");
const Tesseract = require("tesseract.js");
//middlewares
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const PORT = process.env.PORT | 5000;
const Storage = multer.diskStorage({
destination: (req, file, callback) => {
callback(null, __dirname + "/images");
},
filename: (req, file, callback) => {
callback(null, file.originalname);
}
});
const upload = multer({
storage: Storage,
fileFilter: function (req, file, cb) {
checkfiletype(file, cb);
}
}).single("image");
//checkfiletype function
function checkfiletype(file, cb) {
const filetypes = /jpeg|jpg|png/;
const extname = filetypes.test(file.originalname);
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
} else {
cb(" Error : Images Only !");
}
}
//route
app.get("/", (req, res) => {
res.render("index");
});
app.get("/contact", (req, res) => {
res.render("contact");
});
app.post("/upload", (req, res) => {
console.log(req.file);
upload(req, res, err => {
if (err) {
res.render("index", { msg: err });
} else {
if (req.file == undefined) {
res.render("index", {
msg: " Image not Selected !"
});
} else {
console.log(req.file);
var image = fs.readFileSync(
__dirname + "/images/" + req.file.originalname,
{
encoding: null
}
);
Tesseract.recognize(image)
.progress(function (p) {
console.log("progress", p);
})
.then(function (result) {
// res.send(result.text);
res.render("display", {
data: result.text,
path: __dirname + "/images/" + req.file.originalname
});
var myDoc = new pdf();
myDoc.pipe(
fs.createWriteStream(`./pdfs/${req.file.originalname}.pdf`)
);
myDoc
.font("Times-Roman")
.fontSize(24)
.text(`${result.text}`, 100, 100);
myDoc.end();
const downloadpath =
__dirname + "/pdfs/" + req.file.originalname.pdf;
app.get("/download", (req, res) => {
const file = `./pdfs/${req.file.originalname}.pdf`;
res.download(downloadpath);
res.download(file);
});
});
}
}
});
});
app.get("/download", (req, res) => {
// const file = `./pdfs/${req.file.originalname}.pdf`;
res.download(downloadpath);
});
app.get("/showdata", (req, res) => { });
app.listen(PORT, () => {
console.log(`Server running on Port ${PORT}`);
});