-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
178 lines (156 loc) · 4.69 KB
/
app.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
const fs = require("fs");
const request = require("request-promise-native");
const express = require("express");
const app = express();
const port = 3003;
var bodyParser = require("body-parser");
app.use(bodyParser.json({ limit: "25mb" })); // to support JSON-encoded bodies
app.use(
bodyParser.urlencoded({
limit: "50mb",
extended: true,
parameterLimit: 50000,
})
);
const ptp = require("pdf-to-printer");
const http = require("http");
const path = require("path");
app
.post("/print_old", (req, res) => {
console.log(req.body);
let url = req.body.url; // url file pdf
let printer = req.body.printer; // Nama printer
let copies = req.body.copies;
let paperSize = req.body.paperSize; // A4, A5, Letter sesuai setting/nama di printer device
let scale = req.body.scale; // noscale, shrink and fit
let filename = generateString(5);
http.get(url, function (resp) {
var chunks = [];
resp.on("data", function (chunk) {
chunks.push(chunk);
fs.writeFileSync("./tmp/" + filename + ".pdf", chunk);
});
resp.on("end", function () {
const options = {};
if (printer) options.printer = printer;
if (copies) options.copies = copies;
if (paperSize) options.paperSize = paperSize;
if (scale) options.scale = scale;
ptp.print("./tmp/" + filename + ".pdf", options).then(
res.json({
code: "200",
status: "success",
})
);
});
});
})
.on("error", function () {
console.log("error");
res.json({
code: "500",
status: "Error",
});
});
app
.post("/print", (req, res) => {
console.log(req.body);
let url = req.body.url; // url file pdf
let printer = req.body.printer; // Nama printer
let copies = req.body.copies;
let paperSize = req.body.paperSize; // A4, A5, Letter sesuai setting/nama di printer device
let scale = req.body.scale; // noscale, shrink and fit
let orientation = req.body.orientation; // portrait, landscape
let filename = generateString(5);
const file = fs.createWriteStream("./tmp/" + filename + ".pdf");
const request = http.get(url, function (response) {
response.pipe(file);
// after download completed close filestream
file.on("finish", () => {
file.close();
// console.log("Download Completed");
const options = {};
if (printer) options.printer = printer;
if (copies) options.copies = copies;
if (paperSize) options.paperSize = paperSize;
if (scale) options.scale = scale;
if (orientation) options.orientation = orientation;
ptp.print("./tmp/" + filename + ".pdf", options).then(
res.json({
code: "200",
status: "success",
})
);
});
});
})
.on("error", function () {
console.log("error");
res.json({
code: "500",
status: "Error",
});
});
app
.get("/listPrinter", (req, res) => {
ptp.getPrinters().then(function (listPrinter) {
console.log(listPrinter);
res.json(listPrinter);
});
})
.on("error", function () {
console.log("error");
res.json({
code: "500",
status: "Error",
});
});
async function downloadPDF(pdfURL, outputFilename) {
let pdfBuffer = await request.get({ uri: pdfURL, encoding: null });
console.log(pdfBuffer);
console.log("Writing downloaded PDF file to " + outputFilename + "...");
fs.writeFileSync("./tmp/" + outputFilename, pdfBuffer);
}
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
function generateString(length) {
let result = "";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function deleteOldTmp() {
var uploadsDir = __dirname + "/tmp";
fs.readdir(uploadsDir, function (err, files) {
files.forEach(function (file, index) {
fs.stat(path.join(uploadsDir, file), function (err, stat) {
var endTime, now;
if (err) {
return console.error(err);
}
now = new Date().getTime();
endTime = new Date(stat.ctime).getTime() + 3600000;
if (now > endTime) {
fs.unlink(path.join(uploadsDir, file), (err) => {
if (err) {
return console.error(err);
}
console.log("successfully deleted");
});
}
});
});
});
}
setInterval(deleteOldTmp, 3600000);
app.use("/", (req, res) => {
res.json({
code: "404",
status: "Not Found",
});
});
app.listen(port, () => {
console.log(`DirectPrint listening on port ${port}`);
});